简体   繁体   中英

Splitting Elements in a 2Dlist into sub-elements? Python 3

I'm a brand new programmer and started out with python as my first language.

for user in range (1):
    rowcol=input()
a=[]
a=rowcol.split(" ")
rowNum=int(a[0])
colNum=int(a[1])

board=[]    

for row_count in range(rowNum):
    row=[]
    row.append(input())
    board.append(row)
print(board)

the first input is "2 2" which determines the rows and columns the next input is "--" and "--" which inputs these characters in both rows

the array I have rn displays [['--'], ['--']] I need it to display this [['-','-'], ['-','-']]

I don't know how to attempt splitting each element in the array

尝试以下这种方法:

output = [list(y) for y in x for x in board]

I think I achieved the result you desire by replacing this line

row.append(input())

with this one

row += [char for char in input()]

If you are not familiar with the syntax, please check out List Comprehensions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM