How to convert a string with comma-delimited items to a list in Python?

后端 未结 14 640
予麋鹿
予麋鹿 2020-11-28 03:09

How do you convert a string into a list?

Say the string is like text = \"a,b,c\". After the conversion, text == [\'a\', \'b\', \'c\'] and h

14条回答
  •  时光取名叫无心
    2020-11-28 04:04

    All answers are good, there is another way of doing, which is list comprehension, see the solution below.

    u = "UUUDDD"
    
    lst = [x for x in u]
    

    for comma separated list do the following

    u = "U,U,U,D,D,D"
    
    lst = [x for x in u.split(',')]
    

提交回复
热议问题