python05- dictionary derivation, set, set a fixed, nested list comprehension, custom functions

First, the derivation Dictionary

1  "" " 
2      Dictionary derivation
 3      Exercise: exercise01.py
 . 4      Exercise: exercise02.py
 . 5  " "" 
. 6  # 1 2 3 ... 10. 4 -> square 
. 7 dict01 = {}
 . 8  for Item in Range (1, . 11 ):
 . 9      dict01 [Item] Item ** 2 =
 10  Print (dict01)
 . 11  # derivation formula: 
12 is dict02 = {Item: Item 2 **
 13 is            for Item in Range (. 1,. 11 )}
 14  Print (dict02)
 15  
16  #The figures only recording of greater than 5 
. 17 dict01 = {}
 18 is  for Item in Range (. 1,. 11 ):
 . 19      IF Item> 5 :
 20 is          dict01 [Item] Item ** 2 =
 21 is  
22 is  Print (dict01)
 23 is  
24 dict02 = {Item : Item 2 **
 25            for Item in Range (. 1,. 11) IF Item>. 5 }
 26 is  Print (dict02)

Second, the collection

1  "" " 
2      set SET
 . 3      Exercise: exercise03
 . 4      Exercise: exercise04
 . 5  " "" 
. 6  # 1. Create a set 
. 7 set01 = SET ()
 . 8  # SET -> STR 
. 9 set01 = SET ( " abcac " )
 10 list01 = List (set01)
 . 11 str01 = "" .join (list01)
 12 is  Print (str01)   # "BCA" 
13 is  # Create a set with default values 
14 set02 = { " a " ,"b" , " A " }
 15  
16  # 2. additive elements 
. 17 set02.add ( " QTX " )
 18 is  
. 19  # 3. Remove element 
20 is set02.remove ( " A " )
 21 is  
22 is  # 4. get all the elements 
23 is  for Item in set02:
 24      Print (Item)
 25  
26 is  # 5. The mathematical operation 
27 set01 = {. 1, 2,. 3 }
 28 set02 = {2,. 3,. 4 }
 29  # intersection
30  Print (& set01 set02)   # {2,3} 
31 is  # union 
32  Print (set01 | set02)   # {. 1, 2,. 3,. 4} 
33 is  # complement 
34 is  Print (set01 set02 ^)   # {. 1,. 4 } 
35  Print (set01 - set02) # {}. 1 
36  Print (set02 - set01) # {}. 4 
37 [  
38 is  # subset 
39 set03 = {. 1, 2 }
 40  Print (set03 < set01)
 41 is  # superset 
42 is  Print(set01 > set03)

Third, a fixed set of

1 """
2     固定集合
3 """
4 set01 = frozenset([1, 2, 3, 3, 5])
5 list02 = list(set01)
6 print(set01)
7 print(list02)

四、列表推导式嵌套

 1 """
 2     列表推导式嵌套
 3     练习:exercise09.py
 4     16:55
 5 """
 6 list01 = ["a", "b", "c"]
 7 list02 = ["A", "B", "C"]
 8 list03 = []
 9 for r in list01:
10     for c in list02:
11         list03.append(r + c)
12 
13 print(list03)
14 
15 list04 = [r + c for r in list01 for c in list02]
16 print(list04)
17 
18 # 练习:列表的全排列
19 # [“香蕉”,"苹果","哈密瓜"]
20 # [“可乐”,"牛奶"]
21 list01 = ["香蕉","苹果","哈密瓜"]
22 list02 = ["可乐","牛奶"]
23 list03 = []
24 for r in list01:
25     for c in list02:
26         list03.append(r+c)
27 list04 = [r+c for r in list01 for c in list02]
28 print(list03)
29 print(list03)

字典内嵌列表内存图

五、自定义函数

 1 """
 2     自定义函数
 3     练习:exercise10.py
 4     练习:exercise11.py
 5 """
 6 
 7 """
 8 print("直拳")
 9 print("摆拳")
10 print("肘击")
11 print("临门一脚")
12 #...............
13 print("直拳")
14 print("摆拳")
15 print("肘击")
16 print("临门一脚")
17 #...............
18 print("直拳")
19 print("摆拳")
20 print("肘击")
21 print("临门一脚")
22 """
23 
24 
25 # 定义(做功能)函数
26 def attack():
27     """
28         单次攻击 
29     """
30     print("临门一脚")
31     print("直拳")
32     print("摆拳")
33     print("肘击")
34 
35 
36 # 形式参数
37 def attack_repeat(count):
38     """
39         重复攻击
40     :param count: 攻击次数,int类型
41     """
42     for i in range(count):
43         print("临门一脚")
44         print("直拳")
45         print("摆拳")
46         print("肘击")
47 
48 
49 # 调用函数
50 attack()
51 # ...............
52 # 调用函数
53 attack()
54 # ...............
55 # 调用函数
56 attack()
57 print("--------------")
58 # 调用函数
59 # 实际参数
60 attack_repeat(2)

方阵置换算法图

Guess you like

Origin www.cnblogs.com/libotao/p/12391867.html