[英]what is the most efficient way to turn a <class 'str'> into a list in python?
I believe <class 'str'>
is a type of string object.我相信<class 'str'>
是一种字符串对象。 I wanted to transform a <class 'str'>
into a list
of strings.我想将<class 'str'>
转换为字符串list
。 As an example, when I printed out a <class 'str'>
called keywordList
I get:例如,当我打印出一个名为keywordList
的<class 'str'>
keywordList
我得到:
cat
mouse
hat
I want to turn it into: [cat, mouse, hat]
我想把它变成: [cat, mouse, hat]
I did get it to work by doing this:我确实通过这样做让它工作了:
strList = keywordList.strip('][').split(', ')
strList3 = strList[0].splitlines()
But is there an easier way to transform <class 'str'>
into a list of string...但是有没有更简单的方法将<class 'str'>
转换为字符串列表...
Would this solve your problem?这能解决你的问题吗? As far as I understand, you want to convert a string of words into a list of words.据我了解,您想将一串单词转换为单词列表。
keywordList = "cat mouse hat" #your str object
strList = [x for x in keywordList.split(' ')]#List comprehension, splitting the words at a space ' '
print(strList)
Output:输出:
['cat', 'mouse', 'dog']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.