如何将字符串拆分为字符数组?

Adr*_*ian 423 python split

我试图环顾网络寻找将字符串拆分成字符数组的答案,但我似乎无法找到一个简单的方法

str.split(//)似乎没有像Ruby那样工作.有没有循环的简单方法吗?

use*_*312 817

>>> s = "foobar"
>>> list(s)
['f', 'o', 'o', 'b', 'a', 'r']
Run Code Online (Sandbox Code Playgroud)

你需要清单

  • 我认为这比ruby方法好得多,您可以在C级中自由地甚至更好地在序列类型之间进行转换。 (2认同)

Sen*_*ran 69

你取出字符串并将其传递给list()

s = "mystring"
l = list(s)
print l
Run Code Online (Sandbox Code Playgroud)


Lew*_*win 60

您也可以在没有list()的情况下以这种非常简单的方式执行此操作:

>>> [c for c in "foobar"]
['f', 'o', 'o', 'b', 'a', 'r']
Run Code Online (Sandbox Code Playgroud)

  • 这仅仅是"for",没有太多要解释的.我想你应该阅读[数据结构](https://docs.python.org/2/tutorial/datastructures.html)上的python教程,特别是列表理解. (21认同)
  • 欢迎来到stackoverflow.您是否介意稍微扩展答案以解释它如何解决问题. (4认同)
  • 这只是表示`list(map(lambda c:c,iter("foobar")))`,但更具可读性和意义. (4认同)

小智 38

如果要一次处理String一个字符.你有各种选择.

uhello = u'Hello\u0020World'
Run Code Online (Sandbox Code Playgroud)

使用列表理解:

print([x for x in uhello])
Run Code Online (Sandbox Code Playgroud)

输出:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Run Code Online (Sandbox Code Playgroud)

使用地图:

print(list(map(lambda c2: c2, uhello)))
Run Code Online (Sandbox Code Playgroud)

输出:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Run Code Online (Sandbox Code Playgroud)

调用内置列表功能:

print(list(uhello))
Run Code Online (Sandbox Code Playgroud)

输出:

['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Run Code Online (Sandbox Code Playgroud)

使用for循环:

for c in uhello:
    print(c)
Run Code Online (Sandbox Code Playgroud)

输出:

H
e
l
l
o

W
o
r
l
d
Run Code Online (Sandbox Code Playgroud)

  • 这些方法的性能特征是否存在差异? (2认同)

Ale*_*dov 20

我探索了另外两种方法来完成这项任务.这可能对某人有帮助.

第一个很简单:

In [25]: a = []
In [26]: s = 'foobar'
In [27]: a += s
In [28]: a
Out[28]: ['f', 'o', 'o', 'b', 'a', 'r']
Run Code Online (Sandbox Code Playgroud)

而第二个使用maplambda功能.它可能适用于更复杂的任务:

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']
Run Code Online (Sandbox Code Playgroud)

例如

# 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', '', '']
Run Code Online (Sandbox Code Playgroud)

有关更多方法,请参阅python docs


vau*_*tah 19

任务归结为迭代字符串的字符并将它们收集到列表中.最天真的解决方案看起来像

result = []
for character in string:
    result.append(character)
Run Code Online (Sandbox Code Playgroud)

当然,它可以简化为

result = [character for character in string]
Run Code Online (Sandbox Code Playgroud)

但仍有较短的解决方案可以做同样的事情.

list构造函数可用于将任何可迭代(迭代器,列表,元组,字符串等)转换为列表.

>>> list('abc')
['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

最重要的是它在Python 2和Python 3中的工作原理相同.

此外,从Python 3.5开始(感谢令人敬畏的PEP 448)现在可以通过将其解压缩到空列表文字来从任何可迭代构建列表:

>>> [*'abc']
['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

这更整洁,在某些情况下比list直接调用构造函数更有效.

我建议不要使用map为基础的方法,因为map没有返回Python的list 3.请参阅如何使用过滤器,地图,并减少在Python 3.


dja*_*ude 14

简单:

s = 'My'    
print(list(s))
Run Code Online (Sandbox Code Playgroud)


小智 12

我只需要一组字符:

arr = list(str)
Run Code Online (Sandbox Code Playgroud)

如果你想用特定的str分割str:

# str = "temp//temps" will will be ['temp', 'temps']
arr = str.split("//")
Run Code Online (Sandbox Code Playgroud)


Enr*_*dez 12

打开它们:

word = "Paralelepipedo"
print([*word])
Run Code Online (Sandbox Code Playgroud)


小智 8

split()内置函数只会在一定条件的基础上分离值,但在单个单词中,它不能满足条件.所以,它可以在...的帮助下解决list().它在内部调用Array,它将根据数组存储值.

假设,

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']
Run Code Online (Sandbox Code Playgroud)


小智 5

您也可以extend在列表操作中使用方法。

>>> list1 = []
>>> list1.extend('somestring')
>>> list1
['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
Run Code Online (Sandbox Code Playgroud)


Gar*_*127 5

要拆分字符串s,最简单的方法是将其传递给list(). 所以,

s = 'abc'
s_l = list(s) #  s_l is now ['a', 'b', 'c']
Run Code Online (Sandbox Code Playgroud)

您还可以使用列表推导式,它有效但不如上述简洁:

s_l = [c for c in s]
Run Code Online (Sandbox Code Playgroud)

还有其他方法,但这些应该足够了。稍后,如果你想重新组合它们,一个简单的调用"".join(s_l)将把你的列表作为一个字符串返回到它以前的所有荣耀......