在字典中搜索具有两个数字之间值的键

时间:2016-11-13 23:03:49

标签: python python-3.x dictionary search

我需要分析一个字典,其值包含两个给定数字之间的数字(作为参数),并返回那些以其键开头的值

字典:

{'P':[("Eight",1460, 225.0, 200.0, "fresco","Netherlands"),("Six",1465,81.0, 127.1, "tempera", "Netherlands")],
        'V':[("Four",1661, 148.0, 257.0,"oil paint", "Austria"),("Two",1630, 91.0, 77.0, "oil paint","USA")],
        'K':[("Five",1922,63.8,48.1,"watercolor","USA"),("Seven",1950,61.0,61.0,"acrylic paint","USA"),("Two",1965,81.3,100.3,"oil paint","United Kingdom")],
        'C':[("Ten",1496,365.0,389.0,"tempera","Italy")],
        'U':[("Nine",1203,182.0, 957.0,"egg tempera","Italy"), ("Twelve",1200,76.2,101.6,"egg tempera","France")]
        }

该函数应仅返回两个数字之间存在数字的值。因此,如果函数被调用between_two_values,它应该返回这个,如果搜索1464和1496之间的值:

between_two_values(dictionary1(), 1464, 1496)

{'P': [('Six', 1465, 81.0, 127.1, 'tempera',    
'Netherlands')], 'C': [('Ten', 1496, 365.0, 
389.0, 'tempera', 'Italy')]}

如果密钥的某个值没有1464-1496之间的数字,则它不应返回该值,只有那个在该范围内具有数字的值才能返回其值。这就是为什么在上面的例子中P'没有返回1460的第一个值,因为它不在2个数字之间。函数中的第一个数字应该总是小于第二个,如果第一个数字大,那么它应该只返回一个空字典。

这是我提出的代码我不认为它是正确的,但它显示了可以解决此功能的逻辑。我感谢任何帮助

def between_two_values(dictionary,start,end):
    for x in dictionary:
        if end < x < start in dictionary:
            return dictionary(x)

2 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。这是提出问题的一种解决方案。

为了清晰起见,我已将数据格式化得更好。当它被压缩时,我没有立即看到每个字典值都包含在一个列表中。当然,这是一种以风格为导向的变化,但风格有助于提高可读性。

请注意,我做了一些假设,例如每个字典值都是一个列表。例如,没有值的键的边缘情况将表示为[]而不是None。我还有一些推断我认为所需输出来自你给出的例子。最后,您可以考虑使用collections.defaultdict来简化存储匹配的位置。

除此之外,这段代码没什么特别的。你当然可以将它压缩得更多,或者使用类来进行语义化。说到语义,我建议你使用比我更好的变量名:&#34;数据&#34;,&#34;记录&#34;和&#34;值&#34;是非常通用的,但我觉得他们帮助解释了解决方案,而我没有了解这些数据代表什么。

如果您使用的是Python 2,请考虑使用dictionary.iteritems()代替dictionary.items()

数据

data = {
    'P': [
        ('Eight', 1460, 225.0, 200.0, 'fresco', 'Netherlands'), 
        ('Six', 1465, 81.0, 127.1, 'tempera', 'Netherlands'), 
    ], 
    'V': [
        ('Four', 1661, 148.0, 257.0, 'oil paint', 'Austria'), 
        ('Two', 1630, 91.0, 77.0, 'oil paint', 'USA'), 
    ], 
    'K': [
        ('Five', 1922, 63.8, 48.1, 'watercolor', 'USA'), 
        ('Seven', 1950, 61.0, 61.0, 'acrylic paint', 'USA'), 
        ('Two', 1965, 81.3, 100.3, 'oil paint', 'United Kingdom'), 
    ], 
    'C': [
        ('Ten', 1496, 365.0, 389.0, 'tempera', 'Italy'), 
    ], 
    'U': [
        ('Nine', 1203, 182.0, 957.0, 'egg tempera', 'Italy'), 
        ('Twelve', 1200, 76.2, 101.6, 'egg tempera', 'France'), 
    ], 
}

代码

def between_two_values(dictionary, start, end):
    matches = {}
    for key, record_list in dictionary.items():
        for record in record_list:
            value = record[1]
            if start < value < end:
                if key in matches:
                    matches[key].append(record)
                else:
                    matches[key] = [record]
    return matches

result = between_two_values(data, 1464, 1496)
print(result)

输出

{'P': [('Six', 1465, 81.0, 127.1, 'tempera', 'Netherlands')]}

答案 1 :(得分:0)

您可以使用dict理解来构造结果,例如:

>>> {k: [e for e in v if 1464 < e[1] < 1496] for k, v in dictionary.items()}
{'C': [],
 'K': [],
 'P': [('Six', 1465, 81.0, 127.1, 'tempera', 'Netherlands')],
 'U': [],
 'V': []}

然后消除空结果:

def between_two_values(dictionary, start, end):
    result = {k: [e for e in v if start < e[1] < end] for k, v in dictionary.items()}
    return {k: v for k, v in result.items() if v}