简体   繁体   English

为什么我尝试遍历列表列表并更新字典值导致字典未更改?

[英]Why is my attempt to iterate through a list of lists and update dictionary values resulting in unchanged dictionary?

I want to iterate through a list of lists and if an instance in the list matches a key in a dictionary, add 1 to that dictionary key value.我想遍历列表列表,如果列表中的实例与字典中的键匹配,则将 1 添加到该字典键值。 However, when I execute my code it returns the dictionary with no changes to the values.但是,当我执行我的代码时,它会返回字典而不更改值。 Thank you for your time & help.感谢您的时间和帮助。 I searched for a similar question but did not find one that addressed this issue.我搜索了一个类似的问题,但没有找到解决这个问题的问题。 My apologies if I missed it.如果我错过了,我深表歉意。

I have a list of hurricanes and the areas they affected.我有一份飓风及其影响地区的清单。 I have a dictionary with key, value pairs for the uniquely affected area, and the number of times affected (set to zero).我有一个字典,其中包含唯一受影响区域的键、值对以及受影响的次数(设置为零)。 I want to iterate through the list of hurricane instances and each time I pass through a location in the dictionary increase the value of the unique area by 1. Currently, with the code I'm running the returned dictionary is unchanged.我想遍历飓风实例列表,每次通过字典中的位置时,将唯一区域的值增加 1。目前,使用我正在运行的代码,返回的字典保持不变。

I've included smaller examples of material I'm working with: (test_list is a list of areas affected by a hurricane resulting in a list of lists)我已经包含了我正在使用的材料的较小示例:(test_list 是受飓风影响的区域列表,导致列表列表)

test_list = [['Central America', 'Mexico', 'Cuba', 'Florida', 'The Bahamas'], ['Lesser Antilles', 'The Bahamas', 'United States East Coast', 'Atlantic Canada'], ['The Bahamas', 'Northeastern United States'], ['Lesser Antilles', 'Jamaica', 'Cayman Islands', 'Cuba', 'The Bahamas', 'Bermuda']]

test_dict = {
'Central America': 0, 'Mexico': 0, 'Cuba': 0, 'Florida', 0, 'The Bahamas': 0, 'Lesser Antilles': 0, 'United States East Coast', 'Atlantic Canada': 0, 'Northeastern United States': 0, 'Jamaica': 0, 'Cayman Islands': 0, 'Bermuda': 0}

GOAL目标

if I performed:如果我执行:

area_counted = area_counter(test_list, test_dict)

I would have expected:我本来期望:

area_counted = {
'Central America': 1, 'Mexico': 1, 'Cuba': 2, 'Florida', 1, 'The Bahamas': 4, 'Lesser Antilles': 2, 'United States East Coast': 1, 'Atlantic Canada': 1, 'Northeastern United States': 1, 'Jamaica': 1, 'Cayman Islands': 1, 'Bermuda': 1}

However, this was not my result.然而,这不是我的结果。

CODE代码

def area_count(input_dict, input_list):
  new_dict = {}
  new_dict.update(input_dict)
  for i in list:
      for j in i:
        if j == new_dict[j]:
          new_dict[j] += 1
        else:
          pass
  return new_dict

area_counted = area_count(test_dict, test_list)
print(area_counted)

outputs the following:输出以下内容:

area_counted = {
'Central America': 0, 'Mexico': 0, 'Cuba': 0, 'Florida', 0, 'The Bahamas': 0, 'Lesser Antilles': 0, 'United States East Coast', 'Atlantic Canada': 0, 'Northeastern United States': 0, 'Jamaica': 0, 'Cayman Islands': 0, 'Bermuda': 0}

edit-1: edited area_count parameters to input_list instead of list, corrected parameter input order for area_counted to match area_count function. edit-1:将 area_count 参数编辑为 input_list 而不是 list,更正了 area_counted 的参数输入顺序以匹配 area_count function。

new_dict[j] always is equal to 0 initially (since in test_dict all the values are 0) new_dict[j]最初总是等于0 (因为在 test_dict 中所有的值都是 0)

you are comparing it to a string您正在将其与字符串进行比较

if "Central America" == 0:
    set_to_1()

hopefully you can see how this never sets it to 1希望您能看到这永远不会将其设置为 1

I think what you want is我想你想要的是

if j in new_dict:
    new_dict[j] = 1

as an unrelated aside you really should not name a variable list as that is a built in entity that you end up shadowing作为一个无关的,你真的不应该命名一个变量list ,因为它是一个你最终会隐藏的内置实体

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM