[英]How to convert list which contains a single string with multiple entries inside of string. Python 3
Let's say you have a list, which contains a single string as: 假设您有一个列表,其中包含一个字符串,如下所示:
listExample = ['cat, dog, mouse, elephant']
So printing this list return the values as a single string: 因此,打印此列表将值作为单个字符串返回:
>>>'cat, dog, mouse, elephant'
How do you get this string to the point where you can get the entries as multiple strings, such as: 如何使此字符串达到可以将条目作为多个字符串获取的地步,例如:
>>>print(anotherList)
>>>['cat', 'dog', 'mouse', 'elephant']
You should use the split method. 您应该使用split方法。 So do 也是
'cat, dog, mouse, elephant'.split(', ')
this will give you 这会给你
['cat', 'dog', 'mouse', 'elephant']
The ', '
means the string should be split every time there is a comma and then a space. ', '
表示字符串应在每次出现逗号和空格后分开。
The list is not needed, but if you absolutely have to use the list then do 该列表不是必需的,但是如果您绝对必须使用该列表,请执行
listExample[0].split(', ')
to get the string out of the list and then split it. 从列表中取出字符串,然后将其拆分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.