需要通过调用范围内的键来打印字典的值

Need to print the values of a dictionary by calling the keys within a range

我用

做了一本字典
key = integer   
value = string 

我想制作一个 .exe(对于我的水平来说可能有点早),我可以在其中输入 2 个整数,它会在我指定的范围内打印键的值。

例如。

{1: 'General 001-002', 3: 'Computer science', 7: 'Bibliography', 20: 'Library & Information', 30: 'General 030-068', 69: 'Museum Science', 70: 'Journalism & News Media', 80: 'General 080-099', 100: 'Philosophy 100-149', 150: 'Psychology',}

我想输入 5 和 85 并希望它打印

Bibliography  
Library & Information  
General 030-068  
Museum Science  
Journalism & News Media  
General 080-099

编辑:

谢谢大家的建议。

我的代码现在看起来像这样并且正常工作:

text_file = open("dewey.txt", "rt")                     #Open the list containing the values

text = text_file.read().split('\n')
text_file.close()


num = []
with open ('dewey num.txt', 'rt') as in_file:           #Open the list containing the keys
    for line in in_file:
        num.append(line[:3])
intenum = []
for i in num:
    intenum.append(int(i))                              #Make the keys as integers


dict1= dict(zip(intenum,text))                          #Merge the 2 lists in a dictionary
print ('This software will give you the subjects for each stack.')                                  #Instructions for the user
print ('I need to know the range first, please only use the first three numbers of the classmark.') #Instructions for the user

x=input('Please key in the starting number for this stack: ')                                       #Input for start
y=input('Please key in the last number for this stack: ')                                           #Input for stop

start = int(x)
stop = int(y)

values = [dict1[k] for k in range(start, stop + 1) if k in dict1]                                   #Call the values in range
print('The subject(s) for this section: ')
for i in values:                                                                                    #Print the values in range
    print (i)

我的下一步是将它变成 .exe,所以我正在研究 py2exe、pyinstaller 和 cx_freeze。如果您对我的脚本有什么更好的见解,将不胜感激。

我正在使用 Python 3.5.2

阿莱西奥

解决方案很短:

# this is your dictionary
d = {1: 'General 001-002', 3: 'Computer science', 7: .....}

start = 5
stop = 85

values = [d[k] for k in range(start, stop + 1) if k in d]

您可以在 values - 列表中获得所需的内容。

我省略了大部分字典,但它存储在 d

我使用了列表推导,在键 k 处为 k 运行 取范围内的字典值(停止 + 1 因为 range 到达结束范围减去 1),前提是 k 在键值范围内。

表达式

k in d

测试关键字k是否在字典d的关键字中,与测试相同:

k in d.keys()

如果键整数远大于字典中的项目数,则循环遍历范围内的每个整数以查找字典中存在的键可能效率低下。另一种策略是过滤字典的键,对它们进行排序,然后构建所需值的列表。例如,

def key_range(d, lo, hi):
    r = range(lo, hi + 1)
    return [d[k] for k in sorted(k for k in d.keys() if k in r)] 

data = {
    1: 'General 001-002',
    3: 'Computer science',
    7: 'Bibliography',
    20: 'Library & Information',
    30: 'General 030-068',
    69: 'Museum Science',
    70: 'Journalism & News Media',
    80: 'General 080-099',
    100: 'Philosophy 100-149',
    150: 'Psychology',
}

# Test

for item in key_range(data, 5, 85):
    print(item)

输出

Bibliography
Library & Information
General 030-068
Museum Science
Journalism & News Media
General 080-099

FWIW,这是该算法的 "functional" 实现。

def key_range(d, lo, hi):
    r = range(lo, hi + 1)
    return list(map(d.get, sorted(filter(lambda k: k in r, d.keys()))))

在Python2中,那个list调用可以去掉,因为Python2版本的filterreturns一个列表。 OTOH,在 Python 2 中 k in r 测试效率很低;做 lo <= k <= hi.

会更好