【问题标题】:confused with dictionaries与字典混淆
【发布时间】:2021-02-14 13:26:36
【问题描述】:

在第一个 for 循环之后,我的字典缩小了,在 for 循环结束时我只剩下一个键值对,如结果所示。最后以某种方式给出了字符串错误。任何人都可以提出我对字典不了解的地方吗?

states = {
           'Bagmati' : 'BG',
           'Gandaki' : 'GD',
           'Karnali' : 'KL',
           'Janakpur' : "JK",
           'Mechi' : 'MC'
           }

cities = {
            'BG' : 'Kathmandu',
            "GD" : "Pokhara",
            "KL" : "Jumla",
            "MC" : 'Jhapa'
            }
            

cities['BG'] = 'Hetauda'
cities['MC'] = 'Taplejung'
cities['BG'] = 'Ramechap'
cities['JK'] = 'Dhanusha'

print(f'1. {states}')

for states, abbrev in list(states.items()):
    print(f'the {states} is abbreviated as {abbrev}')
print(states)
print(cities)    
#print every city and states
for abbrev, cities in list(cities.items()):
    print(f'the {abbrev} state has {cities} city')
print(f'2. {states}')
print(f'3. {cities}')
# now lets access both the states and cities dictionaries
for states, abbrev in list(state`enter code here`s.items()):
    print(f' the {states} state is abbreviated as {abbrev}')
    print(f' and has the city {cities[abbrev]}')
    

结果:

C:\Users\Prabin\Desktop\Desktop\personal-projects\mystuff\39. Dictionaries>python new12.py
1. {'Bagmati': 'BG', 'Gandaki': 'GD', 'Karnali': 'KL', 'Janakpur': 'JK', 'Mechi': 'MC'}
the Bagmati is abbreviated as BG
the Gandaki is abbreviated as GD
the Karnali is abbreviated as KL
the Janakpur is abbreviated as JK
the Mechi is abbreviated as MC
Mechi
{'BG': 'Ramechap', 'GD': 'Pokhara', 'KL': 'Jumla', 'MC': 'Taplejung', 'JK': 'Dhanusha'}
the BG state has Ramechap city
the GD state has Pokhara city
the KL state has Jumla city
the MC state has Taplejung city
the JK state has Dhanusha city
2. Mechi
3. Dhanusha
Traceback (most recent call last):
  File "new12.py", line 36, in <module>
    for states, abbrev in list(states.items()):
AttributeError: 'str' object has no attribute 'items'

【问题讨论】:

  • 因为您正在重新使用变量states:for states, abbrev in list(states.items()):,所以您的字典会被字符串替换。注意,不要使用list(states.items()),你可以使用states.items()for abbrev, cities in list(cities.items()) 也一样
  • 什么是state'enter code here's.items
  • 基本上:选择与块变量名称不冲突的循环变量名称。 Python 变量作用于函数作用域,而不是块作用域,因此它会覆盖循环外块中的字典。 minimal reproducible example 将是:a = 42; for a in range(1): pass; print(a) => 0

标签: python dictionary


【解决方案1】:

正如@jumpa 提到的,您将字典名称重新用作另一个变量。

试试这个更新:

for state, abbrev in list(states.items()):
    print(f'the {state} is abbreviated as {abbrev}')
print(states)
print(cities)    
#print every city and states
for abbrev, city in list(cities.items()):
    print(f'the {abbrev} state has {city} city')
print(f'2. {states}')
print(f'3. {cities}')
# now lets access both the states and cities dictionaries
for state, abbrev in list(states.items()):
    print(f' the {state} state is abbreviated as {abbrev}')
    print(f' and has the city {cities[abbrev]}')

【讨论】:

  • 非常感谢。那解决了它。所以在这里,基本上我将字典名称重用为循环变量,在列表变量中滚动,在循环结束时,字典最终成为列表中最后一项的字符串。
【解决方案2】:

如果您检查行号。 34 引用类型 `` 在 python 中不被接受。相反,您应该使用 '' 或 " " 类型的引号。 同样在循环中 for states, abbrev in list(stateenter code heres.items()) 使其 for states, abbrev in list(states.items())

【讨论】:

  • 这与问题无关,只是一个不幸的错字,与 SO 的代码块的愚蠢代码编辑器自动建议有关。
【解决方案3】:

问题在于以下几行:

for abbrev, cities in list(cities.items()):
    print(f'the {abbrev} state has {cities} city')

您正在覆盖 cities 变量的原始值。而且,它将保存cities.items()给出的循环中的最后一个值

只需在循环中使用另一个变量。您的代码将如下所示:

for state, abbrv in list(states.items()):
    print(f'the {states} is abbreviated as {abbrv}')
print(states)
print(cities)    
#print every city and states
for abbr, city in list(cities.items()):
    print(f'the {abbr} state has {city} city')
print(f'2. {states}')
print(f'3. {cities}')
# now lets access both the states and cities dictionaries
for state, abbrv in states.items():
    print(f' the {states} state is abbreviated as {abbrv}')
    print(f' and has the city {cities[abbrv]}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多