[英]How do I split a string into a list of characters?
你拿 string 並將它傳遞給 list()
s = "mystring" l = list(s) print l
你也可以在沒有 list() 的情況下以這種非常簡單的方式做到這一點:
>>> [c for c in "foobar"] ['f', 'o', 'o', 'b', 'a', 'r']
If you want to process your String one character at a time. you have various options.
uhello = u'Hello\u0020World'
Using List comprehension:
print([x for x in uhello])
Output:
['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']
Calling Built in list function:
print(list(uhello))
Output:
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
Using for loop:
for c in uhello:
print(c)
Output:
H
e
l
l
o
W
o
r
l
d
如果您只需要一個字符數組:
arr = list(str)
如果你想用特定的分隔符分割 str:
# str = "temp//temps" will will be ['temp', 'temps'] arr = str.split("//")
我探索了另外兩種方法來完成這項任務。 它可能對某人有幫助。
第一個很簡單:
In [25]: a = [] In [26]: s = 'foobar' In [27]: a += s In [28]: a Out[28]: ['f', 'o', 'o', 'b', 'a', 'r']
第二個使用map
和lambda
function。 它可能適用於更復雜的任務:
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 文檔
該任務歸結為迭代 string 的字符並將它們收集到一個列表中。 最天真的解決方案看起來像
result = [] for character in string: result.append(character)
當然也可以簡寫為
result = [character for character in string]
但仍然有更短的解決方案可以做同樣的事情。
list
構造函數可用於將任何可迭代對象(迭代器、列表、元組、string 等)轉換為列表。
>>> list('abc') ['a', 'b', 'c']
最大的優點是它在 Python 2 和 Python 3 中的工作原理相同。
此外,從 Python 3.5 開始(感謝令人敬畏的PEP 448 ),現在可以通過將任何可迭代對象解包為空列表文字來構建列表:
>>> [*'abc'] ['a', 'b', 'c']
這更簡潔,在某些情況下比直接調用list
構造函數更有效。
I'd advise against using map
approaches, because map
does not return a list in Python 3. See How to use filter, map, and reduce in Python 3 .
split()
內置 function 只會根據某些條件將值分開,但在單個單詞中,它不能滿足條件。 因此,它可以在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']
簡單:
s = 'My'
print(list(s))
打開包裝:
word = "Paralelepipedo" print([*word])
To split a string s
, the easiest way is to pass it to list()
. So,
s = 'abc'
s_l = list(s) # s_l is now ['a', 'b', 'c']
You can also use a list comprehension, which works but is not as concise as the above:
s_l = [c for c in s]
There are other ways, as well, but these should suffice. Later, if you want to recombine them, a simple call to "".join(s_l)
will return your list to all its former glory as a string...
您也可以在列表操作中使用extend
方法。
>>> list1 = [] >>> list1.extend('somestring') >>> list1 ['s', 'o', 'm', 'e', 's', 't', 'r', 'i', 'n', 'g']
如果您希望對 string 進行只讀訪問,您可以直接使用數組表示法。
Python 2.7.6 (default, Mar 22 2014, 22:59:38) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> t = 'my string' >>> t[1] 'y'
在不使用正則表達式的情況下可能對測試很有用。 string 是否包含結束換行符?
>>> t[-1] == '\n' False >>> t = 'my string\n' >>> t[-1] == '\n' True
from itertools import chain string = 'your string' chain(string)
類似於list(string)
但返回一個在使用點延遲評估的生成器,因此 memory 高效。
好吧,盡管我很喜歡列表版本,但這是我發現的另一種更冗長的方式(但它很酷,所以我想我會把它添加到戰斗中):
>>> 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']
>>> for i in range(len(a)):
... print a[i]
...
其中a是您要分離的字符串。 值“ a [i]”是字符串的單個字符,可以將它們附加到列表中。
string="footbar"
print([*string])
逐字拆分字符串是一種簡單的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.