2

I have a dictonary with key value pair where I want to set threshold as less than 50% for value which basically means in any of the key value pair where value pair has value less than 50% of all values we should put that key value pair in a dictonary and afterwards we have read those pair in a dictonary and check which key values are affecting the threshold.

{('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}

As you can see in above dictonary pair (a,b) and (b,c) have value 2 and 4 which is less than 50% so here we can say because b is common in both thats why value is less than 50%.So I want to print b as output.Same in case of (f,g) and (g,h) pair so here also output will be g.

So the final output which I want is-- b,g

Kindly help I am new to Python...

Ani
  • 147
  • 2
  • 14

4 Answers4

2

If you want to get keys from a dictionary which have similar not-repeated tuple values, you can first filter out the ones bigger than 5, then chain and count if they repeat, the tools you need are all available within Python's standard libraries:

Build your dictionary:

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}

Filter out using a list comprehension:

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

Count repeated keys:

counter = Counter(chain.from_iterable(less_5))
counter.most_common()

Output:
[('b', 2), ('g', 2), ('a', 1), ('c', 1), ('f', 1), ('h', 1)]

If you really want to print them out:

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

Output:
b
g

Edit: OPs added question to filer out 50%.

Additionally compute your threshold fro the values and use the same filter method through the list comprehension.

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
BernardL
  • 5,162
  • 7
  • 28
  • 47
  • I want to set Threshold value less than 50% Which basically means it should first read all the values from Key Value Pair and Find Highest value from Value pair which is 10 in Current Scenario and then on that value it should set Threshold less than 50%. I also updated the Question for the Same. – Ani Jan 31 '20 at 20:19
1

Here's how I approached this problem:

  1. Extract all the keys based on the threshold of 50%
  2. Merge all the extracted keys into a single tuple
  3. Extract all the duplicate letters (i.e. the letters which caused the value to be less than the threshold) in a 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)

Output

{'g', 'b'}

Chandral
  • 448
  • 1
  • 3
  • 19
  • I want to set Threshold value less than 50% Which basically means it should first read all the values from Key Value Pair and Find Highest value from Value pair which is 10 in Current Scenario and then on that value it should set Threshold less than 50%. I also updated the Question for the Same. – Ani Jan 31 '20 at 20:18
  • @Ani I have edited my answer. I created a simple function that accepts a dictionary and a threshold value as parameters. It also checks if the threshold is between 0 to 100% or not. – Chandral Feb 01 '20 at 06:28
  • Would you please explain what you are doing here? – Ani Feb 03 '20 at 07:20
0

There is probably a sexier method but this one works nonetheless and should be fairly understandable.

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)
Plopp
  • 947
  • 8
  • 16
  • I want to set Threshold value less than 50% Which basically means it should first read all the values from Key Value Pair and Find Highest value from Value pair which is 10 in Current Scenario and then on that value it should set Threshold less than 50%. I also updated the Question for the Same. – Ani Jan 31 '20 at 20:18
0

First create collection that contain letters with they values, then iterate over this collection to get desired result.

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)

Output:

b
g
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • I want to set Threshold value less than 50% Which basically means it should first read all the values from Key Value Pair and Find Highest value from Value pair which is 10 in Current Scenario and then on that value it should set Threshold less than 50%. I also updated the Question for the Same. – Ani Jan 31 '20 at 20:15