首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何根据Python中的输入生成列表?

如何根据Python中的输入生成列表?
EN

Stack Overflow用户
提问于 2018-07-25 09:00:11
回答 1查看 0关注 0票数 0

我正在寻找解决方案。 我的要求是根据for循环中的输入生成单独的列表。 示例:我的for循环范围从0到i(我是用户输入)。 每次迭代都会生成一个列表。 我希望将结果列表保存在单独的列表中。 例如,第一次迭代导致的列表应该保存在列表A中,第二次迭代导致的列表应该保存在列表单独的列表B中,依此类推,直到第二次迭代。

EN

回答 1

Stack Overflow用户

发布于 2018-07-25 18:10:04

i是一个变量,所以你不知道你需要多少lists,这取决于用户的输入。所以你可以用另一个list(而不是list Alist B),它将保存其他列表。你可以这样做:

代码语言:txt
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档