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

后端 未结 14 669
予麋鹿
予麋鹿 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 03:58

    Just to add on to the existing answers: hopefully, you'll encounter something more like this in the future:

    >>> word = 'abc'
    >>> L = list(word)
    >>> L
    ['a', 'b', 'c']
    >>> ''.join(L)
    'abc'
    

    But what you're dealing with right now, go with @Cameron's answer.

    >>> word = 'a,b,c'
    >>> L = word.split(',')
    >>> L
    ['a', 'b', 'c']
    >>> ','.join(L)
    'a,b,c'
    

提交回复
热议问题