[英]How to convert list which contains a single string with multiple entries inside of string. Python 3
假设您有一个列表,其中包含一个字符串,如下所示:
listExample = ['cat, dog, mouse, elephant']
因此,打印此列表将值作为单个字符串返回:
>>>'cat, dog, mouse, elephant'
如何使此字符串达到可以将条目作为多个字符串获取的地步,例如:
>>>print(anotherList)
>>>['cat', 'dog', 'mouse', 'elephant']
您正在描述str.split
的基本用法:
>>> listExample = ['cat, dog, mouse, elephant']
>>> listExample[0].split(', ')
['cat', 'dog', 'mouse', 'elephant']
您应该使用split方法。 也是
'cat, dog, mouse, elephant'.split(', ')
这会给你
['cat', 'dog', 'mouse', 'elephant']
', '
表示字符串应在每次出现逗号和空格后分开。
该列表不是必需的,但是如果您绝对必须使用该列表,请执行
listExample[0].split(', ')
从列表中取出字符串,然后将其拆分。
欲了解更多信息,请参阅本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.