我有以下 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> 打印 vote_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
最佳答案
您在每次迭代中重新初始化列表,因此在执行检查时它始终为空:
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_])
在此代码中,我生成一个列表推导式,从具有 “ward”
的 _data_
的所有元素中提取 “voting_station_name”
等于_no_
。我将此列表转换为一个集合以删除重复项并打印结果。
关于python - 从 JSON Python 字典列表中删除重复输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460552/