从JSON Python中的Dictionary列表中删除重复的输出

阿什维亚

我有以下要解析的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>打印voting_station_name的列表,删除所有重复项-我可以打印但不能删除重复项?

以下是到目前为止我尝试过的代码。

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
Poshi

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

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_])

在这段代码中,我生成了一个列表理解,该列表理解"voting_station_name"_data_具有"ward"等于的所有元素中提取_no_我将此列表转换为一个集合,以删除重复项并打印结果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章