如何将字符串分割为字符列表?Str.split不起作用。
"foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
如何将字符串分割为字符列表?Str.split不起作用。
"foobar" → ['f', 'o', 'o', 'b', 'a', 'r']
当前回答
下面是一个很好的脚本,可以帮助你找到最有效的方法:
import timeit
from itertools import chain
string = "thisisthestringthatwewanttosplitintoalist"
def getCharList(str):
return list(str)
def getCharListComp(str):
return [char for char in str]
def getCharListMap(str):
return list(map(lambda c: c, str))
def getCharListForLoop(str):
list = []
for c in str:
list.append(c)
def getCharListUnpack(str):
return [*str]
def getCharListExtend(str):
list = []
return list.extend(str)
def getCharListChain(str):
return chain(str)
time_list = timeit.timeit(stmt='getCharList(string)', globals=globals(), number=1)
time_listcomp = timeit.timeit(stmt='getCharListComp(string)', globals=globals(), number=1)
time_listmap = timeit.timeit(stmt='getCharListMap(string)', globals=globals(), number=1)
time_listforloop = timeit.timeit(stmt='getCharListForLoop(string)', globals=globals(), number=1)
time_listunpack = timeit.timeit(stmt='getCharListUnpack(string)', globals=globals(), number=1)
time_listextend = timeit.timeit(stmt='getCharListExtend(string)', globals=globals(), number=1)
time_listchain = timeit.timeit(stmt='getCharListChain(string)', globals=globals(), number=1)
print(f"Execution time using list constructor is {time_list} seconds")
print(f"Execution time using list comprehension is {time_listcomp} seconds")
print(f"Execution time using map is {time_listmap} seconds")
print(f"Execution time using for loop is {time_listforloop} seconds")
print(f"Execution time using unpacking is {time_listunpack} seconds")
print(f"Execution time using extend is {time_listextend} seconds")
print(f"Execution time using chain is {time_listchain} seconds")
其他回答
我探索了另外两种方法来完成这项任务。它可能对某人有帮助。
第一个很简单:
In [25]: a = []
In [26]: s = 'foobar'
In [27]: a += s
In [28]: a
Out[28]: ['f', 'o', 'o', 'b', 'a', 'r']
第二个使用map和函数。它可能适用于更复杂的任务:
In [36]: s = 'foobar12'
In [37]: a = map(lambda c: c, s)
In [38]: a
Out[38]: ['f', 'o', 'o', 'b', 'a', 'r', '1', '2']
例如
# isdigit, isspace or another facilities such as regexp may be used
In [40]: a = map(lambda c: c if c.isalpha() else '', s)
In [41]: a
Out[41]: ['f', 'o', 'o', 'b', 'a', 'r', '', '']
有关更多方法,请参阅python文档
下面是一个很好的脚本,可以帮助你找到最有效的方法:
import timeit
from itertools import chain
string = "thisisthestringthatwewanttosplitintoalist"
def getCharList(str):
return list(str)
def getCharListComp(str):
return [char for char in str]
def getCharListMap(str):
return list(map(lambda c: c, str))
def getCharListForLoop(str):
list = []
for c in str:
list.append(c)
def getCharListUnpack(str):
return [*str]
def getCharListExtend(str):
list = []
return list.extend(str)
def getCharListChain(str):
return chain(str)
time_list = timeit.timeit(stmt='getCharList(string)', globals=globals(), number=1)
time_listcomp = timeit.timeit(stmt='getCharListComp(string)', globals=globals(), number=1)
time_listmap = timeit.timeit(stmt='getCharListMap(string)', globals=globals(), number=1)
time_listforloop = timeit.timeit(stmt='getCharListForLoop(string)', globals=globals(), number=1)
time_listunpack = timeit.timeit(stmt='getCharListUnpack(string)', globals=globals(), number=1)
time_listextend = timeit.timeit(stmt='getCharListExtend(string)', globals=globals(), number=1)
time_listchain = timeit.timeit(stmt='getCharListChain(string)', globals=globals(), number=1)
print(f"Execution time using list constructor is {time_list} seconds")
print(f"Execution time using list comprehension is {time_listcomp} seconds")
print(f"Execution time using map is {time_listmap} seconds")
print(f"Execution time using for loop is {time_listforloop} seconds")
print(f"Execution time using unpacking is {time_listunpack} seconds")
print(f"Execution time using extend is {time_listextend} seconds")
print(f"Execution time using chain is {time_listchain} seconds")
你也可以用这种非常简单的方式来做,没有list():
>>> [c for c in "foobar"]
['f', 'o', 'o', 'b', 'a', 'r']
您也可以在列表操作中使用extend方法。
>>> list1 = []
>>> list1.extend('somestring')
>>> list1
['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
Split()内置函数只会在特定条件的基础上分离值,但在单个词中,它不能满足条件。因此,可以借助list()来解决。它在内部调用数组,并根据数组存储值。
假设,
a = "bottle"
a.split() // will only return the word but not split the every single char.
a = "bottle"
list(a) // will separate ['b','o','t','t','l','e']