Home python Get key by value

Get key by value

Author

Date

Category

There is a dictionary:

d = {'a': '1', 'b': '2', 'c': ' 3 '}

is the line:

stroka = 'a3a2c'

If a string element is among the dictionary values, output the key corresponding to the value. If among the keys, then output the value by the key value. It must be printed in two lines, the values ​​are separate from the keys.

That is. for line 'a3a2c' expected output '113' and 'cb'

How to display values ​​by key – I figured it out:

for i in stroka:
  if i in d.keys ():
    print (d [i], end = '')

But I can’t deduce the key by value:

for j in stroka:
  if j in d.values ​​():
    print (?????, end = '')

Answer 1, authority 100%

Something like this:

d = {
  eleven',
  '2': 2,
  3: '3',
}
def get_key (d, value):
  for k, v in d.items ():
    if v == value:
      return k
print (get_key (d, '1'))
print (get_key (d, 2))
print (get_key (d, 42))

Console:

1
2
None

In general, when I need to get a value by key and a key by value, I create two dictionaries.


Answer 2, authority 31%

Ie for line ‘a3a2c’ expected output ‘113’ and ‘cb’

If there is a d dictionary and a keys collection of keys, then to get the corresponding values ​​using the default values ​​for the missing keys:

def get_values ​​(d, keys, default = None):
  return (d.get (k, default) for k in keys)

Example:

d = {'a': '1', 'b': '2', 'c': ' 3 '}
s = 'a3a2c'
print (''. join (get_values ​​(d, s, ''))) # - & gt; 113
inv_d = dict (zip (d.values ​​(), d.keys ()))
print (''. join (get_values ​​(inv_d, s, ''))) # - & gt; cb

If you want to immediately filter out missing keys:

def get_existing_values ​​(d, keys):
  return filter (None, map (d.get, keys))

Example:

& gt; & gt; & gt; '' .join (get_existing_values ​​(inv_d, s))
'cb'

Answer 3, authority 8%

d = dict (a = '1', b = '2', c = '3') # first dictionary
print (d)
stroka = 'a3a2c'
print ('stroka =', stroka)
for m in stroka: # search for elements from stroka in keys d
  if m in d.keys (): # output the values ​​of these keys
    print ('letter', m, 'from line "stroka" is the value key', d [m])
s = []
for i in d.keys (): # second
  s.append ((d [i], i)) # dictionary
b = dict (s) # is the inverse of the first
            # values ​​are keys and keys are values
stroka_ = stroka
for i in stroka_: # search for elements from stroka in d values
  if i in b.keys (): # output the keys of these values
    print ('digit', i, 'from string "stroka" is the key value', b [i])

But the task would be a little more interesting if the representation of the dictionary values ​​were not lowercase, but integer.


Answer 4, authority 8%

For dictionary {‘a’: 1, ‘b’: 2, ‘c’: 3}
(dictionary values ​​are elements of type int)

d = dict (a = 1, b = 2, c = 3) # first dictionary
print (d)
stroka = 'a3a2c'
print ('stroka =', stroka)
for m in stroka: # search for elements from stroka in keys d
  if m in d.keys (): # output the values ​​of these keys
    print ('letter', m, 'from line "stroka" is the value key', d [m])
s = []
for i in d.keys (): # second
  s.append ((d [i], i)) # dictionary
b = dict (s) # is the inverse of the first
            # values ​​are keys and keys are values
stroka_ = stroka #stroka_ = 'a3a2c'
      # find elements from stroka in d values
          # output the keys of these values
v = ','. join (str (b.keys ()))
# v = d, i, c, t, _, k, e, y, s, (, [ 1 , 2 , 3,],)
s _ = []
for i in v:
  try:
    s_.append (int (i))
  except ValueError as e:
    continue
# s_ = [1, 2, 3]
i = 0
while i & lt; len (s_):
  s_ [i] = str (s_ [i])
  i + = 1
# s_ = ['1', '2', '3']
 # find elements from stroka in d values
          # output the keys of these values
for i in stroka_: #stroka_ = 'a3a2c'
  if i in s_: # s_ = ['1', '2', '3']
    print ('value', stroka_ [stroka_.index (i)], 'from the line "stroka" is the key value', b [int (i)])

Answer 5, authority 8%

for key in a:
  print ("% s - & gt;% s"% (key, a [key])) # python3

Answer 6

solved your problem a little differently.

just turn the dictionary keys into tuple with the list () method and store it in a new variable, then call the key values ​​and store it in a variable, and at the end you can already

print (variable_1 [index of the key you saved], variable_2 [index of the key]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions