简体   繁体   English

如何在 Python 中使用任何进入 for 循环的方式创建新列表?

[英]How to create new list with any enter to for-loop in Python?

i want after end second for-loop the create new list, in other words when j increment define new list我想在第二个 for-loop 结束后创建新列表,换句话说,当 j 增加定义新列表时

the code is:代码是:

for j in range(0, countOfAtt):
    for i in range(j, len(content3), countOfAtt):
        list1.append(content3[i])

thank you in advance先感谢您

Does this work?这行得通吗?

final_list = []  # this will hold all the lists you created in for loop
for j in range(0, countOfAtt):
    mylist = []   # initiate a new list
    for i in range(j, len(content3), countOfAtt):
        mylist.append(content3[i])
    final_list.append(mylist)

This will give you a new list (with whatever you were trying to put in it) for every j :这将为每个j为您提供一个新列表(包含您尝试放入的任何内容):

[[content3[i] for i in range(j, len(content3), countOfAtt)] for j in range(countOfAtt)]

If it doesn't give you what you want, then you'll have to show samples of the data you have before the loops, and of the output you desire, so we could check the logic.如果它没有给你想要的东西,那么你必须展示你在循环之前拥有的数据样本,以及你想要的 output,这样我们就可以检查逻辑。

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

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