python:解決在for循環中將字典添加到列表中出現數據覆蓋的問題


出現的問題:

def get_all_ascii_art(self):
result_dic = {}
result_list = []
ascii_art_url = 'https://www.xxxx.net/example'
response = httpx.get(ascii_art_url, headers=self.headers_)
selector = Selector(response.text)
li_list = selector.xpath('//div[@class="category-list"]/ul/li').extract()
for li in li_list:
li = Selector(li)
# link = li.xpath('//a/@href').extract_first()
link = li.css('a::attr(href)').get()
url = ''.join(['https://www.xxxx.net', link])

result_dic['url'] = url
print(url)
type_name = li.css('a::text').get()
result_dic['name'] = type_name
print(type_name)
counts = li.css('small::text').get()
result_dic['count'] = counts
print(counts)
print(result_dic)
result_list.append(result_dic)
print(result_list)
return result_list
上述代碼 想把字典result_dic的數據在循環中添加到列表result_list,
運行結果卻是:

[{'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxx.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}]

Process finished with exit code 0

如上:每次for循環之后數據添加到字典中時都會覆蓋掉上次添加的數據.

回頭再去看看python中字典數據類型的使用方式。。。發現

使用字典的類似dict['a'] = b,這種賦值方式時:如果字典里有相對應的key,那么數據就會被覆蓋掉;如果沒有對應的key才會添加到字典里。

(引用:如果對 dict 中存在的 key-value 對賦值,新賦的 value 就會覆蓋原有的 value,)

同理:update() 方法可使用一個字典所包含的 key-value 對來更新己有的字典。在執行 update() 方法時,如果被更新的字典中己包含對應的 key-value 對,

那么原 value 會被覆蓋;如果被更新的字典中不包含對應的 key-value 對,則該 key-value 對被添加進去。

解決辦法:

1.在循環里字典初始化,每次for循環都將字典初始化,再添加數據,就不會被覆蓋。(每次字典重新開辟內存空間,並使變量dict指向該空間,因此不會出現相同地址的問題)

2.每次為列表添加數據的時候,在內存中其他位置創建與該字典相同的數據並加入列表(若字典內包含列表,需要使用deepcopy)

由於上述代碼中使用的字典內沒有其他容器型數據,所以使用第一種方法來解決數據被覆蓋的問題

結果如下:

[{'url': 'https://www.xxxxl.net/ascii-art/chinese', 'name': '中文文字', 'count': '(103)'}, {'url': 'https://www.xxxxl.net/ascii-art/animals', 'name': '動物', 'count': '(592)'}, {'url': 'https://www.xxxxl.net/ascii-art/vehicles', 'name': '車輛', 'count': '(109)'}, {'url': 'https://www.xxxxl.net/ascii-art/airplanes', 'name': '飛機', 'count': '(159)'}, {'url': 'https://www.xxxxl.net/ascii-art/weapons', 'name': '武器', 'count': '(88)'}, {'url': 'https://www.xxxxl.net/ascii-art/nature', 'name': '自然', 'count': '(96)'}, {'url': 'https://www.xxxxl.net/ascii-art/mythology', 'name': '神話', 'count': '(116)'}, {'url': 'https://www.xxxxl.net/ascii-art/space', 'name': '宇宙太空', 'count': '(75)'}, {'url': 'https://www.xxxxl.net/ascii-art/holiday-and-events', 'name': '節日與活動', 'count': '(96)'}, {'url': 'https://www.xxxxl.net/ascii-art/books', 'name': '書籍', 'count': '(27)'}, {'url': 'https://www.xxxxl.net/ascii-art/cartoons', 'name': '卡通動漫', 'count': '(273)'}, {'url': 'https://www.xxxxl.net/ascii-art/sports', 'name': '運動', 'count': '(101)'}, {'url': 'https://www.xxxxl.net/ascii-art/movies', 'name': '電影', 'count': '(126)'}, {'url': 'https://www.xxxxl.net/ascii-art/electronics', 'name': '電子設備', 'count': '(105)'}, {'url': 'https://www.xxxxl.net/ascii-art/computers', 'name': '計算機', 'count': '(111)'}, {'url': 'https://www.xxxxl.net/ascii-art/buildings', 'name': '建築物', 'count': '(146)'}, {'url': 'https://www.xxxxl.net/ascii-art/people', 'name': '人物', 'count': '(168)'}, {'url': 'https://www.xxxxl.net/ascii-art/plants', 'name': '植物', 'count': '(115)'}, {'url': 'https://www.xxxxl.net/ascii-art/toys', 'name': '玩具', 'count': '(21)'}, {'url': 'https://www.xxxxl.net/ascii-art/religion', 'name': '宗教&信仰', 'count': '(22)'}, {'url': 'https://www.xxxxl.net/ascii-art/comic', 'name': '漫畫', 'count': '(47)'}, {'url': 'https://www.xxxxl.net/ascii-art/food', 'name': '食物&飲料', 'count': '(57)'}, {'url': 'https://www.xxxxl.net/ascii-art/art-and-design', 'name': '藝術&設計', 'count': '(109)'}, {'url': 'https://www.xxxxl.net/ascii-art/music', 'name': '音樂', 'count': '(33)'}, {'url': 'https://www.xxxxl.net/ascii-art/clothing-and-accessories', 'name': '服裝首飾', 'count': '(52)'}, {'url': 'https://www.xxxxl.net/ascii-art/furniture', 'name': '家具&裝飾', 'count': '(87)'}, {'url': 'https://www.xxxxl.net/ascii-art/gesture', 'name': '手勢', 'count': '(27)'}, {'url': 'https://www.xxxxl.net/ascii-art/characters', 'name': '英文文字', 'count': '(43)'}, {'url': 'https://www.xxxxl.net/ascii-art/games', 'name': '游戲', 'count': '(67)'}, {'url': 'https://www.xxxxl.net/ascii-art/logos', 'name': '圖標', 'count': '(108)'}, {'url': 'https://www.xxxxl.net/ascii-art/other', 'name': '其他', 'count': '(103)'}]

Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM