Python 循环特定次数,每次创建一个新列表

Python 循环特定次数,每次创建一个新列表,python,list,loops,Python,List,Loops,基本上我想做的是有一个基于用户输入的列表程序,例如: a=input b=input c=input list1=[a,b,c] listMASTER=[list1,list2,list3...list36] 然后让它再做一次(形成列表2),然后再做一次,依此类推,直到它到达列表37,我想在这里列出列表,例如: a=input b=input c=input list1=[a,b,c] listMASTER=[list1,list2,list3...list36] 我不想写这个: a

基本上我想做的是有一个基于用户输入的列表程序,例如:

a=input
b=input
c=input

list1=[a,b,c]
listMASTER=[list1,list2,list3...list36]
然后让它再做一次(形成列表2),然后再做一次,依此类推,直到它到达列表37,我想在这里列出列表,例如:

a=input
b=input
c=input

list1=[a,b,c]
listMASTER=[list1,list2,list3...list36]
我不想写这个:

a=input
b=input
c=input

listn=[a,b,c]

36次,因此我希望它在每次形成一个新列表时反复循环。

尝试以下方法:

outer_listen = []
n = 36 #Or howmany ever times you want to loop
for i in range(n): #0 through 35
    a = input()
    b = input()
    c = input()
    lstn = [a, b, c]
    outer_listen.append(lstn)

试着这样做:

outer_listen = []
n = 36 #Or howmany ever times you want to loop
for i in range(n): #0 through 35
    a = input()
    b = input()
    c = input()
    lstn = [a, b, c]
    outer_listen.append(lstn)

使用此方法可以轻松完成:

olist=[]
for i in range(n): #n is the number of items you need the list (as in your case, 37)
    lis=[input(),input(),input()]
    olist.append(lis)

这将减少步骤的数量

使用此方法可以轻松完成:

olist=[]
for i in range(n): #n is the number of items you need the list (as in your case, 37)
    lis=[input(),input(),input()]
    olist.append(lis)

这将减少步骤的数量

有点密集,但仍然可以通过列表阅读

n = 36
prompt = 'Please enter for {}{}: '
all_inputs = [[input(prompt.format(char, num)) for char in 'abc'] 
               for num in range(n)]
print(all_inputs)
为您提供36 x 3的输入提示:

Please enter for a0: 1
Please enter for b0: 2
Please enter for c0: 3
Please enter for a1: 4
Please enter for b1: 5
Please enter for c1: 6
...
[['1', '2', '3'], ['4', '5', '6'], ...]

有点密集,但仍然可以通过列表阅读

n = 36
prompt = 'Please enter for {}{}: '
all_inputs = [[input(prompt.format(char, num)) for char in 'abc'] 
               for num in range(n)]
print(all_inputs)
为您提供36 x 3的输入提示:

Please enter for a0: 1
Please enter for b0: 2
Please enter for c0: 3
Please enter for a1: 4
Please enter for b1: 5
Please enter for c1: 6
...
[['1', '2', '3'], ['4', '5', '6'], ...]

可以使用嵌套循环:

list_of_lists = [[input() for _ in range(3)] for _ in range(36)]
或者更方便地,也可以接受来自文件的输入,例如,使用csv格式:

a,b,c
d,f,g
...
对应代码:

import csv
import fileinput

list_of_lists = list(csv.reader(fileinput.input()))
用法:

$ python make-list.py input.csv


可以使用嵌套循环:

list_of_lists = [[input() for _ in range(3)] for _ in range(36)]
或者更方便地,也可以接受来自文件的输入,例如,使用csv格式:

a,b,c
d,f,g
...
对应代码:

import csv
import fileinput

list_of_lists = list(csv.reader(fileinput.input()))
用法:

$ python make-list.py input.csv