[英]How to create lists using for loop in pyhton
What I want to get is我想要得到的是
[a][b][c] [a][b][c]
Getting each index from an array a=[a,b,c].从数组 a=[a,b,c] 中获取每个索引。 I'm using this to approach a value in dictionary.
我正在使用它来接近字典中的值。
All I could find was.append() but this returns me [a,b,c] not [a][b][c]我能找到的只是.append() 但这会返回 [a,b,c] 而不是 [a][b][c]
This is the function that I made这是我做的function
Def loop():
Newlist = []
for i in a:
Newlist.append(i)
return Newlist
You want to append a list of one value, not a single value.您希望 append 列出一个值,而不是单个值。 ie
IE
>>> new_list = []
>>> new_list.append([1])
>>> new_list.append([2])
>>> new_list.append([3])
[[1], [2], [3]]
So in the method you'd do something like this:所以在方法中你会做这样的事情:
def loop():
new_list = []
for i in a:
new_list.append([i])
return new_list
Try this:尝试这个:
Create a new list inside the loop that gets reset each time, add your variable, then add the new list to the parent list.在每次重置的循环内创建一个新列表,添加变量,然后将新列表添加到父列表。
You can also grab individual lists from the parent list using the following: Newlist[1]您还可以使用以下方法从父列表中获取单个列表:Newlist[1]
Def loop():
Newlist = []
for i in a:
temp_list = []
temp_list.append(I)
Newlist.append(temp_list)
return Newlist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.