gpt4 book ai didi

python - 循环打印重复天数

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

我有点困惑我想检查一个值在天数列表中重复了多少次。例如我有一本字典

dict = {
"01" : 0,
"02" : 0,
"03" : 0,
"04" : 0,
"05" : 0,
"06" : 0,
"07" : 0,
}

我有一个 values = ['02','01','02','03','07','05'] 的列表

我想打印这样的结果:

{"Monday": 1,"Tuesday": 2,"Wednesday": 1, "Thursday": 0, "Friday": 1, "Saturday": 0 ,"Sunday": 1}

这是我正在测试的代码

for i in range(len(value_list)):
for j in day:
print(j)

for i in range(len(value_list)):
print(day.key)
# count = value_list.count(list(day)[i])
if value_list[i] == (day):
print("i: ", value_list[i])

最佳答案

我创建了一个新字典叫做days_of_week,并且改变了你的dict的内容,让每一天都由一个数字来引用,所以当你遍历values 的列表并找到它增加特定日期的计数器的一天

代码:

days_of_week = {"Monday": 0, "Tuesday": 0, "Wednesday": 0,
"Thursday": 0, "Friday": 0, "Saturday": 0, "Sunday": 0}
values = ['02', '01', '02', '03', '07', '05']
dict = {"01": 'Monday', "02": 'Tuesday', "03": 'Wednesday', "04": 'Thursday', "05": 'Friday', "06": 'Saturday', "07": 'Sunday'}
for val in values:
if val in dict:
days_of_week[dict[val]]+=1
print(days_of_week)

输出:

{'Monday': 1, 'Tuesday': 2, 'Wednesday': 1, 'Thursday': 0, 'Friday': 1, 'Saturday': 0, 'Sunday': 1}

关于python - 循环打印重复天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70376051/

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