目錄java
集合能夠理解成一個集合體,學習Python的學生能夠是一個集合體;學習Linux的學生能夠是一個集合體。python
pythoners = ['jason', 'nick', 'tank', 'sean'] linuxers = ['nick', 'egon', 'kevin'] # 即報名pythoners又報名linux的學生 py_li_list = [] for stu in pythoners: if stu in linuxers: py_li_list.append(stu) print(f"pythoners and linuxers: {py_li_list}") pythoners and linuxers: ['nick']
上述的列表方式求兩個集合體的關係運算很是複雜,所以有了咱們的集合數據類型。linux
用於關係運算的集合體,因爲集合內的元素無序且集合元素不可重複,所以集合能夠去重,可是去重後的集合會打亂原來元素的順序。app
{}內用逗號分隔開多個元素,每一個元素必須是不可變類型。學習
s = {1, 2, 1, 'a'} # s = set({1,2,'a'}) print(f"s: {s}") s: {1, 2, 'a'} s = {1, 2, 1, 'a', 'c'} for i in s: print(i) 1 2 c a s = set('hello') print(f"s: {s}") s: {'e', 'o', 'h', 'l'}
經常使用操做和內置方法分爲優先掌握(今天必須得記住)、須要掌握(一週內記住)兩個部分。.code
# set之長度len s = {1, 2, 'a'} print(f"len(s): {len(s)}") len(s): 3
# set之成員運算in和not in s = {1, 2, 'a'} print(f"1 in s: {1 in s}") 1 in s: True
# str之|並集 pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"pythoners|linuxers: {pythoners|linuxers}") print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}") pythoners|linuxers: {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'} pythoners.union(linuxers): {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
# str之&交集 pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"pythoners&linuxers: {pythoners&linuxers}") print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}") pythoners&linuxers: {'nick'} pythoners.intersection(linuxers): {'nick'}
# str之-差集 pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"pythoners-linuxers: {pythoners-linuxers}") print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}") pythoners-linuxers: {'tank', 'jason', 'sean'} pythoners.difference(linuxers): {'tank', 'jason', 'sean'}
# str之^對稱差集 pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"pythoners^linuxers: {pythoners^linuxers}") print( f"pythoners.symmetric_difference(linuxers): {pythoners.symmetric_difference(linuxers)}") pythoners^linuxers: {'egon', 'tank', 'kevin', 'jason', 'sean'} pythoners.symmetric_difference(linuxers): {'egon', 'tank', 'kevin', 'jason', 'sean'}
# str之== pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javers = {'nick', 'egon', 'kevin'} print(f"pythoners==linuxers: {pythoners==linuxers}") print(f"javers==linuxers: {javers==linuxers}") pythoners==linuxers: False javers==linuxers: True
# str之父集:>、>= pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javaers = {'jason', 'nick'} print(f"pythoners>linuxers: {pythoners>linuxers}") print(f"pythoners>=linuxers: {pythoners>=linuxers}") print(f"pythoners>=javaers: {pythoners>=javaers}") print(f"pythoners.issuperset(javaers): {pythoners.issuperset(javaers)}") pythoners>linuxers: False pythoners>=linuxers: False pythoners>=javaers: True pythoners.issuperset(javaers): True
# str之子集:<、<= pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javaers = {'jason', 'nick'} print(f"pythoners<linuxers: {pythoners<linuxers}") print(f"pythoners<=linuxers: {pythoners<=linuxers}") print(f"javaers.issubset(javaers): {javaers.issubset(javaers)}") pythoners<linuxers: False pythoners<=linuxers: False javaers.issubset(javaers): True
# set之add() s = {1, 2, 'a'} s.add(3) print(s) {1, 2, 3, 'a'}
# set之remove() s = {1, 2, 'a'} s.remove(1) print(s) {2, 'a'}
# str之difference_update() pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} pythoners.difference_update(linuxers) print(f"pythoners.difference_update(linuxers): {pythoners}") pythoners.difference_update(linuxers): {'tank', 'jason', 'sean'}
# set之discard() s = {1, 2, 'a'} # s.remove(3) # 報錯 s.discard(3) print(s) {1, 2, 'a'}
# set之isdisjoint(),集合沒有共同的部分返回True,不然返回False pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} pythoners.isdisjoint(linuxers) print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}") pythoners.isdisjoint(linuxers): False
有以下列表,列表元素爲不可hash類型,去重,獲得新列表,且新列表必定要保持列表原來的順序blog
stu_info_list = [ {'name':'nick','age':19,'sex':'male'}, {'name':'egon','age':18,'sex':'male'}, {'name':'tank','age':20,'sex':'female'}, {'name':'tank','age':20,'sex':'female'}, {'name':'egon','age':18,'sex':'male'}, ] stu_info_list = [ {'name': 'nick', 'age': 19, 'sex': 'male'}, {'name': 'egon', 'age': 18, 'sex': 'male'}, {'name': 'tank', 'age': 20, 'sex': 'female'}, {'name': 'tank', 'age': 20, 'sex': 'female'}, {'name': 'egon', 'age': 18, 'sex': 'male'}, ] new_stu_info_list = [] for stu_info in stu_info_list: if stu_info not in new_stu_info_list: new_stu_info_list.append(stu_info) for new_stu_info in new_stu_info_list: print(new_stu_info) {'name': 'nick', 'age': 19, 'sex': 'male'} {'name': 'egon', 'age': 18, 'sex': 'male'} {'name': 'tank', 'age': 20, 'sex': 'female'}
s = {1, 2, 'a'} print(f'first:{id(s)}') s.add(3) print(f'second:{id(s)}') first:4480523848 second:4480523848