我想知道如何创造尽可能多的列表作为用户输入。
比方说,用户输入是4。
我想创建4个列表名称批次1个批次2批次3批次4
用相同的名字Batch1-4.csv检索CSV文件中的数据
for i in range(1,3):
list("Batch{0}".format(i))
print(Batch1)
我曾经试过,但导致错误BATCH1没有定义的,因为我没有直接声明BATCH1。
你有什么解决方法吗?
你可以使用列表理解像这样:
>>> 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)]
您可以使用嵌套循环的概念。创建list
的lists
。喜欢 :
main_list = list()
for i in range(4):
temp_list = ["batch{0}".format(i)]
main_list.append(temp_list)