简体   繁体   English

每次需要时在 for 循环中创建一个新列表

[英]Create a new list in a for loop every time necessary

So I am trying to create a new SEPARATE list every time a statement is true in python.因此,每次在 python 中语句为真时,我都尝试创建一个新的 SEPARATE 列表。 Essentially:本质上:

for i in range(z):
    if X is true:
        create a new list

There is no way to know how many lists I need, I can try to estimate as I am doing a greedy type algorithm but I am struggling to figure out how iteratively make new separate lists.没有办法知道我需要多少个列表,我可以尝试估计,因为我正在做一个贪心类型的算法,但我正在努力弄清楚如何迭代地制作新的单独列表。

Try doing尝试做

myList = []
for i in range(z):
  if X is true:
    lis = []        
    #do something with lis
  myList.append(lis)

If you post exactly what you want to do after creating a new list, then a more efficient answer can be explained.. Based on the provided information, this is all I could come up with.如果您在创建新列表后准确发布您想要执行的操作,那么可以解释更有效的答案.. 根据提供的信息,这就是我能想到的全部内容。

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

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