Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为for循环的每个迭代创建新列表_Python_List_Loops - Fatal编程技术网

Python 为for循环的每个迭代创建新列表

Python 为for循环的每个迭代创建新列表,python,list,loops,Python,List,Loops,我有一个字符串列表,我想附加到两个不同的列表中。我无法在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 这是我得到的错误: “类型”对象不支持项分配 如果您希望将数字保留为字符串,欢迎提供任何建议: [i.split(", &

我有一个字符串列表,我想附加到两个不同的列表中。我无法在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
这是我得到的错误: “类型”对象不支持项分配


如果您希望将数字保留为字符串,欢迎提供任何建议:

[i.split(", ") for i in Input]
>> [['1', '3', '4', '7', '13'], ['1', '2', '4', '13', '15']]
如果要将它们更改为整数:

[[int(n) for n in i.split(", ")] for i in Input]
>> [[1, 3, 4, 7, 13], [1, 2, 4, 13, 15]]

我想你只想打开列表,你也可以这样做:

f_list , s_list = Input

是否要列表中的一个字符串和其他列表中的另一个字符串?请从中重复和。我们不知道你期望得到什么结果。我们希望您做出诚实的尝试,然后就您的算法或技术提出具体问题。堆栈溢出不是为了替换现有的文档和教程。你测试过你当前的代码吗?结果是什么,哪里出了问题?请提供一个答案。您想要完成什么还不清楚。我希望列表中有一个字符串,另一个字符串在不同的列表中。谢谢,但理想情况下,我希望它们在两个具有不同名称的单独列表中。您可以使用上述代码将它们保存到两个列表中。像
l1,l2=[i.split(“,”)表示输入中的i]