Python之路,Day03-處理結構化數據


本節內容:

1、元組操作

2、while 循環

3、字典操作

4、字典的嵌套

5、集合操作

6、訪問一個復雜的數據結構的數據

7、習題

 

1、元組(tuple)

https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

元組是一個一旦創建就不能改變的列表。任何情況下這個元組都不能再改變。

通常可以把元組想象成一個常量列表。

語法:

元組只有兩個方法,count和index。

eg:

1 a = (3,2,1,4,5,2,2,4,5,6,4)
2 print(type(a))
3 print(a.index(2))
4 print(a.count(2))

 

 

 1 >>> t = 12345, 54321, 'hello!'
 2 >>> t[0]
 3 12345
 4 >>> t
 5 (12345, 54321, 'hello!')
 6 >>> # Tuples may be nested:
 7 ... u = t, (1, 2, 3, 4, 5)
 8 >>> u
 9 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
10 >>> # Tuples are immutable:
11 ... t[0] = 88888
12 Traceback (most recent call last):
13   File "<stdin>", line 1, in <module>
14 TypeError: 'tuple' object does not support item assignment
15 >>> # but they can contain mutable objects:
16 ... v = ([1, 2, 3], [3, 2, 1])
17 >>> v
18 ([1, 2, 3], [3, 2, 1])
tuple

 

 

2、while 循環

有一種循環叫做死循環,一經觸發,就運行到天荒地老。

天荒地老代碼:

eg1:

1 count = 0
2 while True:
3     print("你是風兒我是沙,纏纏綿綿到天涯...",count)
4     count +=1

 

如何停止循環?

引入break:

eg2:

count = 0
while True:
    print("你是風兒我是沙,纏纏綿綿到天涯...",count)
    count +=1
    if count == 100:  #加入結束條件
        break               #跳出循環

 

猜數字游戲:

eg3:

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 #猜數字(0-100)
 4 
 5 
 6 import getpass
 7 hide_card = int(getpass.getpass("hide_card:"))
 8 count = 0
 9 
10 if hide_card <100 and hide_card >0:
11     while True:
12         if count <5:
13             guess_digit = int(input("guess_digit:"))
14             if guess_digit == hide_card:
15                 print("yes, you got it.")
16                 break
17             elif guess_digit > hide_card:
18                 print("think smaller...")
19             else:
20                 print("think bigger...")
21             count +=1
22         else:
23             print("猜這么多次都不對,你個笨蛋!")
24             print("正確答案:%s"%(hide_card))
25             break
26 else:
27     print("請輸入數字在0-99之間。不要耍賴!")
guess.py

 

任務(25分鍾)

來源Directory 2 課后習題:

  購物車程序項目:

  要求:1、運行程序后,讓用戶輸入支付寶余額,然后打印我們的商品列表給用戶。

       2、讓用戶輸入商品編號進行商品的購買。

       3、用戶選擇商品后,檢查用戶的余額是否夠,若夠則直接扣款,不夠則提醒用戶。

       4、用戶可以隨時退出購買,推出時打印用戶已購買的商品和支付寶余額。

 

3、字典操作(dict)

Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys(鍵), which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().

It is best to think of a dictionary as a set of key: value pairs(大腦P96,字典存儲鍵/值對), with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with del. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.

Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead). To check whether a single key is in the dictionary, use the in keyword.

(Operations Manager

參考:https://docs.python.org/3/tutorial/datastructures.html#dictionaries (python3.7.4)

 

字典是一種key - value 的數據類型

語法:

1 #Zhichao
2 
3 info = {
4     "stu1801":"ZiQi",
5     "stu1802":"XiaoFeng",
6     "stu1803":"LiuYu"
7 }
8 print(info)
 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 #創建字典的常用方法
 4 info = {
 5     "stu1801":"ZiQi",
 6     "stu1802":"XiaoFeng",
 7     "stu1803":"LiuYu"
 8 }
 9 
10 print(info)
11 
12 info2 = dict(stu1801="ZiQi",stu1802="XiaoFeng",stu1803="LiuYu")
13 print(info2)
14 
15 info3 = {}
16 info3["stu1801"]="ZiQi"
17 info3["stu1802"]="XiaoFeng"
18 info3["stu1803"]="LiuYu"
19 print(info3)
Data dict

 

特性:

  • dict 是無序的
  • key 是唯一的
  • key 不可變性

語法:

1 #Zhichao
2 
3 info = {
4     "stu1801":"ZiQi",
5     "stu1802":"XiaoFeng",
6     "stu1803":"LiuYu"
7 }
8 
9 print(info)

 

增加:

1 >>>info["stu1804"] = "Zhichao"
2 >>>print(info)
3 {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu', 'stu1804': 'Zhichao'}
View Code

修改:

1 >>>info["stu1804"] = "Wenjun"
2 >>>print(info)
3 {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu', 'stu1804': 'Wenjun'}
View Code

刪除:

 1 >>>print(info)
 2 {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu', 'stu1804': 'Wenjun'}
 3 >>>info.pop("stu1804") #標准刪除姿勢
 4 >>>print(info)
 5 {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}
 6 
 7 >>>del info["stu1801"] #換個姿勢刪除,del是不是python中通用的刪除方式呢。回顧list
 8 >>>print(info)
 9 {'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}
10 
11 #隨機刪除
12 
13 >>>info = {"stu1801":"ZiQi","stu1802":"XiaoFeng","stu1803":"LiuYu"}
14 >>>print(info)
15 {"stu1801":"ZiQi","stu1802":"XiaoFeng","stu1803":"LiuYu"}
16 >>>info.popitem()
17 >>>print(info)
18 {'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng'}
View Code

查找:

 1 >>>info = {"stu1801":"ZiQi","stu1802":"XiaoFeng","stu1803":"LiuYu"}
 2 >>>info
 3 {"stu1801":"ZiQi","stu1802":"XiaoFeng","stu1803":"LiuYu"}
 4 >>>print("stu1801" in info) #標准用法
 5 True
 6 >>>print(info.get("stu1802")) #獲取
 7 "XiaoFeng"
 8 >>>print(info["stu1802"]) #同上,但看下面
 9 "XiaoFeng"
10 >>>print(info["stu1805"]) #如果一個key不存在,就報錯,get不會,不存在只返回None
11 Traceback (most recent call last):
12   File "C:/Users/zhichao/PycharmProjects/untitled/day02/dict.py", line 12, in <module>
13     print(info["stu1805"])
14 KeyError: 'stu1805'
15 >>>print(info.get("stu1805"))
16 None
View Code

大腦:練習P111

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 vowels = ["a","e","i","o","u"]
 5 word = input("Provide a word to search for vowels:")
 6 found = {}
 7 
 8 # found["a"]=0
 9 # found["e"]=0
10 # found["i"]=0
11 # found["o"]=0
12 # found["u"]=0
13 found = found.fromkeys(vowels,0)
14 
15 for letter in word:
16     if letter in vowels:
17         found[letter] += 1
18 
19 # print(found.items())
20 for k,v in found.items():
21     print(k,"was found",v,"time(s)")
22 # print(found)
vowels4

大腦:練習P121(引入setdefault)

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 
 5 vowels = ["a","e","i","o","u"]
 6 word = input("Provide a word to search for vowels:")
 7 found = {}
 8 
 9 for letter in word:
10     if letter in vowels:
11         found.setdefault(letter,0)  #如果是元音字母,就在字典found中初始化這個元音字母,值為0
12         found[letter] +=1
13 for k,v in sorted(found.items()):
14     print(k,v)
vowels6

 

 

 

思考:為什么dict比list運行速度快 ?

引用:廖雪峰

1 >>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
2 >>> d['Michael']
3 95

為什么dict查找速度這么快?因為dict的實現原理和查字典是一樣的。假設字典包含了1萬個漢字,我們要查某一個字,一個辦法是把字典從第一頁往后翻,直到找到我們想要的字為止,這種方法就是在list中查找元素的方法,list越大,查找越慢。

第二種方法是先在字典的索引表里(比如部首表)查這個字對應的頁碼,然后直接翻到該頁,找到這個字,無論找哪個字,這種查找速度都非常快,不會隨着字典大小的增加而變慢。

dict就是第二種實現方式,給定一個名字,比如'Michael',dict在內部就可以直接計算出Michael對應的存放成績的“頁碼”,也就是95這個數字存放的內存地址,直接取出來,所以速度非常快。

 

4、字典的嵌套/訪問一個復雜數據結構的數據

大腦P138

eg:

 1 #Zhichao
 2 
 3 import pprint
 4 people = {}
 5 people['Ford'] = {"Name":'Ford Prefect',
 6                   "Gender":'Male',
 7                   "Occupation":'Researcher',
 8                   "Home Planet":'Betelgeuse Seven'}
 9 people['Arthur'] = {"Name":'Arthur Dent',
10                   "Gender":'Male',
11                   "Occupation":'Sandwich-Maker',
12                   "Home Planet":'Earth'}
13 people['Tricia'] = {"Name":'Tricia McMillan',
14                     "Gender":'Female',
15                     "Occupation":'Mathematician',
16                     "Home Planet":'Earth'}
17 people['Marvin'] = {"Name":'Marvin',
18                     "Gender":'Unknown',
19                     "Occupation":'Paranoid Android',
20                     "Home Planet":'Unknown'}
21 pprint.pprint(people)
View Code

 

任務:

https://azure.microsoft.com/zh-cn/services/cognitive-services/face/#detection

面部測試,獲取一張圖片的數據,通過訪問字典,查詢該面部數據的:

# 取內容:性別 gender

# 取內容:年齡

# 取內:有無眼睛

# 取內容:頭發狀況

# 取出hairColor中的color為red的confidence值。

  1  [
  2   {
  3     "faceId": "29867cfe-ba72-41a3-8407-57b7f110633b",
  4     "faceRectangle": {
  5       "top": 128,
  6       "left": 459,
  7       "width": 224,
  8       "height": 224
  9     },
 10     "faceAttributes": {
 11       "hair": {
 12         "bald": 0.1,
 13         "invisible": false,
 14         "hairColor": [
 15           {
 16             "color": "brown",
 17             "confidence": 0.99
 18           },
 19           {
 20             "color": "black",
 21             "confidence": 0.57
 22           },
 23           {
 24             "color": "red",
 25             "confidence": 0.36
 26           },
 27           {
 28             "color": "blond",
 29             "confidence": 0.34
 30           },
 31           {
 32             "color": "gray",
 33             "confidence": 0.15
 34           },
 35           {
 36             "color": "other",
 37             "confidence": 0.13
 38           }
 39         ]
 40       },
 41       "smile": 1.0,
 42       "headPose": {
 43         "pitch": -13.2,
 44         "roll": -11.9,
 45         "yaw": 5.0
 46       },
 47       "gender": "female",
 48       "age": 24.0,
 49       "facialHair": {
 50         "moustache": 0.0,
 51         "beard": 0.0,
 52         "sideburns": 0.0
 53       },
 54       "glasses": "ReadingGlasses",
 55       "makeup": {
 56         "eyeMakeup": true,
 57         "lipMakeup": true
 58       },
 59       "emotion": {
 60         "anger": 0.0,
 61         "contempt": 0.0,
 62         "disgust": 0.0,
 63         "fear": 0.0,
 64         "happiness": 1.0,
 65         "neutral": 0.0,
 66         "sadness": 0.0,
 67         "surprise": 0.0
 68       },
 69       "occlusion": {
 70         "foreheadOccluded": false,
 71         "eyeOccluded": false,
 72         "mouthOccluded": false
 73       },
 74       "accessories": [
 75         {
 76           "type": "glasses",
 77           "confidence": 1.0
 78         }
 79       ],
 80       "blur": {
 81         "blurLevel": "low",
 82         "value": 0.0
 83       },
 84       "exposure": {
 85         "exposureLevel": "goodExposure",
 86         "value": 0.48
 87       },
 88       "noise": {
 89         "noiseLevel": "low",
 90         "value": 0.0
 91       }
 92     },
 93     "faceLandmarks": {
 94       "pupilLeft": {
 95         "x": 504.8,
 96         "y": 206.8
 97       },
 98       "pupilRight": {
 99         "x": 602.5,
100         "y": 178.4
101       },
102       "noseTip": {
103         "x": 593.5,
104         "y": 247.3
105       },
106       "mouthLeft": {
107         "x": 529.8,
108         "y": 300.5
109       },
110       "mouthRight": {
111         "x": 626.0,
112         "y": 277.3
113       },
114       "eyebrowLeftOuter": {
115         "x": 461.0,
116         "y": 186.8
117       },
118       "eyebrowLeftInner": {
119         "x": 541.9,
120         "y": 178.9
121       },
122       "eyeLeftOuter": {
123         "x": 490.9,
124         "y": 209.0
125       },
126       "eyeLeftTop": {
127         "x": 509.1,
128         "y": 199.5
129       },
130       "eyeLeftBottom": {
131         "x": 509.3,
132         "y": 213.9
133       },
134       "eyeLeftInner": {
135         "x": 529.0,
136         "y": 205.0
137       },
138       "eyebrowRightInner": {
139         "x": 579.2,
140         "y": 169.2
141       },
142       "eyebrowRightOuter": {
143         "x": 633.0,
144         "y": 136.4
145       },
146       "eyeRightInner": {
147         "x": 590.5,
148         "y": 184.5
149       },
150       "eyeRightTop": {
151         "x": 604.2,
152         "y": 171.5
153       },
154       "eyeRightBottom": {
155         "x": 608.4,
156         "y": 184.0
157       },
158       "eyeRightOuter": {
159         "x": 623.8,
160         "y": 173.7
161       },
162       "noseRootLeft": {
163         "x": 549.8,
164         "y": 200.3
165       },
166       "noseRootRight": {
167         "x": 580.7,
168         "y": 192.3
169       },
170       "noseLeftAlarTop": {
171         "x": 557.2,
172         "y": 234.6
173       },
174       "noseRightAlarTop": {
175         "x": 603.2,
176         "y": 225.1
177       },
178       "noseLeftAlarOutTip": {
179         "x": 545.4,
180         "y": 255.5
181       },
182       "noseRightAlarOutTip": {
183         "x": 615.9,
184         "y": 239.5
185       },
186       "upperLipTop": {
187         "x": 591.1,
188         "y": 278.4
189       },
190       "upperLipBottom": {
191         "x": 593.2,
192         "y": 288.7
193       },
194       "underLipTop": {
195         "x": 597.1,
196         "y": 308.0
197       },
198       "underLipBottom": {
199         "x": 600.3,
200         "y": 324.8
201       }
202     }
203   }
204 ]
example

 

練習:多級地址菜單

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 data = {
 5     "北京":{
 6         "朝陽":{},
 7         "海淀":{},
 8         "昌平":{}
 9     },
10     "上海":{
11         "黃埔":{},
12         "浦東新區":{},
13         "虹口":{}
14     },
15     "廣東":{
16         "深圳":{
17             "羅湖":["深中華","世紀星源"],
18             "福田":["招商證券","深華發","深科技","深圳會展中心"],
19             "南山":["Tencent","深信服","TCL"],
20             "龍崗":["華為","中興通訊"]
21         },
22         "廣州":{},
23         "佛山":{},
24     },
25 }
26 
27 exit_flag = True
28 
29 while exit_flag:
30     for i in data:
31         print(i)   #打印第一層的key
32     choice = input("選擇輸入>>>:")
33     if choice in data:
34         while exit_flag:
35             for i2 in data[choice]:
36                 print("\t",i2)
37             choice2 = input("選擇輸入>>>:")
38             if choice2 == "back":
39                 break
40             elif choice2 == "exit":
41                 exit_flag = False
42             if choice2 in data[choice]:
43                 while exit_flag:
44                     for i3 in data[choice][choice2]:
45                         print("\t\t",i3)
46                     choice3 = input("選擇輸入>>>:")
47                     if choice3 == "back":
48                         break
49                     elif choice3 == "exit":
50                         exit_flag = False
51                     if choice3 in data[choice][choice2]:
52                             for i4 in data[choice][choice2][choice3]:
53                                 print("\t\t\t",i4)
54                             choice4 = input("這是最后一層,返回輸入'back':")
55                             if choice4 == "back":
56                                 pass
57                             elif choice4 == "exit":
58                                 exit_flag = False
59 
60     elif choice == "exit":
61         exit_flag = False
menus

 5、集合操作(sets)

 https://docs.python.org/3/tutorial/datastructures.html#sets (python3.7.4)

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

Here is a brief demonstration:

 1 >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
 2 >>> print(basket)                      # show that duplicates have been removed
 3 {'orange', 'banana', 'pear', 'apple'}
 4 >>> 'orange' in basket                 # fast membership testing
 5 True
 6 >>> 'crabgrass' in basket
 7 False
 8 
 9 >>> # Demonstrate set operations on unique letters from two words
10 ...
11 >>> a = set('abracadabra')
12 >>> b = set('alacazam')
13 >>> a                                  # unique letters in a
14 {'a', 'r', 'b', 'c', 'd'}
15 >>> a - b                              # letters in a but not in b
16 {'r', 'd', 'b'}
17 >>> a | b                              # letters in a or b or both
18 {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
19 >>> a & b                              # letters in both a and b
20 {'a', 'c'}
21 >>> a ^ b                              # letters in a or b but not both
22 {'r', 'd', 'b', 'm', 'z', 'l'}

練習:

 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 list_1 = [1,2,5,6,7,2,3,2,4,4]
 5 
 6 list_2 = set(list_1)
 7 list_3 = set([1,3,4,0,8,66,7])
 8 
 9 print("list_2:",list_2,"\n",type(list_2))
10 print("list_3:",list_3)
11 # 交集
12 print(list_2.intersection(list_3))
13 print(list_2 & list_3)
14 
15 # 並集
16 print(list_2.union(list_3))
17 print(list_2 | list_3)
18 
19 # 差集
20 print(list_2.difference(list_3))
21 print(list_2 - list_3)
22 
23 print(list_3.difference(list_2))
24 print(list_3 - list_2)
25 
26 # 子集
27 
28 print(list_2.issubset(list_3))  #子集
29 print(list_2.issuperset(list_3))  #父集
30 list_4 = set([2,3])
31 print(list_4.issubset(list_2))
32 
33 # 對稱差集
34 print(list_2.symmetric_difference(list_3))
set
 1 # -*- coding:utf-8 -*-
 2 # Author:Zhichao
 3 
 4 list_3 = set([1,3,4,0,8,66,7])
 5 list_3.pop()  # pop() 方法用於隨機移除一個元素。
 6 print(list_3)
 7 # list_3.clear() #清空
 8 # print(list_3)
 9 list_3.remove(3)
10 print(list_3)
11 list_3.add(5)
12 print(list_3)
13 #該方法不同於 remove() 方法,因為 remove() 方法在移除一個不存在的元素時會發生錯誤,而 discard() 方法不會。
14 list_3.discard(9)
15 print(list_3)
16 list_4 = list_3.copy()
17 print(list_4)
set2

大腦P129練習:

1 >>>vowels = set('aeiou')
2 >>>word = input("Provide a word to search for vowels:")
3 >>>found = vowels.intersection(set(word))
4 >>>for vowel in found:
5             print(vowel)
vowels7

 

7、習題

1、輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。

2、投票系統

班上投票競選,將選擇票數最高的同學擔任班長,請你設計一個投票系統,輸入名字即可投票,直到end結束投票,最后統計大家的得票數,公開投票結果,並宣布誰擔任班長。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM