简体   繁体   English

当我想在 Python 中将字典添加到列表中时,为什么 append() 和 += 会给出不同的结果?

[英]Why does append() and += give different results when I want to add a dictionary into a list in Python?

I want to append a dictionary germany into a list travel_log .我想将germany字典附加到列表travel_log中。

travel_log = [
{
  "country": "France",
  "visits": 12,
  "cities": ["Paris", "Lille", "Dijon"]
}]
germany = {
  "country": "Germany",
  "visits": 5,
  "cities": ["Berlin", "Hamburg", "Stuttgart"]
}

When I use travel_log += germany , the result is当我使用travel_log += germany时,结果是

[{'country': 'France', 'visits': 12, 'cities': ['Paris', 'Lille', 'Dijon']}, 'country', 'visits', 'cities'] . [{'country': 'France', 'visits': 12, 'cities': ['Paris', 'Lille', 'Dijon']}, 'country', 'visits', 'cities']

The values in germany disappeared.德国的价值观消失了。

But when I use travel_log.append(germany) , the result is但是当我使用travel_log.append(germany)时,结果是

[{'country': 'France', 'visits': 12, 'cities': ['Paris', 'Lille', 'Dijon']}, {'country': 'Germany', 'visits': 5, 'cities': ['Berlin', 'Hamburg', 'Stuttgart']}]

This is the correct one.这是正确的。

Why these two results are diffrent?为什么这两个结果不同?

Because += extends the list with another iterable, and iterating over a dict iterates over the keys (try print(list(germany)) ).因为+=使用另一个可迭代扩展列表,并且迭代 dict 迭代键(尝试print(list(germany)) )。

You'd get the same effect with travel_log.extend(germany) .你会得到与travel_log.extend(germany)相同的效果。

.append(x) doesn't iterate over x , it just appends it as-is to the list. .append(x)不会迭代x ,它只是将其按原样附加到列表中。

The two results are different because += and .append are designed to do completely different things.这两个结果是不同的,因为+=.append旨在做完全不同的事情。

.append adds a value to the end of a list. .append将一个值添加到列表的末尾。 For example:例如:

a = [1, 2, 3]
a.append(4)
print(a)
# => [1, 2, 3, 4]

+= adds all the values from another iterable to the end of the list - it does the same as .extend . +=将另一个可迭代的所有值添加到列表的末尾 - 它与.extend相同。 If you tried to do a += 4 in the example above, it would error as 4 is not an iterable.如果您尝试在上面的示例中执行a += 4 ,则会出错,因为4不是可迭代的。

However, you are adding a dictionary, and a dictionary is an iterable - iterating over it will iterate over its keys.但是,您正在添加一个字典,而字典是一个可迭代的 - 迭代它会迭代它的键。 Hence += here does not error, but instead adds all the keys of the dictionary to the list (without erroring).因此+=这里不会出错,而是将字典的所有键添加到列表中(没有出错)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么追加后Pycharm调试器和python运行程序会给出两个不同的结果? - Why does the Pycharm debugger and the python running program give two different results after append? 为什么用php编写的河豚代码在Python中给出不同的结果? - Why does blowfish code written in php give different results in Python? AttributeError: 当我尝试将列表添加为字典 python 中的键的值时,“str”对象没有属性“附加”错误 - AttributeError: 'str' object has no attribute 'append' error when i tried to add list as value for a key in dictionary python 列表上的 Python append() 与 + 运算符,为什么这些会给出不同的结果? - Python append() vs. + operator on lists, why do these give different results? 字典的Python列表将值附加到不同的键 - Python list of dictionary append value to different keys Python - append 列表项到字典中的不同键 - Python - append items of list to different keys in dictionary 我想在嵌套列表 Python 中列出 append - I want to append list in nested list Python 为什么我不能添加 <some list> .append到python集? - Why can't I add <some list>.append to a python set? 该代码没有给出我想要在 python 中使用等号的结果 - the code does not give the results what I want using equal sign in python python追加字典到列表 - python append dictionary to list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM