gpt4 book ai didi

python - 字典(映射,**kwargs)与字典(可迭代,**kwargs)

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:54 26 4
gpt4 key购买 nike

阅读 Python 文档,有几种创建字典的方法。

dict()
dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)

https://docs.python.org/2/library/stdtypes.html (5.8 映射类型)

我不明白 mappingiterable 之间的区别 - 文档是这样写的:

If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects.

在我看来,mappingiterable 在这里是同一回事……你能帮我理解其中的区别吗?

最佳答案

I don't understand the difference between mapping and iterable

映射是键/值对的集合,允许键访问值 - 它将键“映射”到值。最明显的 mapping 内置类型是 dict

可迭代对象是可以迭代的对象 - 这基本上意味着您可以在 for obj in iterable: 语句中使用它。这包括序列类型(字符串、列表等)以及相当多的其他类型(文件、dbapi 游标、生成器等),还有听写。

关于python - 字典(映射,**kwargs)与字典(可迭代,**kwargs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34043547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com