+ 1
2D List in Python Error
The input format: 2 2 2 3 4 5 1st line represents the dimension. 2 is the row. 2 is the column. The next lines represent the elements in the list However, my codes below are wrong n, p = [int(x) for x in input().split()] #n= row, p= col num=[] col=[] for i in range(n): num1, num2=[ float(x) for x in input().split()] col.append(num1) col.append(num2) num.append(col) print(num) How to make a 2D list in Python properly so that the output is: [ [2,3], [4,5] ]
6 Réponses
+ 3
for i in range(n):
col=[]
num1, num2=[ float(x) for x in input().split()]
col.append(num1)
col.append(num2)
num.append(col)
+ 4
Hi, Muhd Khairul Amirin !
Here’s an anorher approach:
https://code.sololearn.com/caEgy55umbH3/?ref=app
+ 3
Hello Khairul,
What if 1st number is greater than 1, is it possible?
2 2
2 3
4 5
If it was possible, how the resuting `list` will be?
+ 3
Ipang Thanks for pointing out my mistake. 😅
+ 3
Per Bratthammar Thanks for sharing👍
+ 2
Thanks Oma Falk