Python 3.x Python映射列表返回的字典顺序不正确
我将在最后附加整个代码,但现在我要说到重点 我有一个函数,它获取一个标题和价格str,并把它放在dict上Python 3.x Python映射列表返回的字典顺序不正确,python-3.x,Python 3.x,我将在最后附加整个代码,但现在我要说到重点 我有一个函数,它获取一个标题和价格str,并把它放在dict上 def create_dict(elem): title = ... price = ... print(title) # to check if it's fine print(price, '\n') # same return {title, price} 此函数映射一个列表,然后将其打印在屏幕上 data = map( cre
def create_dict(elem):
title = ...
price = ...
print(title) # to check if it's fine
print(price, '\n') # same
return {title, price}
此函数映射一个列表,然后将其打印在屏幕上
data = map(
create_dict,
result_row_list
)
pprint(list(data))
如您所见,dict应该是这样的{'title':'price'}。但有时,它会反转
这是《指纹》中的
Bloodstained: Ritual of the Night
45,29
Portal Knights <-------- **focus on it**
14,79
Unforgiving - A Northern Hymn
5,79
好的,在再次阅读之后,我看到了错误,我返回了{title,price}作为一个集合,而不是dict。因为集合没有顺序,所以结果是这样的。我在这上面浪费时间有点傻
我不知道如果我删除这个问题,也许有一天它能帮助别人。所以,你们决定吧。无论如何,谢谢你注意,dicts现在保留了创建时插入的顺序,但是
[{'Bloodstained: Ritual of the Night', '45,29'},
{'14,79', 'Portal Knights'}, <-------- **same as before**
{'Unforgiving - A Northern Hymn', '5,79'},
...
{'MovieMator Video Editor Pro - Movie Maker, Video Editing Software', '14,48'}]
from soup import make_soup
from pprint import pprint
def create_dict(elem):
title = elem.find('span', {'class': 'title'}).text
price = elem \
.find('div', {'class': 'search_price'}) \
.text.split('R$')[-1].strip()
print(title)
print(price)
print('')
return {title, price}
url = 'https://store.steampowered.com/search/?filter=weeklongdeals'
soup = make_soup(url)
result_row_list = soup.find_all('a', {'class': 'search_result_row'})
data = map(
create_dict,
result_row_list
)
pprint(list(data))