尝试列表理解:
lst = [[] for _ in xrange(a)]
见下文:
>>> a = 3>>> lst = [[] for _ in xrange(a)]>>> lst[[], [], []]>>> a = 10>>> lst = [[] for _ in xrange(a)]>>> lst[[], [], [], [], [], [], [], [], [], []]>>> # This is to prove that each of the lists in lst is unique>>> lst[0].append(1)>>> lst[[1], [], [], [], [], [], [], [], [], []]>>>
但是请注意,以上内容适用于Python2.x。在Python 3.x.上,由于
xrange已被删除,因此您需要:
lst = [[] for _ in range(a)]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)