如何在Python中根据输入生成列表

2024-03-29 09:11:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在寻找解决办法。我的要求是根据for循环中的输入生成单独的列表。 示例:我的for循环范围从0到I(I是用户输入)。每次迭代都会生成一个列表。我希望结果列表保存在单独的列表中。例如,第一次迭代产生的列表应保存在列表A中,第二次迭代产生的列表应保存在列表B中,以此类推,直到第i次迭代。你知道吗


Tags: 用户示例列表for解决办法
1条回答
网友
1楼 · 发布于 2024-03-29 09:11:24

i是一个变量在这里。所以呢你不知道你需要多少lists。这取决于用户的输入。所以你可以用另一个list(而不是list Alist 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)

相关问题 更多 >