为什么 .append() 方法不适用于字符串,它们的行为不像列表? [英] Why doesn't .append() method work on strings, don't they behave like lists?

查看:48
本文介绍了为什么 .append() 方法不适用于字符串,它们的行为不像列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使字符串实际上是字符常量列表,为什么这个语句会产生错误?

Why does this statement produce an error even though a string is actually a list of character constants?

string_name = ""  
string_name.append("hello word")  

我希望它起作用的原因是因为当我们使用 for 循环时,我们可以使用这个语句:

The reason I expect this to work is because when we use for-loop, we are allowed to use this statement:

for i in string_name:  
    ...

我认为 string_name 在这里被认为是一个列表(?)

I think string_name is considered as a list here(?)

推荐答案

这就是他们在算法和数据结构课程中教你的,这些课程涉及算法语言(虚幻的)而不是真正的编程语言,在 Python 中,字符串是一个字符串,一个列表就是一个列表,它们是不同的对象,您可以使用所谓的字符串连接(这基本上是对字符串的加法运算)附加"到一个字符串:

That's what they teach you in an algorithms and data structures class, that deal with algorithmic languages (unreal) rather than real programming languages, in Python, a string is a string, and a list is a list, they're different objects, you can "append" to a string using what is called string concatenation (which is basically an addition operation on strings):

string_name = "hello"  
string_name = string_name + " world"
print(string_name) # => "hello world"

或速记串联:

string_name = "hello"
string_name += " world"
print(string_name) # => "hello world"

列表和字符串属于这种称为 iterable 的类型.iterables 顾名思义,iterables,意思是你可以用关键字 in 遍历它们,但这并不意味着它们是相同的类型对象:

Lists and strings belong to this type called iterable. iterables are as they're name suggests, iterables, meaning you can iterate through them with the key word in, but that doesn't mean they're the same type of objects:

for i in '123': # valid, using a string
for i in [1, 2, 3]: # valid, using a list
for i in (1, 2, 3): # valid, using a tuple
for i in 1, 2, 3: # valid, using an implicit-tuple
# all valid, all different types

我强烈建议您阅读 Python 文档 和/或采用 Python 教程.

I strongly recommend that you read the Python Documentation and/or take the Python's Tutorial.

来自文档词汇表:

可迭代
能够一次返回一个成员的对象.可迭代对象的示例包括所有序列类型(例如 liststrtuple) 和一些非序列类型,例如 dict、文件对象以及您使用 __iter__() 定义的任何类的对象__getitem__() 方法.可迭代对象可用于 for 循环和许多其他需要序列的地方(zip()map()...).当可迭代对象作为参数传递给内置函数 iter() 时,它会返回该对象的迭代器.此迭代器适用于对一组值进行一次传递.使用可迭代对象时,通常不需要调用iter() 或自己处理迭代器对象.for 语句会自动为您执行此操作,创建一个临时未命名变量以在循环期间保存迭代器.另请参见迭代器、序列和生成器.详细了解迭代.

iterable
An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator. More about iterables.

这篇关于为什么 .append() 方法不适用于字符串,它们的行为不像列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆