[英]create new list for every iteration of for loop
I have a list of strings I want to append to two different lists.我有一个字符串列表,我想将 append 放到两个不同的列表中。 I can't create two separate lists within the for loop
我无法在 for 循环中创建两个单独的列表
Input=["1, 3, 4, 7, 13", "1, 2, 4, 13, 15"]
i=0
for string in Input:
list[i]=[]
list[i].append(string)
i+=1
This is the error I get: 'type' object does not support item assignment这是我得到的错误:'type' object does not support item assignment
Any advice will be appreciated任何建议将被认真考虑
If you want to keep the numbers as strings:如果要将数字保留为字符串:
[i.split(", ") for i in Input]
>> [['1', '3', '4', '7', '13'], ['1', '2', '4', '13', '15']]
If you want to change them to integers:如果要将它们更改为整数:
[[int(n) for n in i.split(", ")] for i in Input]
>> [[1, 3, 4, 7, 13], [1, 2, 4, 13, 15]]
I guess you only want to unpack the list, you can do it likewise:我猜你只想解压列表,你也可以这样做:
f_list , s_list = Input
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.