Python:从字典中删除彼此在range()内的值

时间:2019-01-08 03:59:35

标签: python dictionary

我正在尝试从dict中删除彼此相差2位以内的值,同时保持相似值的最小值。对于下面的dict,这将意味着仅删除group2和group3,而将group1保留,因为54、55和56都在彼此的两位数之内,并且由于54是这三个相似值中的最小值,因此它保留在字典

words = {
        "the": 10,
        "group1": 54,
        "then": 40,
        "now": 50,       
        "group2": 55,            
        "it": 60,
        "group3": 56
        }

至-

words = {
        "the": 10,
        "group1": 54,
        "then": 40,
        "now": 50,                   
        "it": 60
        }

到目前为止,我有:

new_dict = {}    
words_dup = []

for word, value in words.items():
    words_dup.append(value)

for word, value in words.items():
    for item in words_dup:
        if item-value in range(-2,2): 
            words_dup.remove(item)
            new_dict[word] = value

我的方法很草率,只从dict中删除随机键值。有什么更好的方法可以解决此问题,也许不需要像上面一样创建新列表和字典。谢谢。

1 个答案:

答案 0 :(得分:0)

也许这更好?是O(n ^ 2),但我怀疑如果没有更复杂的数据结构,您会比这更好吗?

words = {
    "the": 10,
    "group1": 54,
    "then": 40,
    "now": 50,       
    "group2": 55,            
    "it": 60,
    "group3": 56
}

new_dict = {key: val for key, val in words.items() 
            if all(abs(val - v) > 2 for k, v in words.items() if k != key)}

print(new_dict)
# {'the': 10, 'then': 40, 'now': 50, 'it': 60}

如果需要保留最小的元素:

new_dict = {key: val for key, val in words.items()
            if all(not 0 < val - v <= 2 for k, v in words.items() if k != key)}
print(new_dict)
# {'the': 10, 'group1': 54, 'then': 40, 'now': 50, 'it': 60}