【发布时间】:2019-06-21 02:34:06
【问题描述】:
我的字符串为:
myString = 'example'
如何将其转换为列表:
lst = ['example']
以一种有效的方式?
【问题讨论】:
-
[s]或s.split()s='example'。虽然s.split()慢得多。
标签: python python-3.x
我的字符串为:
myString = 'example'
如何将其转换为列表:
lst = ['example']
以一种有效的方式?
【问题讨论】:
[s] 或 s.split() s='example'。虽然s.split() 慢得多。
标签: python python-3.x
最自然的方式是正确的:
mystr = 'example'
lst = [mystr]
另外,不要将变量命名为str;它覆盖了内置的str。
【讨论】:
您可以使用 append() 函数来实现:
lst = []
str = 'example'
lst.append(str)
【讨论】:
str='example'
l=list(str)
现在 l 将包含 ['e', 'x', 'a', 'm', 'p', 'l', 'e']
【讨论】: