如何按值唯一的字典?

发布于 2024-09-09 19:45:15 字数 1139 浏览 5 评论 0原文

我想在字典中唯一重复的值。看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱果果 2024-09-16 19:45:15
  1. 字典不是序列。没有顺序。

  2. 您需要一个更简单的总体方法。

  3. 字典不会给出“键冲突错误”。它假设您想用新值覆盖旧值。

  4. 我不明白你在这里问什么。

下面的解决方案是从字典中删除重复值的更直接的方法。调整排序或插入循环以控制哪些键应出现在最终字典中。

d = {
    "a":1,
    "b":2,
    "c":2,
    "d":3,
    "e":4,
    "f":5,
    "g":1,
    "h":2,
    "i":2,
    "j":1,
    "k":1}

# Extract the dictionary into a list of (key, value) tuples.
t =  [(k, d[k]) for k in d]

# Sort the list -- by default it will sort by the key since it is
# first in the tuple.
t.sort()

# Reset the dictionary so it is ready to hold the new dataset.
d = {}

# Load key-values into the dictionary. Only the first value will be
# stored.
for k, v in t:
    if v in d.values():
        continue
    d[k] = v

print d
  1. A dict is not a sequence. There is no ordering.

  2. You need a simpler overall approach.

  3. A dict does not give a "key conflict error". It assumes that you want to overwrite the old value with the new value.

  4. 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.

d = {
    "a":1,
    "b":2,
    "c":2,
    "d":3,
    "e":4,
    "f":5,
    "g":1,
    "h":2,
    "i":2,
    "j":1,
    "k":1}

# Extract the dictionary into a list of (key, value) tuples.
t =  [(k, d[k]) for k in d]

# Sort the list -- by default it will sort by the key since it is
# first in the tuple.
t.sort()

# Reset the dictionary so it is ready to hold the new dataset.
d = {}

# Load key-values into the dictionary. Only the first value will be
# stored.
for k, v in t:
    if v in d.values():
        continue
    d[k] = v

print d
牵你的手,一向走下去 2024-09-16 19:45:15
try it out:

from collections import defaultdict
dout = defaultdict(dict)
for k,v in d.iteritems():
    dout[v] = k 
dout = dict(dout)
fdict = dict(zip(dout.values(), dout.keys()))

注意:字典没有重复的键,因此输入字典没有重复的键
希望它会起作用

try it out:

from collections import defaultdict
dout = defaultdict(dict)
for k,v in d.iteritems():
    dout[v] = k 
dout = dict(dout)
fdict = dict(zip(dout.values(), dout.keys()))

N.B: dictionary don't have duplicate key so input dictionary has no duplicate key
hopefully it will works

乜一 2024-09-16 19:45:15

也许这有一些帮助:

import collections

d = ... # like above
d1 = collections.defaultdict(list)

for k, v in d.iteritems():
    d1[v].append(k)

print d1

Maybe this is of some help:

import collections

d = ... # like above
d1 = collections.defaultdict(list)

for k, v in d.iteritems():
    d1[v].append(k)

print d1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文