我有一个带有键值对的字典,我想将阈值设置为小于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}
正如你可以在上面dictonary对看(a,b)
和(b,c)
有值2和4,所以在这里我们可以说,由于B是两种常见这就是为什么值小于50%。所以我想打印B中output.Same小于50%如果是(f,g)
and (g,h)
对,那么这里的输出也是g。
所以我想要的最终输出是-b,g
请帮助我是Python的新手...
这是我解决此问题的方法:
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'}
我想将阈值设置为小于50%,这基本上意味着它应该首先从“键值对”中读取所有值,然后从“值对”中找到“当前方案”中为10的最高值,然后在该值上将阈值设置为小于50% 。我也更新了同样的问题。
@Ani我已经编辑了答案。我创建了一个简单的函数,该函数接受字典和阈值作为参数。它还检查阈值是否在0到100%之间。
您能解释一下您在这里做什么吗?