2017-01-16 49 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']