词典无明显原因地发生了变化

2024-04-29 04:45:50 发布

您现在位置:Python中文网/ 问答频道 /正文

出于学习的原因,我正在尝试创建一个琐事游戏,但我的字典发生了一些奇怪的事情,我无法理解

我通常测试的代码是:

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'


Tags: httpscomapitypeeasyamountcatscience
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)

您将在此处重新分配给词典:

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

您要求for循环分配给icat[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一样)

相关问题 更多 >