在python中将字符串转换为列表 [英] string to list conversion in python
本文介绍了在python中将字符串转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串.
s = '1989, 1990'
我想使用python&将其转换为列表我想要输出为
I want to convert that to list using python & i want output as,
s = ['1989', '1990']
有没有最快的班轮方式呢?
Is there any fastest one liner way for the same?
推荐答案
使用分割方法:
>>> '1989, 1990'.split(', ')
['1989', '1990']
但是您可能想要:
-
使用替换
由','
因此:
>>> '1989, 1990,1991'.replace(' ', '').split(',')
['1989', '1990', '1991']
如果您的字符串来自用户输入,这样做会更好,因为用户可能会忘记在逗号后打空格.
This will work better if your string comes from user input, as the user may forget to hit space after a comma.
这篇关于在python中将字符串转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文