从排序的字典中获得唯一值

时间:2018-10-16 10:06:21

标签: python python-3.x dictionary

我有一个类似以下的字典:我想要一个具有唯一 kbid 值且具有 distance 值最小的新字典。并且新字典也必须按距离进行排序。

{"0":{"distance":291.21,"kbid":"aa"},
"1":{"distance":296.23,"kbid":"xx"},
"2":{"distance":299.21,"kbid":"xx"},
"3":{"distance":299.23,"kbid":"zz"},
"4":{"distance":309.21,"kbid":"aa"},
"5":{"distance":310.23,"kbid":"yy"},
"6":{"distance":310.23,"kbid":"yy"}}

下面是上面的示例输出。

{"0":{"distance":291.21,"kbid":"aa"},
"1":{"distance":296.23,"kbid":"xx"},
"2":{"distance":299.23,"kbid":"zz"},
"3":{"distance":310.23,"kbid":"yy"}}

代码在python中。

2 个答案:

答案 0 :(得分:1)

这将做您想要的。它首先检查重复的kbid条目,仅存储重复的最小值。然后创建一个有序字典,对距离对“ kbid”,按距离排序。然后将这些值放入具有相对于有序字典中位置的索引的普通字典中。

from collections import OrderedDict

dict = {"0":{"distance":291.21,"kbid":"aa"},
"1":{"distance":296.23,"kbid":"xx"},
"2":{"distance":299.21,"kbid":"xx"},
"3":{"distance":299.23,"kbid":"zz"},
"4":{"distance":309.21,"kbid":"aa"},
"5":{"distance":310.23,"kbid":"yy"},
"6":{"distance":310.23,"kbid":"yy"}}

# Check for duplicates.
kbid_dict = {}
for key, value in dict.items():
    if value.get("kbid") in kbid_dict:
        if value.get("distance") < kbid_dict[value.get("kbid")]:
            kbid_dict[value.get("kbid")] = value.get("distance")
    else:
        kbid_dict[value.get("kbid")] = value.get("distance")

# Create a sorted dictionary based on distance.
sorted_dict = OrderedDict(sorted(kbid_dict.items(), key=lambda t: t[1]))
# Put the values into a new dictionary with the required index.
final_dict = {}

for index, value in enumerate(sorted_dict):
    final_dict[index] = {"distance": sorted_dict[value], "kbid" : value}

如果您需要将索引(即0、1等)作为字符串,只需替换

final_dict[index]

使用

final_dict[str(index)]

在最后一行。

答案 1 :(得分:0)

anysorted一起使用,尽管此列表的特定示例不需要排序,这将适用于未排序的条件

d = {"0":{"distance":291.21,"kbid":"aa"},
"1":{"distance":296.23,"kbid":"xx"},
"2":{"distance":299.21,"kbid":"xx"},
"3":{"distance":299.23,"kbid":"zz"},
"4":{"distance":309.21,"kbid":"aa"},
"5":{"distance":310.23,"kbid":"yy"},
"6":{"distance":310.23,"kbid":"yy"}}


res = {}
d = dict(sorted(d.items(), key=lambda x: x[1]['distance']))

for k, v in d.items():
    if any(v['kbid'] in i.values() for i in res.values()):
        pass
    else:
        res[k] = v
print(res)
# {'0': {'distance': 291.21, 'kbid': 'aa'}, '1': {'distance': 296.23, 'kbid': 'xx'}, '3': {'distance': 299.23, 'kbid': 'zz'}, '5': {'distance': 310.23, 'kbid': 'yy'}}