字典更改没有明显原因

时间:2020-02-27 22:07:23

标签: python dictionary printing

出于学习原因,我试图创建一个琐事游戏,但是我的字典发生了一些我无法理解的奇怪现象。

我通常测试以查看其工作方式的代码是:

cat = {
    'easy': {
        'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=easy&type=boolean',
        'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=easy&type=boolean',
        'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=easy&type=boolean',
        'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=easy&type=boolean',
        'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=easy&type=boolean',
        'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=easy&type=boolean'
    },
    'medium': {
        'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean',
        'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean',
        'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean',
        'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean',
        'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean',
        'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean'
    }
}

print(cat)

for level in cat:
    print(level)

catselect = []
while catselect not in ("1", "2"):
    catselect = input("Select a category, for easy press 1, for medium press 2: ")

    if catselect == "1":
        selectedcat = "easy"
    elif catselect == "2":
        selectedcat = "medium"
    print(f"You selected the {selectedcat} difficulty level")

    print("The subjects can be: ")
    for i, cat[selectedcat] in enumerate(cat[selectedcat]):
        print(i, cat[selectedcat])

print(cat)

因此,当代码最终运行时,cat词典是不一样的 还有更多,我没有任何理由可能会发生这种情况。

这是我看到的:

{'easy': {'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=easy&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=easy&type=boolean', 'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=easy&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=easy&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=easy&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=easy&type=boolean'}, 'medium': {'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean', 'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean'}}
easy
medium
Select a category, for easy press 1, for medium press 2: 1
You selected the easy difficulty level
The subjects can be: 
0 general knowledge
1 books
2 films
3 music
4 sports
5 science & nature
{'easy': 'science & nature', 'medium': {'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean', 'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean'}}

easy的所有类别都去了哪里?为什么我最终以'science & nature'结尾?

2 个答案:

答案 0 :(得分:6)

您将在此处分配回字典:

for i, cat[selectedcat] in enumerate(cat[selectedcat]):

您要让for循环分配给i cat[selectedcat] 。不要那样做。

上面基本上是这样做的:

iterator = iter(enumerate(cat[selectedcat]))
while True:
    try:
        next_item = next(iterator)
    except StopIteration:
        break
    i, cat[selectedcat] = next_item

,因此将cat[selectedcat] 中的每个值分配给cat[selectedcat]本身

它确实起作用,因为cat[selectedcat]对象仍在引用enumerate()所引用的原始字典,因此其所有键仍在循环中生成。但是,cat字典本身被要求依次用每个类别字符串替换selectedcat键的值。您可以看到这种情况的发生,因为然后您在循环内打印 cat[selectedcat]的新值。

如果要用数字显示值,则要为循环使用其他新的变量名,例如:

for i, category in enumerate(cat[selectedcat]):
    print(i, category)

在这里,category是一个新变量(就像i一样。)

答案 1 :(得分:2)

问题是这一行:

    for i, cat[selectedcat] in enumerate(cat[selectedcat]):

每次循环,它都会将cat[selectedcat]的元素分配给cat[selectedcat],这将修改字典。因此,第一次通过循环,就可以了

cat[selectedcat] = cat[selectedcat][0]

循环完成后,cat[selectedcat]的值将是cat[selectedcat]的最后一个元素。

通常应将普通变量用作循环变量:

for i, value in enumerate(cat[selectedcat]):
    print(i, value)