-1

我有一个字典列表,其中包含一对带有坐标的地方。每对都必须是唯一的。例如origin-London和被认为与和destination-Oslo相同。考虑到它们具有不同的键,我如何删除这些重复项。origin-Oslodestination-London

[
    {
        "origin": "North pole",
        "destination": "London",
        "origin_lat": "90",
        "origin_log": "0",
        "destination_lat": "51.50853",
        "destination_log": "-0.12574"
    },
    {
        "origin": "North pole",
        "destination": "Oslo",
        "origin_lat": "90",
        "origin_log": "0",
        "destination_lat": "59.91273",
        "destination_log": "10.74609"
    },
    {
        "origin": "London",
        "destination": "Oslo",
        "origin_lat": "51.50853",
        "origin_log": "-0.12574",
        "destination_lat": "59.91273",
        "destination_log": "10.74609"
    },
    {
        "origin": "Oslo",
        "destination": "London",
        "origin_lat": "59.91273",
        "origin_log": "10.74609",
        "destination_lat": "51.50853",
        "destination_log": "-0.12574"
    }
]
4

1 回答 1

0
list_dict = [
    {
        "origin": "North pole",
        "destination": "London",
        "origin_lat": "90",
        "origin_log": "0",
        "destination_lat": "51.50853",
        "destination_log": "-0.12574"
    },
    {
        "origin": "North pole",
        "destination": "Oslo",
        "origin_lat": "90",
        "origin_log": "0",
        "destination_lat": "59.91273",
        "destination_log": "10.74609"
    },
    {
        "origin": "London",
        "destination": "Oslo",
        "origin_lat": "51.50853",
        "origin_log": "-0.12574",
        "destination_lat": "59.91273",
        "destination_log": "10.74609"
    },
    {
        "origin": "Oslo",
        "destination": "London",
        "origin_lat": "59.91273",
        "origin_log": "10.74609",
        "destination_lat": "51.50853",
        "destination_log": "-0.12574"
    }
]

keep_list = []
ori_dest = []

#You make a list of all origin/destination for each dict entry
for dic in list_dict:
    #You sort that combination to cancel the origin/destination tag difference
    ori_dest.append(sorted([dic['origin'],dic['destination']]))

#You keep unique entries in your list
ori_dest = list(set(tuple(x) for x in ori_dest))

#You filter your original list of dict
for dic in list_dict:
    #If the sorted combination origin/dest is in your unique list, then you keep
    if tuple(sorted([dic['origin'],dic['destination']])) in ori_dest:
        keep_list.append(dic)
        #You purge your list of unique combinations to not add it twice
        ori_dest.remove(tuple(sorted([dic['origin'],dic['destination']])))

for dic in keep_list:
    print (dic)

输出:

{'origin': 'North pole', 'destination': 'London', 'origin_lat': '90', 'origin_log': '0', 'destination_lat': '51.50853', 'destination_log': '-0.12574'}
{'origin': 'North pole', 'destination': 'Oslo', 'origin_lat': '90', 'origin_log': '0', 'destination_lat': '59.91273', 'destination_log': '10.74609'}
{'origin': 'London', 'destination': 'Oslo', 'origin_lat': '51.50853', 'origin_log': '-0.12574', 'destination_lat': '59.91273', 'destination_log': '10.74609'}
于 2021-01-10T22:47:25.663 回答