Python教程

Python: value => key

本文主要是介绍Python: value => key,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  1. list derivatives & dict deconstruction
    ebb = dict(zip('abcde', (11, 22, 11, 33, 22)))
    
    print(ebb)
    
    print([k for k, v in ebb.items() if v == 11])

     

  2. filter
    ebb = dict(zip('abcde', (11, 22, 11, 33, 22)))
    
    print(ebb)
    
    print(dict(filter(lambda item: item[1] == 11, ebb.items())))

     

  3. dict.keys() and dict.values() have the corresponding index
    ebb = dict(zip('abcde', (11, 22, 11, 33, 22)))
    
    print(ebb)
    
    print(tuple(ebb.keys())[tuple(ebb.values()).index(11)])

     

  4. dict derivatives and deconstruction
    ebb = dict(zip('abcde', (11, 22, 11, 33, 22)))
    
    print(ebb)
    
    print({v: k for k, v in ebb.items()}[11])

     

这篇关于Python: value => key的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!