[英]How can I make a list of lists from user input?
Here is what I did-这就是我所做的-
grid_len = input("Enter Grid Length: ") #Assuming grid_length to be 3
s = []
while True:
s.append(input())
if len(s) == int(grid_len)**2: #grid_length^2 will be 9
print(s)
break
When Input is for example 1 in the first loop, 2 in the second, 3 in the third and so on upto 9;例如,当 Input 在第一个循环中为 1,在第二个循环中为 2,在第三个循环中为 3,依此类推直到 9; It creates a list like this:
它创建一个这样的列表:
['1','2','3','4','5','6','7','8','9']
But I want it something like this:但我想要这样的东西:
[[1,2,3],[4,5,6],[7,8,9]]
A version based on list comprehension.基于列表理解的版本。
s = [[input("Enter number: ") for _ in range(grid_len)] for _ in range(grid_len)]
print s
Note: Two forward slashes "//" are not valid python comment identifiers注意:两个正斜杠“//”不是有效的python注释标识符
Something I found from this question: How do you split a list into evenly sized chunks?我从这个问题中发现了一些东西: 如何将列表分成大小均匀的块?
>>> mylist = [1,2,3,4,5,6,7,8,9]
>>> def chunks(l, n):
... return [l[i:i+n] for i in range(0, len(l), n)]
>>> chunks(mylist,3)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Integrated into your code:集成到您的代码中:
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
grid_len = input("Enter Grid Length: ")
s = []
while True:
s.append(int(input())) # Notice I put int() around input()
if len(s) == int(grid_len)**2:
s = chunks(s,grid_len)
print(s)
break
EDIT: Changed the second parameter in chunks to match grid_len
.编辑:更改块中的第二个参数以匹配
grid_len
。 This will now work for not just 3.这现在不仅适用于 3。
Here's my code:这是我的代码:
grid_len = input("Enter Grid Length: ")
s = []
for i in range(grid_len): #looping to append rows
s.append([]) #append a new row
for j in range(grid_len): #looping to append cells
s[-1].append(input()) #append a new cell to the last row, or you can also append to `i`th row
I have got your problem, the very simple solution is我有你的问题,非常简单的解决方案是
grid_length = int(input())
s = []
for i in range(grid_length):
b = list(map(int, input().split()))
s.append(b)
print(s)
试试这个:
arr = [list(map(int, input().split())) for i in range(int(input()))]
You should create a new sub-list every grid_length
elements:您应该为每个
grid_length
元素创建一个新的子列表:
grid_len = int(input("Enter Grid Length: "))
s = []
for _ in range(grid_length):
sub_list = []
for _ in range(grid_length):
sub_list.append(input())
s.append(sub_list)
print(s)
Note that, in general, you should use for
every time that you have to iterate sequentially over an object or you know how many times to repeat a loop.请注意,通常,您应该
for
每次必须顺序迭代对象或知道重复循环的次数时使用for
。 while
is generally better to handle "strange" conditions that are hard to factor in terms of number of iterations or iterating over an iterable. while
通常更好地处理难以根据迭代次数或迭代迭代次数的“奇怪”条件。
Using nested list comprehensions:使用嵌套列表推导式:
>>> grid_len = input("Enter Grid Length: ")
Enter Grid Length: 4
>>> incrementer = iter(xrange(1, grid_len ** 2 + 1))
>>> s = [[next(incrementer) for x in xrange(grid_len)] for y in xrange(grid_len)]
>>> print s
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
试试这个:
x = [[int(input()) for c in range(grid_len)] for r in range(grid_len)]
Try this:试试这个:
grid_len = int(input()) #just one input
grid = [[input(),input(),input()] for _ in range(grid_len)] #grid_len * 3 inputs
print(grid)
Here is my code.这是我的代码。 Using this we can have different sizes of lists inside the list:
使用它,我们可以在列表中拥有不同大小的列表:
len_of_list = int(input('Enter the first layer list size: '))
list_of_list = []
for i in range(len_of_list):
len_of_elem_inside_list = int(input('Enter the second layer list sizes: '))
inside_list =[]
for j in range(len_of_elem_inside_list):
elements_in_the_list = int(input('Enter the elements inside the list of list: '))
inside_list.append(elements_in_the_list)
list_of_list.append(inside_list)
print('Output: ',list_of_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.