【问题标题】:why python list behave like this ? why not it doesn't add "de" to the list为什么 python 列表的行为是这样的?为什么不将“de”添加到列表中
【发布时间】:2021-03-19 16:47:21
【问题描述】:

请解释为什么这段代码会有这样的行为。

x=["a","b","c"]
x+="de"
print (x)

output := ['a', 'b', 'c', 'd', 'e']

为什么会像上面的输出?为什么它不像下面的行那样输出?我知道我们可以将“de”附加到这样的输出中。“+”运算符如何在列表中工作?

output := ['a', 'b', 'c', 'de']

【问题讨论】:

标签: python python-3.x list operators


【解决方案1】:

除了@khelwood 的反应:如果你想用+= 添加'de',你可以这样做:

x = ["a","b","c"]
x += ["de"]
print(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多