аватар question@mail.ru · 01.01.1970 03:00

How to get all the keys with the same values?

There is such a dictionary: mydict = {'Main': 'Open', 'Second': 'Close', 'Third': 'Open'} . It is necessary to get all the keys to the values ​​of 'open'

. Class = ""> 'Main' : 'open' , 'second' : 'clause' , 'Third' : 'open' } print ( list (mydict.keys ()) [ List (mydict.values ​​()). Index ( 'open' )])

it displays only main . How to display all the keys with the meaning of 'Open' ?

аватар answer@mail.ru · 01.01.1970 03:00

Lea list inclusions , they very often come in handy in python, because they allow you to make samples quite briefly and clearly:

  MyDict = { 'Main' :  'open' ,  'second' :  'clause' ,  'Third' :  'open' }   print  ([k  for  k  in  in  MyDICT  if  mydict [k] ==  "open" "])  

conclusion:

  [ 'main' ,  'third' ]   

this is essentially the same solution, only written into one line and without using a variable for accumulating to accumulate List.

Latest

Similar