我不确定此问题是否曾被问过,但是......在将字典值添加到字典中的“值”后,我需要帮助来更新字典值。请帮忙!
import csv
with open('USVideos.csv', encoding="utf-8") as file:
reader = csv.reader(file, delimiter=',')
header = next(reader)
youtube_usa_videos = []
for row in reader:
youtube_usa_videos.append(row)
Unique_channels = []
for list in youtube_usa_videos:
if list[2] in Unique_channels:
continue
else:
Unique_channels.append(list[2])
Like_per_channel = dict((channel, 0) for channel in Unique_channels)
for (key, value) in Like_per_channel.items():
for list in youtube_usa_videos:
if list[2] == key:
value += int(list[7])
else:
continue
print(Like_per_channel)
在找到我想要增加的值的唯一键后,如何更新“Like_per_channel.items()”中的值?
谢谢!
答案 0 :(得分:0)
而不是:
if list[2] == key:
value += int(list[7])
使用:
if list[2] == key:
Like_per_channel[key] += int(list[7])
更新正在遍历的字典不是一个好主意。您可以创建副本并进行更新。