基于valu的字典键打印

2024-06-01 04:33:53 发布

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

大家好。我在为我的cs期末考试而学习,字典在里面。我对字典很熟悉,但这本让我难堪。我该如何着手解决这样一个问题?你知道吗

Given the dictionary below, continue to write code (old style, no functions required) that will produce a list of all the keys for which the values are in the range 1 to 100 inclusive. Do not hardcode your program for the given dictionary.

D = {1:1000, 2:2000, 3:3000, 1111:10, 2222:20, 3333:30}

是的,这是一个实际的复习问题,但这不是家庭作业。你知道吗

我在想我应该在字典中做一个for循环,但那不会有帮助,因为dict是存储的key:value,我真的只需要比较value。你知道吗

任何事都值得感激!你知道吗


Tags: thetofordictionary字典valuestylecode
3条回答

我想我真的弄明白了!你知道吗

我没有运用理解力,因为我的教授从来没有因为任何原因复习过。你知道吗

d ={1:1000, 2:2000, 3:3000, 1111:10, 2222:20, 3333:30}

for key in d:
    if d[key] >=1 and d[key] <=100:
        print(key)
    else:
        pass

以下内容如何:

>>> D = {1:1000, 2:2000, 3:3000, 1111:10, 2222:20, 3333:30}
>>> [key for key in D if 1 <= D[key] <= 100]
[3333, 2222, 1111]
[k for k, v in D.items() if 1 <= v <= 100]

相关问题 更多 >