这个问题在这里已经有了答案:
如何将字符串拆分为字符数组? 10个答案我是Python的新手,我试图将每个单独的字符串放入数组的单个元素中。
string= 'Hello' array= [] length_of_string= len(string)-1 for i in range (length_of_string): array.append(string(i)) print(array)但是,当我运行此代码时发生错误,如下所示。
array.append(string(i)) TypeError: 'str' object is not callable当我正常使用字符串或数字追加到数组时, append函数工作正常,但在这种情况下它不起作用。
我需要做些什么来获得
['H','e','l','l','o']This question already has an answer here:
How to split a string into array of characters? 13 answersI'm new to Python and I'm attempting to place each individual character of a string into an individual element of an array.
string= 'Hello' array= [] length_of_string= len(string)-1 for i in range (length_of_string): array.append(string(i)) print(array)However when I run this code an error occurs as shown below.
array.append(string(i)) TypeError: 'str' object is not callableThe append function works fine when I append to an array using strings or numbers normally but in this instance it does not work.
What do I have to do to get
['H','e','l','l','o']最满意答案
你的意思是string[i]如果你不想要string[i]第i个元素(不是string(i) - python不是matlab)。 但是,这样做要快得多
list(string) # ['H','e','l','l','o']You mean string[i] if you wan't the ith element of string (not string(i) -- python isn't matlab). However, it's much faster just to do
list(string) # ['H','e','l','l','o']更多推荐
发布评论