但它不太快。有没有办法在没有循环的情况下做到这一点?Hi!I would like to create a pretty big list of lists; a list 3,000,000long, each entry containing 5 empty lists. My application will appenddata each of the 5 sublists, so they will be of varying lengths (so noarrays!).Does anyone know the most efficient way to do this? I have tried:list = [[[],[],[],[],[]] for _ in xrange(3000000)]but its not soooo fast. Is there a way to do this without looping? 你可以这样做: [[[],[],[],[],[]]] * 3000000 虽然我不知道它的表现是否比你已经拥有的更好。 有。You can do:[[[],[],[],[],[]]] * 3000000although I don''t know if it performs any better than what you alreadyhave. Paul Rudin写道:Paul Rudin wrote: Dr Mephesto< dn **** @ googlemail.comwrites:Dr Mephesto <dn****@googlemail.comwrites: >嗨! 我想创建一个非常大的列表列表;列表3,000,000 长,每个条目包含5个空列表。我的应用程序将附加5个子列表中的每一个的数据,因此它们将具有不同的长度(因此没有数组!)。 有谁知道最有效的方法这个?我试过了: list = [[[],[],[],[],[]]对于_xrange(3000000)] 但它不是太快了有没有办法在没有循环的情况下做到这一点?>Hi!I would like to create a pretty big list of lists; a list 3,000,000long, each entry containing 5 empty lists. My application will appenddata each of the 5 sublists, so they will be of varying lengths (so noarrays!).Does anyone know the most efficient way to do this? I have tried:list = [[[],[],[],[],[]] for _ in xrange(3000000)]but its not soooo fast. Is there a way to do this without looping? 你可以这样做: [[[],[],[],[],[]]] * 3000000 虽然我不知道它的表现是否比你已经有的好b $ b。You can do: [[[],[],[],[],[]]] * 3000000although I don''t know if it performs any better than what you alreadyhave. 您知道这是非常不同的,因为嵌套列表是 引用,而不是新实例?因此结果很可能(鉴于人们偶然发现了这么多次),这不是理想的结果...... DiezYou are aware that this is hugely different, because the nested lists arereferences, not new instances? Thus the outcome is most probably (given thegazillion of times people stumbled over this) not the desired one...Diez Paul Rudin写道:Paul Rudin wrote: 博士写道:Dr writes: > ;我想创建一个相当大的列表列表;列表3,000,000 长,每个条目包含5个空列表。我的应用程序将附加5个子列表中的每一个的数据,因此它们将具有不同的长度(因此没有数组!)。 有谁知道最有效的方法这个?我试过了: list = [[[],[],[],[],[]]对于_xrange(3000000)] 但它不是太快了有没有办法在没有循环的情况下做到这一点?>I would like to create a pretty big list of lists; a list 3,000,000long, each entry containing 5 empty lists. My application will appenddata each of the 5 sublists, so they will be of varying lengths (so noarrays!).Does anyone know the most efficient way to do this? I have tried:list = [[[],[],[],[],[]] for _ in xrange(3000000)]but its not soooo fast. Is there a way to do this without looping? 你可以这样做: [[[],[],[],[],[]]] * 3000000 虽然我不知道它的表现是否比你已经有的好b $ b。You can do: [[[],[],[],[],[]]] * 3000000although I don''t know if it performs any better than what you alreadyhave. 实际上,它产生3000000个引用相同 5元素列表的列表。一个简化的例子:Actually, that produces list of 3000000 references to the same5-element list. A reduced example: >> lst = [[[],[], [],[],[]]] * 3 lst [1] [1] .append(42) print lst>>lst = [[[],[],[],[],[]]] * 3lst[1][1].append(42)print lst [[[],[42],[],[],[]],[[],[42],[],[],[]],[[],[ 42],[],[],[]]] - --Bryan[[[], [42], [], [], []], [[], [42], [], [], []], [[], [42], [], [], []]]----Bryan 这篇关于创造真正的大名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-07 02:58