gpt4 book ai didi

python - 对Python字典列表进行排序

转载 作者:行者123 更新时间:2023-12-01 05:56:53 24 4
gpt4 key购买 nike

我有这个代码:

from BeautifulSoup import BeautifulSoup

TABLE_CONTENT = [['958','<a id="958F" href="javascript:c_row(\'958\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'958\')" title="click date time to show origin_list (evid=958)">1945/07/16 11:29:45</a>','33.6753','-106.4747','','-.03','21','','','TRINITY','&nbsp;','&nbsp;','<a href="javascript:c_md(\'958\')" title="click here to show source data">SourceData</a>','&nbsp;'],['959','<a id="959F" href="javascript:c_row(\'959\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'959\')" title="click date time to show origin_list (evid=959)">1945/08/05 23:16:02</a>','34.395','132.4538','','-.58','15','','','LITTLEBOY','&nbsp;','&nbsp;','<a href="javascript:c_md(\'959\')" title="click here to show source data">SourceData</a>','&nbsp;']]

EVENT_LIST = []
for EVENT in TABLE_CONTENT:
events = {}
for index, item in enumerate(EVENT):
if index == 0:
events['id'] = item
if index == 4:
soup = BeautifulSoup(item)
for a in soup.findAll('a'):
events['date'] = ''.join(a.findAll(text=True))
if index == 2:
events['country'] = item
if index == 3:
events['type'] = item
if index == 5:
events['lat'] = item
if index == 6:
events['lon'] = item
if index == 8:
events['depth'] = item
if index == 9:
events['yield'] = item
if index == 12:
events['name'] = item
sorted(events, key=lambda key: events['id'])
EVENT_LIST.append(events)
print '=== new record ==='
EVENT_LIST.sort(key=lambda x: x['id'])
print EVENT_LIST

我遇到的第一个问题是,在 EVENT_LIST 中,字典对象的顺序与添加到列表中的顺序不同,例如,当我打印结果时,“lat”和“lon”不是按顺序:

[{'name': 'TRINITY', 'country': 'USA', 'lon': '-106.4747', 'yield': '21', 'lat': '33.6753', 'depth': '-.03', 'date': u'1945/07/16 11:29:45', 'type': 'Atmospheric', 'id': '958'}, {'name': 'LITTLEBOY', 'country': 'USA', 'lon': '132.4538', 'yield': '15', 'lat': '34.395', 'depth': '-.58', 'date': u'1945/08/05 23:16:02', 'type': 'Atmospheric', 'id': '959'}]

还有更好的方法来编写这段代码吗?

最佳答案

您可以使用 OrderedDict 保留插入字典的顺序。容器。来自手册:

Return an instance of a dict subclass, supporting the usual dict methods. An OrderedDict is a dict that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

此功能自 2.7 版本以来才出现。

@更好的方法:您可以将后续的 if index == ... 更改为 elif index == ... 因为,如果索引为 2,它永远不会可以是 5。或者您可以存储索引/键组合并使用它们来存储您的项目。示例(未尝试):

combos={
0: 'id',
2: 'country',
3: 'type',
5: 'lat',
6: 'lon',
8: 'depth',
9: 'yield',
12: 'name' }

...

for index, item ...:
if index == 4:
soup = BeautifulSoup(item)
for a in soup.findAll('a'):
events['date'] = ''.join(a.findAll(text=True))
elif index in combos:
events[combox[index]]=item

我想你明白了。

关于python - 对Python字典列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12059781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com