如何在键值对中查找小于指定阈值的公钥



我有一个带有键值对的字典,我想将值的阈值设置为小于 50%,这基本上意味着在任何键值对中,值对的值小于所有值的 50%,我们应该将该键值对放在字典中,然后我们在字典中读取这些对并检查哪些键值影响阈值。

{('a','b'):2,('b','c'):4,('c','d'):6,('d','e'):8,('e','f'):8,('f','g'):3,('g','h'):2,('h','i'):7,(i,j):10}

正如您在上面的字典对中看到的,(a,b)(b,c)的值为 2 和 4,小于 50%,所以在这里我们可以说因为 b 在两者中都很常见,这就是为什么值小于 50% 的原因。所以我想打印 b 作为输出。在(f,g)(g,h)对的情况下相同,所以这里的输出也将是g。

所以我想要的最终输出是 - b,g

请帮助我是 Python 的新手...

如果你想从字典中获取具有类似不重复元组值的键,你可以先过滤掉大于 5 的键,然后链式计数,如果它们重复,你需要的工具都可以在 Python 的标准库中找到:

建立你的字典:

from collections import Counter
from itertools import chain
dic = {('a','b'):2,('b','c'):4,('c','d'):6,('d','e'):8,('e','f'):8,('f','g'):3,('g','h'):2,('h','i'):7}

使用列表推导式过滤掉:

less_5 = [k for k,v in dic.items() if v < 5]

计算重复键数:

counter = Counter(chain.from_iterable(less_5))
counter.most_common()
Output:
[('b', 2), ('g', 2), ('a', 1), ('c', 1), ('f', 1), ('h', 1)]

如果您真的想打印出来:

for k,v in counter.items():
if v > 1:
#only print if they key appears in 2 different keys
print(k)
Output:
b
g

编辑:OP 向申报人添加了 50% 的问题。

此外,计算值的阈值,并通过列表推导使用相同的筛选方法。

from collections import Counter
from itertools import chain
dic = {('a','b'):2,('b','c'):4,('c','d'):6,('d','e'):8,('e','f'):8,('f','g'):3,('g','h'):2,('h','i'):7,('i','j'):10}
thresh = max(v for v in dic.values())/2 #This sets the threshold at half of max
less_thresh = [k for k,v in dic.items() if v < thresh] #This filters keys less than thresh
counter = Counter(chain.from_iterable(less_thresh))

for k,v in counter.items():
if v > 1:
print(k)
Output:
b
g

以下是我解决这个问题的方法:

  1. 根据 50% 的阈值提取所有密钥
  2. 将所有提取的键合并到一个元组中
  3. 提取Set()中的所有重复字母(导致值小于阈值的字母(
def get_my_data(dictionary, threshold):
if 0 <= threshold <= 100:
threshold = (max([value for value in dictionary.values()])) * (threshold/100) # Sets threshold value from dictionary values
merged_keys = ()
for key, value in dictionary.items():
if value < threshold:
merged_keys += key
return set(letter for letter in merged_keys if merged_keys.count(letter) > 1)
else:
return f"Invalid threshold value: {threshold}, please enter a value between 0 to 100."

your_dictionary = {('a', 'b'): 2, ('b', 'c'): 4, ('c', 'd'): 6, ('d', 'e'): 8, ('e', 'f'): 8, ('f', 'g'): 3,
('g', 'h'): 2, ('h', 'i'): 7, ('i', 'j'): 10}
result = get_my_data(your_dictionary, 50)
print(result)

输出

{'g', 'b'}

可能有一种更性感的方法,但这种方法仍然有效,应该可以理解。

keys_under_threshold = set() 
duplicates = set() 
for key, val in d.items(): 
if val <= 5: 
if key[0] not in keys: 
keys_under_threshold.add(key[0]) 
else: 
duplicates.add(key[0]) 
if key[1] not in keys: 
keys_under_threshold.add(key[1]) 
else: 
duplicates.add(key[1]) 
print(duplicates)

首先创建包含带有值的字母的集合,然后迭代此集合以获得所需的结果。

data = {('a','b'):2,('b','c'):4,('c','d'):6,('d','e'):8,('e','f'):8,('f','g'):3,('g','h'):2,('h','i'):7}
# collect letters with values
collection = {}
for key, value in data.items():
collection.setdefault(key[0], []).append(value)
collection.setdefault(key[1], []).append(value)
# get desired results
for key, value in collection.items():
if len(value) > 1 and all( i < 5 for i in value):
print(key)

输出:

b
g

最新更新