我已经从REST API中读取并过滤了我需要的数据,如下所示:
[[key,value] for groups in results for group in groups['results'] for key,value in group.items() if key in ['utc_offset','country','city','state','name','link','lat','lon']]
我的输出:
[['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/slashdot-3/'],
['lon', -79.44000244140625],
['name', 'The Toronto Slashdot Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/webcentric/'],
['lon', -79.44000244140625],
['name', 'The Greater Toronto Web Centric Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/php-27/'],
['lon', -79.37999725341797],
['name', 'The Toronto PHP Meetup Group'],
['state', 'ON'],
.
.
.
]]
我尝试做的是将重复键分组,以便将它们建模为Django的数据对象:
[['utc_offset', -14400000],['country', 'CA'],['city', 'Toronto'],['link', 'https://www.meetup.com/slashdot-3/'],['lon', -79.44000244140625],['name', 'The Toronto Slashdot Meetup Group'],['state', 'ON'],['lat', 43.7599983215332]]
我试过这个:
[zip(range(8),[key,value]) for groups in results for group in groups['results'] for key,value in group.items() if key in ['utc_offset','country','city','state','name','link','lat','lon']]
但我的输出是这样的:
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F388>
.
.
.
# twice the amount of the records I already have
所以这几乎不是答案,所以任何帮助都会很棒。谢谢!
修改 我想要的输出就是这样
[['utc_offset', value],['country',value],['city',value],['link',value],['lon',value],['name',value],['state',value],['lat',value]]
答案 0 :(得分:1)
我认为这就是你所追求的目标(我仍然不能100%确定你希望我对每个结果块做什么):
l = [['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/slashdot-3/'],
['lon', -79.44000244140625],
['name', 'The Toronto Slashdot Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/webcentric/'],
['lon', -79.44000244140625],
['name', 'The Greater Toronto Web Centric Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/php-27/'],
['lon', -79.37999725341797],
['name', 'The Toronto PHP Meetup Group'],
['state', 'ON']]
result = []
tmp = []
for item in l:
if item[0] == 'lat':
tmp.append(item)
result.append(tmp)
tmp = []
else:
tmp.append(item)
print(result)
结果是:
[
[
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/slashdot-3/'],
['lon', -79.44000244140625],
['name', 'The Toronto Slashdot Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332]
],
[
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/webcentric/'],
['lon', -79.44000244140625],
['name', 'The Greater Toronto Web Centric Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332]
]
]
utc_offset
到lat
的每个块都会放在自己的列表中。如果您需要其他格式,请告诉我。