字典 dict.items()
[root@kube python]# cat demo.py #coding:utf-8 drinks = { 'martini': {'vodka', 'vermouth'}, 'black russian': {'vodka', 'kahlua'}, 'white russian': {'cream', 'kahlua', 'vodka'}, 'manhattan': {'rye', 'vermouth', 'bitters'}, 'screwdriver': {'orange juice', 'vodka'} } print(drinks.items()) #drinks.items() 获取所有键值对 ,name 为key ,contexts 为 values for name, contexts in drinks.items(): if 'vodka' in contexts: print(name) [root@kube python]#
list tuple ,dict 的变化
[root@kube review]# cat demo1.py marxes = ['Groucho', 'Chico', 'Harpo'] pythons = ['Chapman', 'Cleese', 'Gilliam', 'Jones', 'Palin'] stooges = ['Moe', 'Curly', 'Larry'] lt = [marxes, pythons,stooges] print(lt) tu = marxes,pythons,stooges print(tu) dt = {'marxes':marxes, 'pythons':pythons,'stooges':stooges} print(dt) [root@kube review]# py demo1.py [['Groucho', 'Chico', 'Harpo'], ['Chapman', 'Cleese', 'Gilliam', 'Jones', 'Palin'], ['Moe', 'Curly', 'Larry']] (['Groucho', 'Chico', 'Harpo'], ['Chapman', 'Cleese', 'Gilliam', 'Jones', 'Palin'], ['Moe', 'Curly', 'Larry']) {'marxes': ['Groucho', 'Chico', 'Harpo'], 'pythons': ['Chapman', 'Cleese', 'Gilliam', 'Jones', 'Palin'], 'stooges': ['Moe', 'Curly', 'Larry']} [root@kube review]#
字典推导式
除了列表,字典也有自己的推导式。最简单的例子就像:
{ key_expression : value_expression for expression in iterable }
[root@kube python]# cat demo1.py # 1 word = 'letters' letter_counts = {letter: word.count(letter) for letter in word} print(letter_counts) #2 for letter in word: print(letter,':',word.count(letter)) [root@kube python]# py demo1.py #使用字典推导式和for 循环的区别就是,字典活去除相同键值的key:value 值 {'l': 1, 'e': 2, 't': 2, 'r': 1, 's': 1} l : 1 e : 2 t : 2 t : 2 e : 2 r : 1 s : 1 [root@kube python]#
函数
[root@kube review]# cat demo4.py #coding:utf-8 def commentary(color): if color == 'red': return 'It`s a tomato' elif color == 'green': return 'this a green pepper' else: return 'it`s no heaed' comm1 = commentary('red') comm2 = commentary('green') comm3 = commentary('black') print(comm1) print(comm2) print(comm3) [root@kube review]# py demo4.py It`s a tomato this a green pepper it`s no heaed [root@kube review]#
函数之内部函数
[root@localhost python]# cat demo1.py #coding:utf-8 #内部函数的作用是给外部的函数增加字符串 参数: #相当于将参数传递给 inner 函数并且返 def outer(a,b): def inner(aa,bb): return aa * bb return inner(a,b) print(outer(33,44)) [root@localhost python]# py demo1.py 1452 [root@localhost python]#
函数之闭包
[root@localhost python]# cat demo2.py #coding:utf-8 #内部函数可以看作一个闭包。闭包是一个可以由另一个函数动态生成的函数,并且可以改 变和存储函数外创建的变量的值。 # inner() 函数是一个闭包,存储了 test(saying) 函数的值。 def test1(saying): def inner(): return 'we are inner function : %s ' % saying return inner #返回的是一个函数 a = test1('hello word') print(a()) [root@localhost python]# py demo2.py we are inner function : hello word [root@localhost python]#
装饰器
[root@localhost python]# cat demo5.py #coding:utf-8 def document_it(func): def new_function(*args, **kwargs): print('Running function:', func.__name__) print('Positional arguments:', args) print('Keyword arguments:', kwargs) result = func(*args, **kwargs) print('Result:', result) return result return new_function @document_it #将add_ints 作为参数传递给 document_it 装饰器,这意味着 add_ints() 被替换成了 new_function() 函数,如果要执行 add_ints() 的值就需要 def add_ints(a,b): #在 new_function() 函数中执行 fun(*args,**kwargs),修饰器就将被修饰的函数作为参数传递给修饰器 return a + b add_ints(3,5) [root@localhost python]# py demo5.py Running function: add_ints Positional arguments: (3, 5) Keyword arguments: {} Result: 8 [root@localhost python]#