Creating Sublists from a “List”

前端 未结 5 2043
余生分开走
余生分开走 2021-01-24 08:21

I have a list in a file, looks like this

c4a24534,2434b375,e750c718, .... 

I have split at \",\" and brought the below list.

x         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-24 09:05

    You can use zip to separate the list into two collections.

    x=[
    ['c4a2', '4534'], 
    ['2434', 'b375'],
    ['e750', 'c718']
    ]
    
    i,q = zip(*x)
    
    print i
    print q
    

    Result:

    ('c4a2', '2434', 'e750')
    ('4534', 'b375', 'c718')
    

    These are tuples, though. If you really want lists, you would need to convert them.

    i,q = map(list, zip(*x))
    #or:
    i,q = [list(tup) for tup in zip(*x)]
    #or:
    i,q = zip(*x)
    i, q = list(i), list(q)
    

提交回复
热议问题