简体   繁体   English

动态声明列表中的子列表

[英]Dynamically declaring sub-list within a list

I have a list PairObject which could have any number of elements. 我有一个列表PairObject ,可以有任意数量的元素。
eg PairObject = ['abc','efg','jkl'] . 例如PairObject = ['abc','efg','jkl'] The elements of list PairObject keeps changing on different condition. 列表PairObject的元素在不同条件下不断变化。

Now I have a list Price=[[],[],[],[]] where each sub-list within list Price is associated to each element of list PairObject so that the number of sub-lists in list Price is equal to the len(PairObject) . 现在我有一个列表Price=[[],[],[],[]]其中列表中的每个子列表Price与列表PairObject每个元素相关联,因此列表中的子列表数量等于len(PairObject)

the code is written something like this... 代码是这样编写的......

PairObject =['abc','efg','jkl']
Price=[[],[],[],[]]
for j in range(0,len(PairObject))
     Price[j].append()

Now my question is ... how can I dynamically declare Price, when I don't know the len(PairObject)... which could be 2,3,4 or any number... 现在我的问题是......当我不知道len(PairObject)时,我怎么能动态地声明Price ...哪个可能是2,3,4或任何数字......

If you mean you want 如果你的意思是你想要的

len(Price) == len(PairObject)

then you can declare Price as 然后你可以将Price声明为

Price = [[] for _ in range(len(PairObject))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM