Home > Software design >  Compare multiple value in a dictionary and get bigger value key
Compare multiple value in a dictionary and get bigger value key

Time:05-27

How can I compare that 2 keys with the highest length of the value? I wanna found the highest key name which has a bigger length.

{
'UFTBUSD': ['Hammer', 'Belthold', 'Doji', 'Three Outside', 'Hanging Man', 'Closing Marubozu', 'Dragonfly Doji', 'Engulfing Pattern', 'Gravestone Doji', 'Harami Pattern'], 
'ANCBUSD': ['Belthold', 'Doji', 'Three Outside', 'Shooting Star', 'Closing Marubozu', 'Engulfing Pattern', 'Harami Pattern']
}

CodePudding user response:

thisdict = {
    'UFTBUSD': ['Hammer', 'Belthold', 'Doji', 'Three Outside', 'Hanging Man', 'Closing Marubozu', 'Dragonfly Doji', 'Engulfing Pattern', 'Gravestone Doji', 'Harami Pattern'], 
    'ANCBUSD': ['Belthold', 'Doji', 'Three Outside', 'Shooting Star', 'Closing Marubozu', 'Engulfing Pattern', 'Harami Pattern']
    }
    
def find_longest_key(mydict):
    count_value = 0
    for x, y in mydict.items():
      for value in y:
        if len(value) > count_value:
            count_value  = len(value)
            print(len(value))
            longest_value = value
            longest_values_key = x
    print(longest_value)
    print(longest_values_key)
    return longest_values_key
    
    
find_longest_key(thisdict)

CodePudding user response:

thisdict = {
    'UFTBUSD': ['Hammer', 'Belthold', 'Doji', 'Three Outside', 'Hanging Man', 'Closing Marubozu', 'Dragonfly Doji', 'Engulfing Pattern', 'Gravestone Doji', 'Harami Pattern'], 
    'ANCBUSD': ['Belthold', 'Doji', 'Three Outside', 'Shooting Star', 'Closing Marubozu', 'Engulfing Pattern', 'Harami Pattern']
    }

def find_largest_key(pattern_with_symbol):
   count = 0
   for key, value in pattern_with_symbol.items():
      if len(value) > count:
         count = len(value)
         long_key = key
   return long_key

print(find_largest_key(this_dict))
  • Related