Python - String to list

前端 未结 5 760
情深已故
情深已故 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 01:00

    Use a serialization library like json:

    import json
    
    l1 = ['aa','bb','cc']
    s = json.dumps(l1)
    l2 = json.loads(s)
    
    print s
    print l1 == l2
    

提交回复
热议问题