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

Get a key

"

there is a dictionary:

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

there is a string:

  Stroka =  '"a3a2c'    

if the element is among the dictionary values, it is necessary to derive the key value, if among the keys - then among the keys - then among the keys - then among the keys - then among the keys - then among the keys. Display the value of the key to two lines, values ​​separately from the keys.

i.e. I figured out:

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

but I can’t display the key by value:

   for  j  in  stroka:   if  j  in  d.values ​​():   print  '' )     

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

"

something like this:

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

console:

   1    2    none      

in general, when I need to get the key and key key, I conquer two dictionaries.

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

obtaining a symbol by its number from Unicode:

   & gt; & gt; & gt;   chr  ( 65 )   'a' a '   chr  ( 122 )   'z'    & gt; & gt; & gt;   print  ( 128522 )) ?    

is better instead of instead chr use unichr

receipt of the number from the unicode by the symbol:

   & gt; & gt; & gt;   Ord  ( 'a' )   65    Ord  ( 'z' )   122    & gt; & gt; & gt;   Ord  ( '?' )   128522      

in pre -prepared Latin (and not only) symbols in the symbols. The string :

   & gt; & gt; & gt;   Import  string  & gt; & gt; & gt;  string.ascii_uppercase  '"abcDephhijklmnopqrsstuvwxyz'    & gt; & gt; & gt;  string.ascii_lowercase  '"abcodefghijklmnopqrsstuvwxyz'    & gt; & gt; & gt;  string.Askii_letters  '"abcDefhijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'    

It is a cycle:

   for  c  in  string.ascii_letters:   print  (c)     

in all generator expressions and conditions, of course, too:

  & gt; & gt; & gt;   Import  string  & gt; & gt; & gt;  s =  '' foo, take the barotsudbazinlatinskoye '   & gt; & gt; & gt;  s2 =  ''  .join (c  in  s  if  in  in  string.ascii_letters)   & gt; & gt; & gt;   print  (s2) foobarbaz                                      

Latest

Similar