Python - String to list

前端 未结 5 755
情深已故
情深已故 2021-01-15 00:52

I need cast a list to a string and get back the string to a list. There\'s a python way to make this behavior?

l1 = [\'aa\',\'bb\',\'cc\']
s = str(l1)
l2 = c         


        
5条回答
  •  孤街浪徒
    2021-01-15 00:57

    If you are always working with a list, then the following kind of thing should work just fine. Beware, it will not cast ints to ints and such; they will always be string items in the list.

    l1 = ['aa','bb','cc']
    s = str(l1)
    
    l2 = s[1:-1].split(',')
    
    print l2
    ['aa', 'bb', 'cc']
    

    You probably want to check the boundaries of the string and such to check that it's long enough for that array stuff too...

提交回复
热议问题