0

What does {digit:string for (string,digit) in operandi.items()} do in the code below?

operandi =   {'zero':0, 'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six':6, 'seven':7, 'eight':8, 'nine':9}
operatori = {'plus': lambda x,y: x+y, 'minus': lambda x,y: x-y, 'times': lambda x,y: x*y, 'divided by': lambda x,y: x/y}

text = input('What should I compute?')

tokens = text.split()

op1 = operandi[tokens[0]]
op = operatori[tokens[1]]
op2 = operandi[tokens[2]]

result = op(op1, op2)

rev_operandi = {digit:string for (string,digit) in operandi.items()}
print(rev_operandi[result])
sshashank124
  • 31,495
  • 9
  • 67
  • 76
mid
  • 1
  • You should try it out with a simple dictionary. Should be pretty quick to check – sshashank124 Jan 04 '20 at 10:24
  • 1
    Switches key and value pairs in the `operandi` dictionary. – Péter Leéh Jan 04 '20 at 10:24
  • You seem to believe that `digit` is a method, but it's just a variable. `digit: string` in the dictionary comprehension simply uses the value of the variable `digit` as the key and the value of `string` as its value in the generated dictionary. – tripleee Jan 04 '20 at 10:26

0 Answers0