Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 有没有办法创建和用户输入一样多的列表?_Python_List_Variables - Fatal编程技术网

Python 有没有办法创建和用户输入一样多的列表?

Python 有没有办法创建和用户输入一样多的列表?,python,list,variables,Python,List,Variables,我想知道如何创建尽可能多的用户输入列表 假设用户输入为4 我想创建4个列表名Batch1 Batch2 Batch3 Batch4 从具有相同Batch1-4.csv名称的csv文件检索数据 for i in range(1,3): list("Batch{0}".format(i)) print(Batch1) 我已经尝试过了,但是由于我没有直接声明Batch1,所以导致错误Batch1没有定义 您有什么解决方法吗?您可以使用嵌套循环概念。创建列表的列表。比如: main_l

我想知道如何创建尽可能多的用户输入列表

假设用户输入为4

我想创建4个列表名Batch1 Batch2 Batch3 Batch4

从具有相同Batch1-4.csv名称的csv文件检索数据

for i in range(1,3):
    list("Batch{0}".format(i))
    print(Batch1)
我已经尝试过了,但是由于我没有直接声明Batch1,所以导致错误Batch1没有定义


您有什么解决方法吗?

您可以使用嵌套循环概念。创建
列表的
列表
。比如:

main_list = list()
for i in range(4):
    temp_list = ["batch{0}".format(i)]
    main_list.append(temp_list)

您可以这样使用列表理解:

>>> main_list = ["batch{0}".format(i) for i in range(4)]
>>> main_list
['batch0', 'batch1', 'batch2', 'batch3']
如果需要列表列表,请执行以下操作:

>>> main_list = [["batch{0}".format(i)] for i in range(4)]
>>> main_list
[['batch0'], ['batch1'], ['batch2'], ['batch3']]
使用用户输入,您的脚本可能如下所示:

n = int(input('Enter a number:'))
main_list = [["batch{0}".format(i)] for i in range(1,n+1)]

他不希望名字是一个列表吗?主列表是一个列表。不知道你在问什么。我必须承认,原来的问题让我有点困惑。我认为@RedCricket的思路是正确的。OP想写一个程序来生成
n
字符串,这样他就可以打开同名文件。谢谢Red Cricket。你答对了我的问题。现在我只需要为我要使用的批处理[i]指定main_list[i]。再次感谢并对我的模棱两可的问题表示抱歉:)如果您构建了一个列表列表,
main\u list[0]
将产生
['batch1']
,而
main\u list[0][0]
将产生
'batch1'
。只需构造一个列表,使
main_list[0]
产生
batch1
可能会更简单。您可以分享有关您请求的更多信息吗?谢谢Taohidul,不过为了代码目的,我稍微调整了您的代码我的temp_list=“batch{0}”。格式(I)但其余的工作正常。再次感谢