简体   繁体   中英

How print a list into separate lists python

I have a list:

grades= ['doe john     100  90  80  90', 'miller sally  70  90  60 100  80', 'smith jakob   45  55  50  58', 'white jack    85  95  65  80  75']

I want to be able to break that list so the output would be:

['doe john     100  90  80  90']
['miller sally  70  90  60 100  80']
['smith jakob   45  55  50  58']
['white jack    85  95  65  80  75']

Additionally, I would like to split the elements in the list so it looks like:

['doe', 'john',     '100',  '90',  '80',  '90']
['miller', 'sally',  '70',  '90',  '60', '100',  '80']
['smith', 'jakob',   '45',  '55',  '50',  '58']
['white', 'jack',    '85',  '95',  '65',  '80',  '75']

I'm not really sure how to go about doing this or if this is even possible as I'm just starting to learn python. Any ideas?

for l in grades:
    l = l.split()

OR

final = [l.split() for l in grades]

See Split string on whitespace in Python

This can be done quickly with .split() in a list comprehension.

grades = ['doe john     100  90  80  90', 'miller sally  70  90  60 100  80', 'smith jakob   45  55  50  58', 'white jack    85  95  65  80  75']

grades = [grade.split() for grade in grades]
print (grades)

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