python list中嵌套dic或者list,根据某个字段去重

#列表中字典中的某个字段重复,进行去重
#车牌号重复的,去除掉当前记录
targets =[
    {
        "longitude":"116.2833386",
        "latitude":"38.7794393",
        "plateNo":"京A123456"
    },
    {
        "longitude":"116.28333654",
        "latitude":"38.77943432",
        "plateNo":"京A123456"
    },
    {
        "longitude": "116.283336521",
        "latitude": "38.77943433",
        "plateNo": "京A123452"
    }
]
# print(targets[0])
news_targets = []
news_targets.append(targets[0])
platenolist =[]
for item in targets:
    plateno = item["plateNo"]
    for new_item in news_targets:
        platenolist.append(new_item["plateNo"])
        if plateno not in  platenolist:
            news_targets.append(item)
print(news_targets)

打印结果:

[{'longitude': '116.2833386', 'latitude': '38.7794393', 'plateNo': '京A123456'}, {'longitude': '116.283336521', 'latitude': '38.77943433', 'plateNo': '京A123452'}]

上面的方法在数据量大的时候,去重速度太慢,推荐下面的方法:

 

from operator import itemgetter
from itertools import groupby

#根据车牌号进行分组,根据分组生成新的列表
target =[[116.4653721, 39.0811007, '京A123456'],[116.46537222, 39.0811321, '京B123456'],[116.46512322, 39.08143211, '京B123456']]
news_targets=[]
for elt, items in groupby(target, itemgetter(2)):
    # print("车牌号:",elt, "item:",items)
    # print("车牌号:", elt)
    veh_count = 0
    for i in items:
        veh_count = veh_count + 1
    news_targets.append(i)
    # print("临时列表“",len(temp_news_targets))
    # news_targets.append(temp_news_targets[0])
    # print(veh_count)
print("新的数据列表:", news_targets)
print("新数据的长度:", len(news_targets))


输出结果:
新的数据列表: [[116.4653721, 39.0811007, '京A123456'], [116.46512322, 39.08143211, '京B123456']]
新数据的长度: 2

你可能感兴趣的:(python,python,list)