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))

Python之路,Day03-处理结构化数据_第1张图片

 

 

 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 "", line 1, in 
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 
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结束投票,最后统计大家的得票数,公开投票结果,并宣布谁担任班长。

你可能感兴趣的:(Python之路,Day03-处理结构化数据)