Python list entry error

grid = []
for _ in range(3):
    grid.append(raw_input().split())

Input:

000
000
000

Exit : [['000'], ['000'], ['000']].

How do I change my code to get the result?

[['0','0','0'], ['0','0','0'],['0','0','0']]

+4
source share
2 answers

You have:

"000".split() == ["000"]

Do you want to:

list("000") == ["0", "0", "0"]
+10
source

You do not put a space between every 0 so as not to split anything, just list the list on raw_input:

grid = [list(raw_input())]
for _ in range(3):
    grid.append(list(raw_input()))

You can also use the comp list:

 grid = [list(raw_input()) for _ in range(3)]

If you want to split, you will need to enter 0 0 0with spaces in between.

In [1]: "0 0 0".split()
Out[1]: ['0', '0', '0']

Trying to split "000"returns ['000']because there is no separator to split, no spaces, etc.

+3
source

All Articles