# Python-week06 **Repository Path**: lin_shiying/python-week06 ## Basic Information - **Project Name**: Python-week06 - **Description**: No description available - **Primary Language**: Unknown - **License**: AFL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-11-01 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Python-week06 ## 一、字典和集合的用法总结 #### 1.1字典 字典是一种key-value的数据类型,又叫做键/值对。 字典的每个键值(key-value)对用冒号(:)分割,每个键值对之间用逗号(,)分割,整个字典包括在括号{}中,格式如下所示: dic = {key1 : value1, key2 : value2 } - 特性:字典是无序的,键一般是唯一的,如果重复最后的一个键值对会替换前面的,值不需要唯一。 值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。 #### 1.2字典的访问 把相应的键放入括弧{},如下所示: ``` dict = {'Name': 'Chrystal', 'Age': 26, 'Birthday': '1994.10.24'} ``` 如果用字典里没有的键访问数据,会输出错误,例如: ``` dict = {'Name': 'Chrystal', 'Age': 26, 'Birthday': '1994.10.24'} print ("dict['Alice']: ", dict['Alice']) ``` 输出结果为: ``` dict['Alice']: Traceback (most recent call last): File "test.py", line 5, in print "dict['Alice']: ", dict['Alice'] KeyError: 'Alice' ``` #### 1.3字典的修改 ##### 1.3.1增加 向字典添加新内容的方法是增加新的键/值对,修改或删除已有键/值对,示例如下: ``` dict = {'Name': 'Chrystal', 'Age': 26, 'Birthday': '1994.10.24'} dict['Hometown'] = "USA" # 添加 print ("dict['Hometown']: ", dict['Hometown']) ``` ``` dict['Hometown']: USA ``` ##### 1.3.2删除 可以只删单一的元素,也能直接清空字典,清空只需一项操作。 删除一个元素用del命令,如下所示: ``` dict = {'Name': 'Chrystal', 'Age': 26, 'Birthday': '1994.10.24'} del dict['Name'] # 删除键是'Name'的条目 dict ``` ``` {'Age': 26, 'Birthday': '1994.10.24'} ``` 直接清空字典: ``` dict.clear() dict ``` ``` {} ``` #### 1.4字典内置函数&方法 ##### 1.4.1内置函数 序号|函数及描述 ---|:--:|---: 1|cmp(dict1, dict2),比较两个字典元素。 2|len(dict),计算字典元素个数,即键的总数. 3|str(dict),输出字典可打印的字符串表示。 4|type(variable),返回输入的变量类型,如果变量是字典就返回字典类型。 ##### 1.4.2内置方法 序号|函数及描述 ---|:--:|---: 1|dict.clear(),删除字典内所有元素. 2|dict.get(key, default=None),返回指定键的值,如果值不在字典中返回default值. 3|dict.items(),以列表返回可遍历的(键, 值) 元组数组. 4|pop(key[,default]),删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 否则,返回default值。 5|popitem(),返回并删除字典中的最后一对键和值。 #### 1.5迭代处理字典 ``` found = {} found_list = ["a","b","c","d","e"] found ["a"] =0 found ["b"] =0 found ["c"] =0 found ["d"] =0 found ["e"] =0 shuru = input("请输入一个字符串") for i in shuru: print(i) if i in found_list: found.setdefault(i,0) #if i not in found: #found[i]=0 found[i] +=1 for kv in found: print(kv,"in found",found[kv],"time(s)") ``` #### 1.6字典 in和not in ``` fruit= {} fruit ["apples"] =10 if "bananas" in fruit: fruit["bananas"] += 1 else: fruit["bananas"] =0 fruit["bananas"] += 1 print(fruit) ``` #### 2.1集合 集合(set)是一个 **无序** 的不重复元素序列。集合中只有逗号,没有冒号。在创建集合时,重复的元素会自动合并。 可以使用大括号 { } 或者 set() 函数创建集合。 ``` a = {'a','e','i','o','u'} ``` 或者 ``` a = set("aeiou") ``` ``` a = set(["a","e","i","o","u"]) ``` #### 2.2将集合化为有序列表( 利用sorted()和list() ) ``` a = {'a','e','i','o','u'} b = sorted(list(a)) a ``` #### 2.3union合并集合 ``` a = {'a','e','i','o','u'} b = “hello” c = a.union(set(b)) c ``` #### 2.4difference() 告诉不同元素 ``` a = {'a','e','i','o','u'} b = “hello” c = a.difference(set(b)) c ``` #### 2.5intersection() 报告共同对象 ``` a = {'a','e','i','o','u'} b = “hello” c = a.intersection(set(b)) c ``` ## 二、代码练习 #### 统计个字符个数 ``` found = { "空格":0, "数字":0, "字母":0, "其他":0 } shuru = input("请随机输入一个字符串") for i in shuru: if i > 'a' and i < 'z' or i > 'A' and i < 'Z': found['字母'] += 1 elif i in '0123456789': found['数字'] += 1 elif i == ' ': found['空格'] += 1 else: found['其他'] += 1 for kv in found: print(kv, "is found",found[kv],"time(s)") ``` #### 迭代处理(频度) ``` found = {} found_list = ["a","b","c","d","e"] found ["a"] =0 found ["b"] =0 found ["c"] =0 found ["d"] =0 found ["e"] =0 shuru = input("请输入一个字符串") for i in shuru: print(i) if i in found_list: found.setdefault(i,0) #if i not in found: #found[i]=0 found[i] +=1 for kv in found: print(kv,"in found",found[kv],"time(s)") ``` ## 三、字典查询练习 ``` results = [ { "faceId1": "74539247-ceb0-4d09-b503-c1eef4ccedc7", "faceRectangle": { "top": 169, "left": 553, "width": 195, "height": 195 }, "faceAttributes": { "complexion": "black", "hair": { "bald": 0.81, #"invisible": false, "hairColor": [ { "color": "No hair", } ] }, "smile": 1.0, "headPose": { "pitch": 0.3, "roll": -4.6, "yaw": -6.7 }, "gender": "male", "age": 26.0, "facialHair": { "moustache": 0.4, "beard": 0.1, "sideburns": 0.1 }, "glasses": "NoGlasses", "makeup": { #"eyeMakeup": false, #"lipMakeup": true }, "emotion": { "anger": 0.0, "contempt": 0.0, "disgust": 0.0, "fear": 0.0, "happiness": 1.0, "neutral": 0.0, "sadness": 0.0, "surprise": 0.0 }, "occlusion": { #"foreheadOccluded": false, #"eyeOccluded": false, #"mouthOccluded": false }, "accessories": [], "blur": { "blurLevel": "low", "value": 0.0 }, "exposure": { "exposureLevel": "goodExposure", "value": 0.66 }, "noise": { "noiseLevel": "low", "value": 0.0 } }, "faceLandmarks": { "pupilLeft": { "x": 603.2, "y": 229.9 }, "pupilRight": { "x": 691.9, "y": 220.2 }, "noseTip": { "x": 650.9, "y": 270.9 }, "mouthLeft": { "x": 609.0, "y": 313.0 }, "mouthRight": { "x": 697.2, "y": 308.5 }, "eyebrowLeftOuter": { "x": 570.1, "y": 210.9 }, "eyebrowLeftInner": { "x": 618.5, "y": 202.3 }, "eyeLeftOuter": { "x": 588.8, "y": 233.3 }, "eyeLeftTop": { "x": 603.1, "y": 224.7 }, "eyeLeftBottom": { "x": 603.7, "y": 236.3 }, "eyeLeftInner": { "x": 618.5, "y": 231.2 }, "eyebrowRightInner": { "x": 669.4, "y": 197.5 }, "eyebrowRightOuter": { "x": 723.1, "y": 196.5 }, "eyeRightInner": { "x": 678.9, "y": 225.9 }, "eyeRightTop": { "x": 691.0, "y": 215.8 }, "eyeRightBottom": { "x": 694.7, "y": 227.7 }, "eyeRightOuter": { "x": 708.0, "y": 220.4 }, "noseRootLeft": { "x": 635.4, "y": 231.4 }, "noseRootRight": { "x": 659.7, "y": 229.0 }, "noseLeftAlarTop": { "x": 628.8, "y": 257.6 }, "noseRightAlarTop": { "x": 672.4, "y": 254.0 }, "noseLeftAlarOutTip": { "x": 616.3, "y": 276.0 }, "noseRightAlarOutTip": { "x": 684.8, "y": 269.5 }, "upperLipTop": { "x": 652.7, "y": 298.2 }, "upperLipBottom": { "x": 654.3, "y": 308.4 }, "underLipTop": { "x": 656.9, "y": 328.1 }, "underLipBottom": { "x": 657.9, "y": 339.6 } } }, { "faceId2": "c8c2e4b7-9e3e-408f-a24b-1a7983ea2ffb", "faceRectangle": { "top": 189, "left": 172, "width": 183, "height": 183 }, "faceAttributes": { "complexion": "black", "hair": { "bald": 0.31, #"invisible": false, "hairColor": [ { "color": "brown", "confidence": 0.99 }, { "color": "black", "confidence": 0.8 }, { "color": "red", "confidence": 0.35 }, { "color": "blond", "confidence": 0.16 }, { "color": "other", "confidence": 0.11 }, { "color": "gray", "confidence": 0.1 }, { "color": "white", "confidence": 0.0 } ] }, "smile": 1.0, "headPose": { "pitch": -3.7, "roll": 8.0, "yaw": 10.9 }, "gender": "female", "age": 32.0, "facialHair": { "moustache": 0.0, "beard": 0.0, "sideburns": 0.0 }, "glasses": "NoGlasses", "makeup": { #"eyeMakeup": true, #"lipMakeup": true }, "emotion": { "anger": 0.0, "contempt": 0.0, "disgust": 0.0, "fear": 0.0, "happiness": 1.0, "neutral": 0.0, "sadness": 0.0, "surprise": 0.0 }, "occlusion": { #"foreheadOccluded": false, #"eyeOccluded": false, #"mouthOccluded": false }, "accessories": [], "blur": { "blurLevel": "low", "value": 0.0 }, "exposure": { "exposureLevel": "goodExposure", "value": 0.65 }, "noise": { "noiseLevel": "low", "value": 0.0 } }, "faceLandmarks": { "pupilLeft": { "x": 222.8, "y": 237.0 }, "pupilRight": { "x": 308.8, "y": 248.0 }, "noseTip": { "x": 267.3, "y": 292.1 }, "mouthLeft": { "x": 213.6, "y": 314.4 }, "mouthRight": { "x": 304.3, "y": 322.3 }, "eyebrowLeftOuter": { "x": 192.2, "y": 213.5 }, "eyebrowLeftInner": { "x": 253.0, "y": 214.4 }, "eyeLeftOuter": { "x": 209.0, "y": 235.6 }, "eyeLeftTop": { "x": 224.3, "y": 232.9 }, "eyeLeftBottom": { "x": 222.5, "y": 241.6 }, "eyeLeftInner": { "x": 238.8, "y": 242.0 }, "eyebrowRightInner": { "x": 297.7, "y": 219.3 }, "eyebrowRightOuter": { "x": 343.8, "y": 229.0 }, "eyeRightInner": { "x": 296.9, "y": 249.7 }, "eyeRightTop": { "x": 310.7, "y": 244.6 }, "eyeRightBottom": { "x": 309.9, "y": 253.2 }, "eyeRightOuter": { "x": 325.0, "y": 250.4 }, "noseRootLeft": { "x": 260.1, "y": 245.5 }, "noseRootRight": { "x": 279.6, "y": 248.9 }, "noseLeftAlarTop": { "x": 245.3, "y": 271.1 }, "noseRightAlarTop": { "x": 287.2, "y": 277.6 }, "noseLeftAlarOutTip": { "x": 229.8, "y": 284.8 }, "noseRightAlarOutTip": { "x": 297.6, "y": 293.3 }, "upperLipTop": { "x": 264.2, "y": 311.8 }, "upperLipBottom": { "x": 264.2, "y": 320.2 }, "underLipTop": { "x": 259.5, "y": 340.5 }, "underLipBottom": { "x": 259.0, "y": 353.0 } } }, { "faceId3": "4e720115-c138-43f5-b772-958e3e6bf55c", "faceRectangle": { "top": 347, "left": 410, "width": 137, "height": 137 }, "faceAttributes": { "complexion": "black", "hair": { "bald": 0.11, #"invisible": false, "hairColor": [ { "color": "brown", "confidence": 0.92 }, { "color": "blond", "confidence": 0.87 }, { "color": "black", "confidence": 0.36 }, { "color": "gray", "confidence": 0.25 }, { "color": "red", "confidence": 0.24 }, { "color": "other", "confidence": 0.18 }, { "color": "white", "confidence": 0.0 } ] }, "smile": 0.0, "headPose": { "pitch": 2.5, "roll": -0.3, "yaw": -8.9 }, "gender": "male", "age": 1.0, "facialHair": { "moustache": 0.0, "beard": 0.0, "sideburns": 0.0 }, "glasses": "NoGlasses", "makeup": { #"eyeMakeup": false, #"lipMakeup": false }, "emotion": { "anger": 0.0, "contempt": 0.0, "disgust": 0.0, "fear": 0.0, "happiness": 0.0, "neutral": 0.247, "sadness": 0.0, "surprise": 0.752 }, "occlusion": { #"foreheadOccluded": false, #"eyeOccluded": false, #"mouthOccluded": false }, "accessories": [], "blur": { "blurLevel": "low", "value": 0.0 }, "exposure": { "exposureLevel": "goodExposure", "value": 0.72 }, "noise": { "noiseLevel": "low", "value": 0.0 } }, "faceLandmarks": { "pupilLeft": { "x": 449.9, "y": 384.5 }, "pupilRight": { "x": 511.2, "y": 386.9 }, "noseTip": { "x": 477.7, "y": 408.8 }, "mouthLeft": { "x": 454.0, "y": 453.8 }, "mouthRight": { "x": 501.5, "y": 451.1 }, "eyebrowLeftOuter": { "x": 425.7, "y": 374.7 }, "eyebrowLeftInner": { "x": 462.5, "y": 371.9 }, "eyeLeftOuter": { "x": 439.0, "y": 386.4 }, "eyeLeftTop": { "x": 447.3, "y": 380.6 }, "eyeLeftBottom": { "x": 447.7, "y": 389.5 }, "eyeLeftInner": { "x": 457.6, "y": 386.4 }, "eyebrowRightInner": { "x": 497.1, "y": 372.8 }, "eyebrowRightOuter": { "x": 531.6, "y": 377.0 }, "eyeRightInner": { "x": 500.6, "y": 389.5 }, "eyeRightTop": { "x": 511.7, "y": 382.7 }, "eyeRightBottom": { "x": 511.7, "y": 393.7 }, "eyeRightOuter": { "x": 522.5, "y": 389.8 }, "noseRootLeft": { "x": 471.6, "y": 387.1 }, "noseRootRight": { "x": 485.6, "y": 388.2 }, "noseLeftAlarTop": { "x": 465.7, "y": 406.8 }, "noseRightAlarTop": { "x": 489.2, "y": 408.0 }, "noseLeftAlarOutTip": { "x": 460.3, "y": 418.4 }, "noseRightAlarOutTip": { "x": 494.6, "y": 417.4 }, "upperLipTop": { "x": 477.0, "y": 434.7 }, "upperLipBottom": { "x": 477.2, "y": 441.6 }, "underLipTop": { "x": 476.8, "y": 456.4 }, "underLipBottom": { "x": 476.0, "y": 464.0 } } } ] # 创建字典,收集数据 dict1 = { "glasses": print("glasses:",results[0]["faceAttributes"]["glasses"]), "smile": print("smile:",results[0]["faceAttributes"]["smile"]), "age": print("age:",results[0]["faceAttributes"]["age"]), "gender": print("gender:",results[0]["faceAttributes"]["gender"]), "haircolor": print("haircolor:",results[0]["faceAttributes"]["hair"]["hairColor"][0]["color"]), "complexion": print("complexion:",results[0]["faceAttributes"]["complexion"]) } dict2 = {"glasses": print("glasses:",results[1]["faceAttributes"]["glasses"]), "smile": print("smile:",results[1]["faceAttributes"]["smile"]), "age": print("age:",results[1]["faceAttributes"]["age"]), "gender": print("gender:",results[1]["faceAttributes"]["gender"]), "haircolor": print("haircolor:",results[1]["faceAttributes"]["hair"]["hairColor"][0]["color"]), "complexion": print("complexion:",results[1]["faceAttributes"]["complexion"]) } dict3 = {"glasses": print("glasses:",results[2]["faceAttributes"]["glasses"]), "smile": print("smile:",results[2]["faceAttributes"]["smile"]), "age": print("age:",results[2]["faceAttributes"]["age"]), "gender": print("gender:",results[2]["faceAttributes"]["gender"]), "haircolor": print("haircolor:",results[2]["faceAttributes"]["hair"]["hairColor"][0]["color"]), "complexion": print("complexion:",results[2]["faceAttributes"]["complexion"]) } ``` 结果为 ``` glasses: NoGlasses smile: 1.0 age: 26.0 gender: male haircolor: No hair complexion: black glasses: NoGlasses smile: 1.0 age: 32.0 gender: female haircolor: brown complexion: black glasses: NoGlasses smile: 0.0 age: 1.0 gender: male haircolor: brown complexion: black ```