简体   繁体   English

如何通过指定值范围从字典中提取数据?

[英]How can I extract data from a dictionary by specifying a range of values?

I have a dictionary, 我有一本字典,

{"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"} 

I want to extract the data from this dictionary whose values are in the range 20 - 30. 我想从这个字典中提取数据,其值在20 - 30范围内。

Example. 例。 I want the output should be 我想输出应该是

apple:20   
banana:25 

I was thinking to order the dictionary first by the values, but it does not seem to work. 我正在考虑首先按值排序字典,但它似乎不起作用。

I tried dict= sorted(dict.items(), key=lambda t: t[1]) 我试过dict= sorted(dict.items(), key=lambda t: t[1])
but the output shows like this: 但输出显示如下:

[("apple", "20"), ("orange" , "40"), ("banana" , "25"), ("mango" , "50")]

How can I get output as: 我怎样才能得到输出:

apple:20   
banana:25

You can do the following: 您可以执行以下操作:

> d = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"}
> for item in sorted(d.items(), key=lambda i: int(i[1])):
>   print ': '.join(item)
apple: 20
banana: 25
orange: 40
mango: 50

sorted sorts the dict 's items (which are key-value tuples ) by the value using aa specific sorting key . sorted排序的dict的项(其为键-值tuples ),使用AA特定排序由值key

sample_dict={"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"}
for name,num in sample_dict.items():
    if int(num) in range(20,31):
        print('%s: %s' %(name,num))
d = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"}

for k, v in d.items():
    if 20 <= int(v) <= 30:
        print(k, ':', v, sep='')

prints 版画

apple:20
banana:25

With using of list comprehension you can extract (key,values) that their values (first cast to int ) are in range 20-30 : 通过使用列表推导,您可以提取(key,values)它们的值(首次转换为int )在20-30范围内:

d = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"}
[(k,v) for k,v in d.items() if  20 <= int(v) <= 30]
# [('banana', '25'), ('apple', '20')]

Then you can have your desired output. 然后你可以得到你想要的输出。

Returns a dict {'apple': '20', 'banana': '25'} 返回一个字典{'apple': '20', 'banana': '25'}

>>> d = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"} >>> {key: val for key,val in d.items() if 20 <= int(val) <= 30} {'apple': '20', 'banana': '25'} >>> d = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"} >>> {key: val for key,val in d.items() if 20 <= int(val) <= 30} {'apple': '20', 'banana': '25'}

Using dictionary comprehension: 使用字典理解:

the_dict = {"apple" : "20", "orange" : "40", "banana" : "25", "mango" : "50"}

new_dict = {the_dict.keys()[i]:the_dict.values()[i] for i in range(len(the_dict.values())) if int(the_dict.values()[i]) <= 30 and    int(the_dict.values()[i] >= 20)}

print new_dict

You could use a set to obtain fast membership testing. 您可以使用一set来获得快速成员资格测试。 Doing so also eliminates the need to convert the dictionary's values to integers since the members of the set are strings: 这样做也消除了将字典的值转换为整数的需要,因为set的成员是字符串:

d = {"apple": "20", "orange": "40", "banana": "25", "mango": "50"}
limits = set(str(n) for n in range(20, 31))

for key, value in sorted(d.items()):
    if value in limits:
        print('{}:{}'.format(key, value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM