如何按值唯一的字典?
我想在字典中唯一重复的值。看起来像这样:
d = {
"a":1,
"b":2,
"c":2,
"d":3,
"e":4,
"f":5,
"g":1,
"h":2,
"i":2,
"j":1,
"k":1}
这就是我所做的:
# sort and unique the dict values
obj = d.values()
K = []
K = sorted(list(zip(*[(x,K.append(x)) for x in obj if not x in K])[0]
V=[]
for v1 in L:
V.append([k for k, v in obj.iteritems() if v == v1][0])
d_out = dict(zip(K, V))
1。 那么,K、V 的顺序正确吗? 另外,它可能有点复杂,任何人都可以给出一个简单的解决方案来通过它的值来唯一的字典吗?
2. 下面的可以更简单一些吗?
for v1 in L:
V.append([k for k, v in obj.iteritems() if v == v1][0])
这在我的测试中不起作用:
[V.append([k for k, v in obj.iteritems() if v == v1][0]) for v1 in L]
3。 我意识到我可以使用交换键值来实现这一点(通过其值唯一的字典),但我不知道当交换导致与此键冲突时如何选择键:
dict((value, key) for key, value in my_dict.iteritems())
我知道是否再次交换它< /strong> 该值将是唯一的,但是,当发生键冲突时,这只会覆盖该键,而没有机会进行选择。我很困惑为什么这没有出现关键冲突错误?我可以做一些事情来选择丑陋的方式旁边的键稍后覆盖新字典的键吗?
4. 我搜索并发现 python dict 的一些 "None" 值得到了很好的讨论,任何人都可以给我一个示例,它的用途是什么以及它在使用 python dict 时会产生什么影响?
I want to unique duplicate values in a dict. It looks like this:
d = {
"a":1,
"b":2,
"c":2,
"d":3,
"e":4,
"f":5,
"g":1,
"h":2,
"i":2,
"j":1,
"k":1}
Here is what I did:
# sort and unique the dict values
obj = d.values()
K = []
K = sorted(list(zip(*[(x,K.append(x)) for x in obj if not x in K])[0]
V=[]
for v1 in L:
V.append([k for k, v in obj.iteritems() if v == v1][0])
d_out = dict(zip(K, V))
1.
So, will the K,V be in a right order?
Also, it may a bit complex, can anyone give a simple solution to unique a dict by it's values?
2.
Can the following be more simple?
for v1 in L:
V.append([k for k, v in obj.iteritems() if v == v1][0])
This is not working on my testing:
[V.append([k for k, v in obj.iteritems() if v == v1][0]) for v1 in L]
3.
I realized I may use the swap key value to achieve that (unique a dict by its value), but I have no idea how to select the key when swap caused a key conflict with this:
dict((value, key) for key, value in my_dict.iteritems())
I know if swap it again the value will be unique, however, this just overwrites the key when a key conflict happens, giving no chance to make a selection. I feel confused why this gives no key conflict error? And can I do something to select the key beside the ugly way overwrite the new dict's key later?
4.
I searched and find some "None" values for python dict are well discussed, anyone can give me a sample what it is used for and what it will impacted in using python dict?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字典不是序列。没有顺序。
您需要一个更简单的总体方法。
字典不会给出“键冲突错误”。它假设您想用新值覆盖旧值。
我不明白你在这里问什么。
下面的解决方案是从字典中删除重复值的更直接的方法。调整排序或插入循环以控制哪些键应出现在最终字典中。
A dict is not a sequence. There is no ordering.
You need a simpler overall approach.
A dict does not give a "key conflict error". It assumes that you want to overwrite the old value with the new value.
I don't understand what you're asking here.
The solution below is a more straightforward way of removing dupe values from a dictionary. Adjust either the sort or the insertion loop to control which keys should appear in the final dict.
注意:字典没有重复的键,因此输入字典没有重复的键
希望它会起作用
N.B: dictionary don't have duplicate key so input dictionary has no duplicate key
hopefully it will works
也许这有一些帮助:
Maybe this is of some help: