Reputation: 1315
I need to place user input in a multi dimension list in python. I am new at Python. Some user inputs would be
11001111
10001100
11000000
I need to place them in list
a[[1,1,0,0,1,1,1,1],[1,0,0,0,1,1,0,0],[1,1,0,0,0,0,0,0]]
What I do. And its completely useless
for i in range(3):
for b in range(7):
a[i][b] = int(input())
Upvotes: 0
Views: 841
Reputation: 13317
This could work too :
a = []
for x in range(3):
a.append([int(let) for let in input('enter number:\n')])
Upvotes: 0
Reputation: 140168
If you accept that the users types 21 digits+enter each time, what you're doing works, although it isn't the best way, looks a lot like C code.
However, it's not useless, it works provided you initialize your bidimensional array properly for instance like this:
a = [[0]*7 for _ in range(3)]
this creates 3 lists of 7 elements. You can read/write them with this exact loop you posted.
If you want to read 3 lines of single digit numbers, you could do it in only like using double list comprehension:
a = [[int(x) for x in input()] for _ in range(3) ]
Upvotes: 1
Reputation: 1653
Try doing this:
a = []
for i in range(3):
k = raw_input()
a.append([int(j) for j in k])
print a
Upvotes: 0
Reputation: 249123
You can either read a line at a time and process individual characters:
for i in range(3):
line = input()
for b in range(7):
a[i][b] = int(line[b])
Or you can read a character at a time:
sys.stdin.read(1)
Upvotes: 3