我写了一个函数,该函数应该将2D列表插入表中。将obj插入列表中一次
这是代码:
seats_plan = [[True, True, True, True, True], [True, True, True, True, True], [True, True, True, True, True], [True, True, True, True, True], [True, True, True, True, True]]
def print_database(seats_plan):
for row in seats_plan:
row.insert(0, seats_plan.index(row))
seats_plan.insert(0, [' ', '0', '1', '2', '3', '4'])
for k in seats_plan:
for char in k:
if char is True:
print '.',
elif char is False:
print 'x',
else:
print char,
print
,输出是:
0 1 2 3 4
0 . . . . .
1 . . . . .
2 . . . . .
3 . . . . .
4 . . . . .
,但它也改变seats_plan
,所以如果我再次调用该函数再次插入数字。 如何在不更改原来的seats_plan
的情况下只插入一次?
您应该创建一个副本* *列表和修改,而不是原来的。 – jonrsharpe
你想要一个函数在第一次被调用时将事物插入表中,但不是第二次? –