首页 > 代码库 > 定义列表,并给其中某一项赋值

定义列表,并给其中某一项赋值

 a=[[]]*10
>>> a
[[], [], [], [], [], [], [], [], [], []]
>>> a[2].append(10)
>>> a[2]
[10]
>>> a
[[10], [10], [10], [10], [10], [10], [10], [10], [10], [10]]

>>> a=[[] for _ in xrange(10)]
>>> a[0].append(10)
>>> a
[[10], [], [], [], [], [], [], [], [], []]

定义列表,并给其中某一项赋值