Python 加入字典项,键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23997240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Join dictionary item, keys
提问by TechnoBee
Please see below code snippet for join method (used Python 2.7.2):
请参阅以下连接方法的代码片段(使用 Python 2.7.2):
iDict={'1_key':'abcd','2_key':'ABCD','3_key':'bcde','4_key':'BCDE'}
'--'.join(iDict)
Result shown as
结果显示为
'2_key--1_key--4_key--3_key'
Please comment why only keys are joined? Also the sequence is not in order.
请评论为什么只有键被加入?而且顺序也不是很好。
Note - below are the individual methods.
注意 - 以下是个别方法。
'--'.join(iDict.values())
==>'ABCD--abcd--BCDE--bcde'
==> the sequence is not in order'--'.join(iDict.keys())
==>'2_key--1_key--4_key--3_key'
==> the sequence is not in orde
'--'.join(iDict.values())
==>'ABCD--abcd--BCDE--bcde'
==> 顺序不对'--'.join(iDict.keys())
==>'2_key--1_key--4_key--3_key'
==> 顺序不对
采纳答案by Henry Keiter
Python dictionaries are unordered (or rather, their order is arbitrary), and when you iterate on them, only the keys are returned:
Python 字典是无序的(或者更确切地说,它们的顺序是任意的),当您对它们进行迭代时,只会返回键:
>>> d = {'0':0, '1':1, '2':2, '3':3, '4':4}
>>> print(d)
{'4': 4, '1': 1, '0': 0, '3': 3, '2': 2}
If you need both keys and values, use iDict.items()
.
如果您需要键和值,请使用iDict.items()
.
If you need ordering, use collections.OrderedDict
.
如果您需要订购,请使用collections.OrderedDict
.
回答by Martijn Pieters
Iteration over a dictionary only ever yields keys:
迭代字典只会产生键:
>>> list(iDict)
['2_key', '1_key', '4_key', '3_key']
See the dict()
documentation:
请参阅dict()
文档:
iter(d)
Return an iterator over the keys of the dictionary. This is a shortcut foriterkeys()
.
iter(d)
在字典的键上返回一个迭代器。这是 的快捷方式iterkeys()
。
Both list()
and str.join()
will call iter()
on their arguments to iterate over the elements.
双方list()
并str.join()
会打电话给iter()
他们的争论中的元素进行迭代。
Dictionaries are unorderedcontainers; their order stems from the underlying data structure and depends on the insertion and deletion history of the keys.
字典是无序的容器;它们的顺序源于底层数据结构,取决于键的插入和删除历史。
This is documented under dict.items()
:
这记录在dict.items()
:
CPython implementation detail: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary's history of insertions and deletions.
CPython 实现细节:键和值以任意顺序列出,该顺序是非随机的,因 Python 实现而异,并取决于字典的插入和删除历史。
Also see Why is the order in dictionaries and sets arbitrary?
回答by m.wasowski
If you see the docs, you learn that iterating over dict
returns keys.
如果您查看文档,就会了解到迭代dict
返回键。
You need to iterate over dict.items()
, that it over tuples (key, value)
:
你需要迭代dict.items()
,它在元组上(key, value)
:
'--'.join(iDict.items())
If you need to have key AND value joined in one string, you need to explicitly tell Python how to do this:
如果需要将键 AND 值连接到一个字符串中,则需要明确告诉 Python 如何执行此操作:
'--'.join('{} : {}'.format(key, value) for key, value in iDict.items())