I've tried to look around the web for answers to splitting a string into an array of characters but I can't seem to find a simple method
我试着在网上寻找将一个字符串分割成一系列字符的答案,但我似乎找不到一个简单的方法
str.split(//)
does not seem to work like Ruby does. Is there a simple way of doing this without looping?
split(//)似乎不像Ruby那样工作。有没有一种简单的方法可以不使用循环?
10 个解决方案
#2
60
You take the string and pass it to list()
将字符串传递给list()
s = "mystring"l = list(s)print l
#3
50
You can also do it in this very simple way without list():
您也可以使用这个非常简单的方法,而不需要list():
>>> [c for c in "foobar"]['f', 'o', 'o', 'b', 'a', 'r']
#4
17
I explored another two ways to accomplish this task. It may be helpful for someone.
我探索了另外两种方法来完成这项任务。它可能对某人有帮助。
The first one is easy:
第一个很简单:
In [25]: a = []In [26]: s = 'foobar'In [27]: a += sIn [28]: aOut[28]: ['f', 'o', 'o', 'b', 'a', 'r']
And the second one use map
and lambda
function. It may be appropriate for more complex tasks:
第二个用映射和函数。它可能适用于更复杂的任务:
In [36]: s = 'foobar12'In [37]: a = map(lambda c: c, s)In [38]: aOut[38]: ['f', 'o', 'o', 'b', 'a', 'r', '1', '2']
For example
例如
# isdigit, isspace or another facilities such as regexp may be usedIn [40]: a = map(lambda c: c if c.isalpha() else '', s)In [41]: aOut[41]: ['f', 'o', 'o', 'b', 'a', 'r', '', '']
See python docs for more methods
有关更多方法,请参阅python文档
#5
16
The task boils down to iterating over characters of the string and collecting them into a list. The most naïve solution would look like
该任务可归结为对字符串的字符进行迭代并将它们收集到一个列表中。最天真的解决方案应该是这样的
result = []for character in string: result.append(character)
Of course, it can be shortened to just
当然,它也可以简化为just
result = [character for character in string]
but there still are shorter solutions that do the same thing.
但是仍然有更短的解可以做同样的事情。
list
constructor can be used to convert any iterable (iterators, lists, tuples, string etc.) to list.
list构造函数可用于将任何可迭代的(迭代器、列表、元组、字符串等)转换为list。
>>> list('abc')['a', 'b', 'c']
The big plus is that it works the same in both Python 2 and Python 3.
最大的优点是它在Python 2和Python 3中都是一样的。
Also, starting from Python 3.5 (thanks to the awesome PEP 448) it's now possible to build a list from any iterable by unpacking it to an empty list literal:
同样,从Python 3.5开始(多亏了令人敬畏的PEP 448),现在可以从任何可迭代的列表中构建一个列表,方法是将其解压为一个空列表:
>>> [*'abc']['a', 'b', 'c']
This is neater, and in some cases more efficient than calling list
constructor directly.
这是更整洁的,在某些情况下比直接调用list构造函数更有效。
I'd advise against using map
-based approaches, because map
does not return a list in Python 3. See How to use filter, map, and reduce in Python 3.
我建议不要使用基于地图的方法,因为map不会返回Python 3中的列表。请参见如何在Python 3中使用筛选、映射和减少。
#6
10
If you want to process your String one character at a time. you have various options.uhello = u'Hello\u0020World'
如果您想一次处理一个字符串。你有很多不同的选择。uhello = u 'Hello \ u0020World '
Using List comprehension:print([x for x in uhello])Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
使用列表理解:打印([x x uhello])输出:[' H ',' e ',' l ',' l ',' o ',' ',' W ',' o ',' r ',' l ',' d ')
using map:print(list(map(lambda c2: c2, uhello)))Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
使用地图:打印(列表(地图(λc2:c2,uhello)))输出:[‘H’,‘e’,‘l’,‘l’,‘o’,‘,‘W’,‘o’,‘r’,‘l’,' d ')
Calling Built in list functionprint(list(uhello))Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
调用建于functionprint列表(列表(uhello))输出:[' H ',' e ',' l ',' l ',' o ',' ',' W ',' o ',' r ',' l ',' d ')
Using for loop:for c in uhello: print(c)
使用for循环:for c in uhello: print(c)
#7
7
simple:
简单:
s = 'My' print(list(s))
#8
3
If you wish to read only access to the string you can use array notation directly.
如果希望只读取对字符串的访问,可以直接使用数组表示法。
Python 2.7.6 (default, Mar 22 2014, 22:59:38) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> t = 'my string'>>> t[1]'y'
Could be useful for testing without using regexp.Does the string contain an ending newline?
可以在不使用regexp的情况下进行测试。这个字符串包含结束换行吗?
>>> t[-1] == '\n'False>>> t = 'my string\n'>>> t[-1] == '\n'True
#9
1
Well, much as I like the list(s) version, here's another more verbose way I found (but it's cool so I thought I'd add it to the fray):
嗯,就像我喜欢这个列表一样,这里还有一个更详细的方法(但它很酷,所以我想把它加入到争论中):
>>> text = "My hovercraft is full of eels">>> [text[i] for i in range(len(text))]['M', 'y', ' ', 'h', 'o', 'v', 'e', 'r', 'c', 'r', 'a', 'f', 't', ' ', 'i', 's', ' ', 'f', 'u', 'l', 'l', ' ', 'o', 'f', ' ', 'e', 'e', 'l', 's']
#10
1
>>> for i in range(len(a)):... print a[i]...
where a is the string that you want to separate out. The values "a[i]" are the individual character of the the string these could be appended to a list.
a是要分离的字符串。值“a[i]”是可以附加到列表中的字符串的单个字符。
#1
#2
60
You take the string and pass it to list()
将字符串传递给list()
s = "mystring"l = list(s)print l
#3
50
You can also do it in this very simple way without list():
您也可以使用这个非常简单的方法,而不需要list():
>>> [c for c in "foobar"]['f', 'o', 'o', 'b', 'a', 'r']
#4
17
I explored another two ways to accomplish this task. It may be helpful for someone.
我探索了另外两种方法来完成这项任务。它可能对某人有帮助。
The first one is easy:
第一个很简单:
In [25]: a = []In [26]: s = 'foobar'In [27]: a += sIn [28]: aOut[28]: ['f', 'o', 'o', 'b', 'a', 'r']
And the second one use map
and lambda
function. It may be appropriate for more complex tasks:
第二个用映射和函数。它可能适用于更复杂的任务:
In [36]: s = 'foobar12'In [37]: a = map(lambda c: c, s)In [38]: aOut[38]: ['f', 'o', 'o', 'b', 'a', 'r', '1', '2']
For example
例如
# isdigit, isspace or another facilities such as regexp may be usedIn [40]: a = map(lambda c: c if c.isalpha() else '', s)In [41]: aOut[41]: ['f', 'o', 'o', 'b', 'a', 'r', '', '']
See python docs for more methods
有关更多方法,请参阅python文档
#5
16
The task boils down to iterating over characters of the string and collecting them into a list. The most naïve solution would look like
该任务可归结为对字符串的字符进行迭代并将它们收集到一个列表中。最天真的解决方案应该是这样的
result = []for character in string: result.append(character)
Of course, it can be shortened to just
当然,它也可以简化为just
result = [character for character in string]
but there still are shorter solutions that do the same thing.
但是仍然有更短的解可以做同样的事情。
list
constructor can be used to convert any iterable (iterators, lists, tuples, string etc.) to list.
list构造函数可用于将任何可迭代的(迭代器、列表、元组、字符串等)转换为list。
>>> list('abc')['a', 'b', 'c']
The big plus is that it works the same in both Python 2 and Python 3.
最大的优点是它在Python 2和Python 3中都是一样的。
Also, starting from Python 3.5 (thanks to the awesome PEP 448) it's now possible to build a list from any iterable by unpacking it to an empty list literal:
同样,从Python 3.5开始(多亏了令人敬畏的PEP 448),现在可以从任何可迭代的列表中构建一个列表,方法是将其解压为一个空列表:
>>> [*'abc']['a', 'b', 'c']
This is neater, and in some cases more efficient than calling list
constructor directly.
这是更整洁的,在某些情况下比直接调用list构造函数更有效。
I'd advise against using map
-based approaches, because map
does not return a list in Python 3. See How to use filter, map, and reduce in Python 3.
我建议不要使用基于地图的方法,因为map不会返回Python 3中的列表。请参见如何在Python 3中使用筛选、映射和减少。
#6
10
If you want to process your String one character at a time. you have various options.uhello = u'Hello\u0020World'
如果您想一次处理一个字符串。你有很多不同的选择。uhello = u 'Hello \ u0020World '
Using List comprehension:print([x for x in uhello])Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
使用列表理解:打印([x x uhello])输出:[' H ',' e ',' l ',' l ',' o ',' ',' W ',' o ',' r ',' l ',' d ')
using map:print(list(map(lambda c2: c2, uhello)))Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
使用地图:打印(列表(地图(λc2:c2,uhello)))输出:[‘H’,‘e’,‘l’,‘l’,‘o’,‘,‘W’,‘o’,‘r’,‘l’,' d ')
Calling Built in list functionprint(list(uhello))Output:['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
调用建于functionprint列表(列表(uhello))输出:[' H ',' e ',' l ',' l ',' o ',' ',' W ',' o ',' r ',' l ',' d ')
Using for loop:for c in uhello: print(c)
使用for循环:for c in uhello: print(c)
#7
7
simple:
简单:
s = 'My' print(list(s))
#8
3
If you wish to read only access to the string you can use array notation directly.
如果希望只读取对字符串的访问,可以直接使用数组表示法。
Python 2.7.6 (default, Mar 22 2014, 22:59:38) [GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> t = 'my string'>>> t[1]'y'
Could be useful for testing without using regexp.Does the string contain an ending newline?
可以在不使用regexp的情况下进行测试。这个字符串包含结束换行吗?
>>> t[-1] == '\n'False>>> t = 'my string\n'>>> t[-1] == '\n'True
#9
1
Well, much as I like the list(s) version, here's another more verbose way I found (but it's cool so I thought I'd add it to the fray):
嗯,就像我喜欢这个列表一样,这里还有一个更详细的方法(但它很酷,所以我想把它加入到争论中):
>>> text = "My hovercraft is full of eels">>> [text[i] for i in range(len(text))]['M', 'y', ' ', 'h', 'o', 'v', 'e', 'r', 'c', 'r', 'a', 'f', 't', ' ', 'i', 's', ' ', 'f', 'u', 'l', 'l', ' ', 'o', 'f', ' ', 'e', 'e', 'l', 's']
#10
1
>>> for i in range(len(a)):... print a[i]...
where a is the string that you want to separate out. The values "a[i]" are the individual character of the the string these could be appended to a list.
a是要分离的字符串。值“a[i]”是可以附加到列表中的字符串的单个字符。