Python:将字符串的单个字符插入数组[复制](Python: Inserting individual characters of a string into an array [duplicat

编程入门 行业动态 更新时间:2024-10-24 03:20:45
Python:将字符串的单个字符插入数组[复制](Python: Inserting individual characters of a string into an array [duplicate])

这个问题在这里已经有了答案:

如何将字符串拆分为字符数组? 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 answers

I'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 callable

The 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']

更多推荐

本文发布于:2023-08-04 23:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   字符   Inserting   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!