如何将列表的值分配到字典里
我想把属于各个城市的值分配到省份,并且统计总数,目前只做到统计城市的总数,但想不出统计省份总数的方式。list1 = [["青岛",100],["长春",100],["吉林",100],["长春",100]]
province_dict = {"吉林":["长春","吉林","四平"], "山东":["青岛","东营","济南"],"江苏":["连云港","南京","苏州"]}
# Create a dictionary with city names as key
list2 = []
for i in list1:
list2.append(i)
dict1 = {i:[] for i in list2}
# Assign area into city
for i in list1:
for keys,values in dict1.items():
if i == keys :
values.append(i)
dict2 = dict(zip(dict1.keys(), [ for item in dict1.values()]))
list1 = [["青岛", 100], ["长春", 100], ["吉林", 100], ["长春", 100]]
province_dict = {"吉林": ["长春", "吉林", "四平"], "山东": ["青岛", "东营", "济南"], "江苏": ["连云港", "南京", "苏州"]}
city_dict = dict()# province_dict的反向字典
for k, v in province_dict.items():
for j in v:
city_dict = k# {"长春": "吉林", "四平": "吉林" ...}
# print(city_dict)
result = dict.fromkeys(province_dict.keys(), 0)# 创建结果字典
for c in list1:
result]] += c# 累计分
print(result) 问题已解决 谢谢大家! suchocolate 发表于 2021-10-28 08:27
谢谢大佬! suchocolate 发表于 2021-10-28 08:27
大佬 你是怎么学的呀 有什么推荐的学习方法和资源吗 mrdeer 发表于 2021-10-28 12:49
大佬 你是怎么学的呀 有什么推荐的学习方法和资源吗
小甲鱼的书,runoob.com,力扣题库等。
页:
[1]