在JSON-Python中从字典列表中删除重复输出

2024-04-25 06:09:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的JSON文件,我正试图解析和遇到一些问题。你知道吗

[
{
"ballot_name": "LAPP, David",
"office": "MAYOR",
"votes": "7",
"voting_station": "3",
"voting_station_id": "703",
"voting_station_name": "Branton JR High School",
"voting_station_type": "Regular",
"ward": "7"
},
{
"ballot_name": "SMITH, Bill",
"office": "MAYOR",
"votes": "683",
"voting_station": "1",
"voting_station_id": "1101",
"voting_station_name": "St. Mary's Parish Hall",
"voting_station_type": "Regular",
"ward": "11"
},
{
"ballot_name": "HEATHER, Larry R",
"office": "MAYOR",
"votes": "1",
"voting_station": "37",
"voting_station_id": "737",
"voting_station_name": "Clover Living",
"voting_station_type": "Special",
"ward": "7"
},
{
"ballot_name": "OLSON, Curtis",
"office": "MAYOR",
"votes": "0",
"voting_station": "32",
"voting_station_id": "1432",
"voting_station_name": "Lake Bonavista Village",
"voting_station_type": "Special",
"ward": "14"
},
{
"ballot_name": "LIN, Jun",
"office": "COUNCILLOR",
"votes": "2",
"voting_station": "66",
"voting_station_id": "366",
"voting_station_name": "Memorial Park Library",
"voting_station_type": "Advance",
"ward": "3"
},
{
"ballot_name": "HEJDUK, Marek",
"office": "COUNCILLOR",
"votes": "0",
"voting_station": "67",
"voting_station_id": "767",
"voting_station_name": "Saddletowne Library",
"voting_station_type": "Advance",
"ward": "7"
},

我到目前为止的目标是做到以下几点

1>;打印投票站名称列表删除所有重复项-我可以打印但不能删除重复项?你知道吗

下面是我迄今为止尝试的代码。你知道吗

import json
import urllib

print "This is Json Data Parser Program \nThis program will download the Election Results from 2017 file from OpenData Portal"


_url_= "https://data.cityname.ca/resource/kqmd-3dsq.json"
_response_ = urllib.urlopen(_url_)
_data_= json.loads(_response_.read())

#with open('data.json', 'w') as outfile:
#    json.dump(_data_,outfile,indent=4,sort_keys=True)
def _ward_(_no_):
    print "Your choosen ward number is" , _no_
    for _i_ in _data_:
        result = []
        if (_i_["ward"] == _no_ and  _i_["voting_station_name"] not in result):
                    result.append(_i_["voting_station_name"])
                    print result

_ward_("12")

我可以得到如下输出,但是我们可以看到它有一些重复项"voting_station_name"

如何删除输出中的重复项?你知道吗

This is Json Data Parser Program
This program will download the CoC Election Results from 2017 file from OpenData Portal
Your choosen ward number is 12
Cranston School
McKenzie Towne Care Centre
Millican/Ogden Community Association
Age Care - Seton Seniors Community
Auburn Heights Retirement Residence
University of Calgary Taylor Family Digital Librar
McKenzie Towne Church
Age Care - Seton Seniors Community
Christ the King Catholic School
Auburn Heights Retirement Residence

Tags: namefromidjsondataistyperesult
1条回答
网友
1楼 · 发布于 2024-04-25 06:09:42

您在每次迭代中都要重新初始化列表,因此在执行检查时列表始终为空:

def _ward_(_no_):
    print "Your choosen ward number is" , _no_
    result = []
    for _i_ in _data_:
        if (_i_["ward"] == _no_ and  _i_["voting_station_name"] not in result):
                    result.append(_i_["voting_station_name"])
    print result

编辑:

你要求我改进代码结构。我不确定这是否是一个进步,你应该尝试和基准的结果,但我的发展会是这样的:

def _ward_(_no_):
    print "Your choosen ward number is" , _no_

    print set([e["voting_station_name"] for e in _data_ if e["ward"]==_no_])

在这段代码中,我生成一个列表压缩,从_data_的所有元素中提取"voting_station_name",这些元素的"ward"等于_no_。我将此列表转换为一个集合,以删除重复项并打印结果。你知道吗

相关问题 更多 >