Reputation: 69
How can we take input of n number of lists in python
for example
2
1 2 3
4 5 6 7
here 2 is specifying number of lists that are going to input
1 2 3 is one list
4 5 6 7 is second list
another example
3
1 2 3
4 5 6 8
2 3 5 7
3 indicates 3 lists are going to input
1 2 3 is list one
4 5 6 8 is list two
2 3 5 7 id list three
i have done this code
n=input()
for i in range(n):
b=map(int,raw_input().split())
i am struck with this how can i take input for n number of lists i able to take only one list into one variable i want take to different different variables
Upvotes: 0
Views: 1152
Reputation: 46
There are a few things to fix:
Keep it simple and use lists instead of a map.
n = int(input())
rows = [] # will contain the input as a list of integers
for i in range(n):
row_string = input()
row = [int(num) for num in row_string.split()]
rows.append(row)
Upvotes: 0
Reputation: 321
do you want to read from file or cli?
If you read from file, you can iterate over its content line by line and work only in the specific lines.
The lines input you can split to get the single numbers into a list.
nums=[]
with open(infile.txt) as f:
n=0
for i, line in enumerate(f):
if i==1:
n == 1
elif i <= n
nums[i] = line.split()
Upvotes: 0
Reputation: 82919
i want take to different different variables
You can not assign the input to "different" variables in the loop, particularly if you do not know how large n
will be. Instead, you should append the different values of b
to a list of lists, e.g. bs
.
n = input()
bs = []
for i in range(n):
bs.append(map(int, raw_input().split()))
Or use a list comprehension:
bs = [map(int, raw_input().split()) for _ in range(n)]
Upvotes: 1