我正在寻找解决方案。 我的要求是根据for循环中的输入生成单独的列表。 示例:我的for循环范围从0到i(我是用户输入)。 每次迭代都会生成一个列表。 我希望将结果列表保存在单独的列表中。 例如,第一次迭代导致的列表应该保存在列表A中,第二次迭代导致的列表应该保存在列表单独的列表B中,依此类推,直到第二次迭代。
发布于 2018-07-25 18:10:04
i
是一个变量,所以你不知道你需要多少lists,
这取决于用户的输入。所以你可以用另一个list
(而不是list A
,list B
),它将保存其他列表。你可以这样做:
i= int(input("how many iteration : ")) #input value of i
all_lists = [] #this will store your all lists
for j in range(i): #loop from 0 to i
temp_list = [] #this will hold the temporary lists
n = int(input("number of inputs : ")) #how many inputs will be in each list
for k in range(n):
value = input()
temp_list.append(value) #insert the values to temporary list
all_lists.append(temp_list) #insert the temporary list to your main list
print(all_lists)
https://stackoverflow.com/questions/-100005754
复制相似问题