2017-01-16 48 views
-4

I tried using all the methods suggested by others but its not working. methods like str.split(), lst = list("abcd") but its throwing error saying [TypeError: 'list' object is not callable]分割每個字符的Python 3.5

I want to convert string to list for each character in the word input str= "abc" should give list = ['a','b','c']

我想要得到的str的字符以列表形式 輸出的字 - [ '一', 'B', 'C', 'd', 'E', 'F'],但其給出[ 'ABCDEF']

str = "abcdef" 
l = str.split() 
print l 
+3

'列表( 「ABCDEF」)的' – MYGz

+6

可能的複製(HTTP: //stackoverflow.com/questions/4978787/how-to-split-a-string-into-array-of-characters-with-python) –

+0

@MYGz:類型錯誤:名單'對象不是可調用 –

回答

2

首先,不要使用list作爲變量名。它會阻止你做你想做的事,因爲它會影響list類的名字。

您可以通過簡單地從字符串構建一個列表做到這一點:

l = list('abcedf') 

l到列表['a', 'b', 'c', 'e', 'd', 'f']

+0

嗯,我想LST =名單(「ABCDEF」),它拋出一個錯誤類型錯誤:「名單」對象不是可調用 –

+1

我想補充,你不應該使用' str'作爲變量名稱。 – Fejs

+0

@AbhishekPriyankar刪除此行'列表= str.split()'。 – Fejs

0

首先,不使用列表作爲變量的名字在你的程序中。它是python中定義的關鍵字,這不是一個好習慣。

如果你有,

str = 'a b c d e f g' 

然後,

list = str.split() 
print list 
>>>['a', 'b', 'c', 'd', 'e', 'f', 'g'] 

由於拆分默認情況下將在空間工作,它會給你所需要的。

在你的情況,你可以用,

print list(s) 
>>>['a', 'b', 'c', 'd', 'e', 'f', 'g'] 
0

問:「我想將字符串轉換爲列出的單詞每個字符」

答:您可以使用一個簡單的list comprehension

輸入:

new_str = "abcdef" 

[character for character in new_str] 

輸出:[?如何將字符串分割成與Python字符數組]

['a', 'b', 'c', 'd', 'e', 'f']