首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与字典混淆

与字典混淆
EN

Stack Overflow用户
提问于 2020-11-02 03:43:02
回答 3查看 53关注 0票数 0

在第一个for循环之后,我的字典缩小了,在for循环的结尾处只剩下一个键值对,如结果所示。最后不知何故,它给出了字符串错误。有没有人能建议我对字典有什么不理解的地方?

代码语言:javascript
复制
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]}')

结果:

代码语言:javascript
复制
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'
EN

回答 3

Stack Overflow用户

发布于 2020-11-02 03:51:42

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

尝试此更新:

代码语言:javascript
复制
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]}')
票数 0
EN

Stack Overflow用户

发布于 2020-11-02 03:57:15

问题是在这些代码行中:

代码语言:javascript
复制
for abbrev, cities in list(cities.items()):
    print(f'the {abbrev} state has {cities} city')

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

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

代码语言:javascript
复制
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]}')
票数 0
EN

Stack Overflow用户

发布于 2020-11-02 03:52:00

如果检查第34行,python中不接受引用类型。相反,您应该使用'‘或’‘类型的引号。另外,在循环for states中,list(state**enter code here**s.items())中的缩写将其设置为,在list(states.items())中设置为

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64636233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档