In [1]:varibal = {
'a':100, 'b':100, 'c':200 }
In [2]:varibal['a']
Out[2]:100
In [3]:varibal.items()
Out[3]:dict_items([('c', 200), ('b', 100), ('a', 100)])
In [4]:[key for key,value in varibal.items() if value == 100]
Out[4]:['b', 'a']
函数-抽象概念
In [5]:def get_keys(dict_varibal, value): return [k for k, v in dict_varibal.items() if v == value] In [6]:get_keys(varibal,200) Out[6]:['c']
函数是组织好的,可重复使用的,能够完成特定功能的代码块,它是代码块的抽象
In [7]: get_keys( {'a':40}, 40)
Out [7]: ['a']
def get_keys(dict_varibal, value):
return [k for k, v in dict_varibal.items() if v == value]
-get_keys 函数名
-()中为参数,dict_varibal:形参,调用的时候传递的值才是实参
-return 返回值
1-位置参数
2-关键字参数,可以按照顺序去写
位置参数是不可以交换位置的
In [8]:get_keys(40, {'a':40})
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-20-0b500268442c> in <module>() ----> 1 get_keys(40, {'a':40}) <ipython-input-15-2a83166bc68e> in get_keys(dict_varibal, value) 1 def get_keys(dict_varibal, value): ----> 2 return [k for k, v in dict_varibal.items() if v == value] AttributeError: 'int' object has no attribute 'items'
关键字参数
In [9]:get_keys(dict_varibal={'a':40},value=40)
Out[9]:['a']
In [10]:get_keys(value=40, dict_varibal={'a':40})
Out[10]:['a']