1

Here are some tests:

Input: "zero nine five two"

Output: "four"

Input: "four six two three"

Ouput: "three"

Here is my code, which works until the last step where I need to inverse lookup key by value, which I dont know how to do. Any advice?

def average_string(s):
    num_dic = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 
    'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}
    str_split = s.split()
    int_values = []
    for key, val in num_dic.items():
        if key in str_split: 
            int_values.append(val)
            int_avg = int(sum(int_values) / len(int_values))
    return int_avg
Andy G
  • 19,232
  • 5
  • 47
  • 69
Mr. Jibz
  • 511
  • 2
  • 7
  • 21
  • Duplicate of: https://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping – nanojohn Jul 27 '17 at 22:03
  • Also you can calculate your average with following code: `int(sum([num_dic[s] for s in str_split]) / len(str_split))` – Kedrzu Jul 27 '17 at 22:06
  • Do you ever expect to have a word of a digit get input into your function twice? lfor example: `"two zero nine five two"` – jacoblaw Jul 27 '17 at 22:07
  • To perform a lookup, rather than attempting to reverse the entire dictionary, I would use items() or iteritems() as answered [here](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary). That is, unless there is future value in switching the keys,values. – Andy G Jul 27 '17 at 22:07
  • Possible duplicate of [Get key by value in dictionary](https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary) – Andy G Jul 27 '17 at 22:09
  • Just return `return {v: k for k, v in num_dic.items()}[int_avg]` instead of `int_avg`, note dictionaries are not intended to work this way though. – Vinícius Figueiredo Jul 27 '17 at 22:12
  • Just because OP asked about reversing a dict doesn't mean it's the best solution for him. This question shouldn't be marked as a duplicate. – Sunny Patel Jul 27 '17 at 22:18

4 Answers4

1

You can try this:

num_dic = {'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6, 'seven': 7, 'eight': 8, 'nine': 9}

s = "zero nine five two"

new_dict = {b:a for a, b in num_dic.items()}

average = sum(num_dic[i] for i in s.split())/float(len(s.split()))

final_average = new_dict[average]

Output:

four
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

Why not just utilize a list of words to represent the numbers where the index represents their respective value?

def average_string(s):
    num_list = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
    str_split = s.split()
    int_sum = sum(map(num_list.index, str_split))
    return num_list[int_sum / len(str_split)]

The map goes over each element in str_split and translates it into the numeric version by calling num_list.index() using each element as a parameter.

Finally using the same num_list, use the average value as the index to get the string version back.

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • How about `return num_list[int_sum // len(str_split)]` (or worse, `return num_list[int(round(1.0 * int_sum / len(str_split)))]`) to allow it to work in both Python2 and Python 3 since OP didn't specify a version. – cdlane Jul 28 '17 at 06:42
0

Here is how you inverse a dictionary. This is a duplicated question though:

inv_dic = {v: k for k, v in num_dic.items()}
nanojohn
  • 572
  • 1
  • 3
  • 13
0

As far as I know there isn't a way to lookup in a dict by value. Instead you can loop over the dict to find the correct value like this:

for text, number in num_dic.iteritems():
    if number == int_avg:
        print text

Here is another question where you can find this: Get key by value in dictionary

You can use num_dic.items() in Python 3

Colin Maxfield
  • 162
  • 1
  • 1
  • 12