奇怪的问题,第 6 行和第 11 行,很奇怪,我不知道为什么?
1 l1 = ['a', 'b', 'c']
2
3 l2 = [[]] * 3
4 for i in xrange(0, len(l1)):
5 l2[i%len(l1)].extend(l1[i]) # look! not [li[i]] here
6 print 'l2: ', l2 # problem is here
7
8 l3 = [[]] * 3
9 for i in xrange(0, len(l1)):
10 l3[i%len(l1)].extend([l1[i]])
11 print 'l3: ', l3
12
13 l4 = [[]] * 3
14 for i in xrange(0, len(l1)):
15 if l4[i%len(l1)] == []:
16 l4[i%len(l1)] = [l1[i]]
17 else:
18 l4[i%len(l1)].extend([l1[i]])
19 print 'l4: ', l4
输出打击:
l2: [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
l3: [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
l4: [['a'], ['b'], ['c']]
有人可以指出为什么吗?谢谢。