首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JSON Python中删除字典列表中的重复输出

在JSON Python中删除字典列表中的重复输出
EN

Stack Overflow用户
提问于 2018-07-22 06:20:09
回答 1查看 123关注 0票数 1

我有以下JSON文件,我正在尝试解析,但遇到了一些问题。

代码语言:javascript
复制
[
{
"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列表-我可以打印但不能删除重复项?

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

代码语言:javascript
复制
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"

如何删除输出中的重复项?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-22 06:27:33

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

代码语言:javascript
复制
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

编辑:

你问我对代码结构的改进。我不确定这是否是一种改进,你应该尝试并对结果进行基准测试,但我的开发应该是这样的:

代码语言:javascript
复制
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_"ward"等于_no_的所有元素中提取"voting_station_name"。我将这个列表转换为一个集合,以删除重复项并打印结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51460552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档