集合類型內置方法

1、集合類型內置方法(set)

集合能夠理解成一個集合體,學習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

1.用途:用於關係運算的集合體,因爲集合內的元素無序且集合元素不可重複,所以集合能夠去重,可是去重後的集合會打亂原來元素的順序。app

2.定義:{}內用逗號分隔開多個元素,每一個元素必須是不可變類型。學習

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'}

3.經常使用操做+內置方法:經常使用操做和內置方法分爲優先掌握(今天必須得記住)、須要掌握(一週內記住)兩個部分。code

1.1 優先掌握(*****)

  1. 長度lenblog

  2. 成員運算in和not inrem

  3. |並集、unionhash

  4. &交集、intersectionit

  5. -差集、difference

  6. ^對稱差集、symmetric_difference
  7. ==

  8. 父集:>、>= 、issuperset

  9. 子集:<、<= 、issubset

1.長度len

# set之長度len
s = {1, 2, 'a'}

print(f"len(s): {len(s)}")
len(s): 3

2.成員運算in和not in

# set之成員運算in和not in
s = {1, 2, 'a'}

print(f"1 in s: {1 in s}")
1 in s: True

集合類型內置運算-集合運算英文.jpg?x-oss-process=style/watermark

3.|並集

# 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'}

4.&交集

# 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'}

5.-差集

# 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'}

6.^對稱差集

# 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'}

7.==

# 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

8.父集:>、>=

# 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

9.子集:<、<=

# 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

1.2 須要掌握(****)

  1. add

  2. remove

  3. difference_update

  4. discard

  5. isdisjoint

1.add()

# set之add()
s = {1, 2, 'a'}
s.add(3)

print(s)
{1, 2, 3, 'a'}

2.remove()

# set之remove()
s = {1, 2, 'a'}
s.remove(1)

print(s)
{2, 'a'}

3.difference_update()

# 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'}

4.discard()

# set之discard()
s = {1, 2, 'a'}
# s.remove(3)  # 報錯
s.discard(3)

print(s)
{1, 2, 'a'}

5.isdisjoint()

# 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

2、練習

有以下列表,列表元素爲不可hash類型,去重,獲得新列表,且新列表必定要保持列表原來的順序

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'}

4.存一個值or多個值:多個值,且值爲不可變類型。

5.有序or無序:無序

s = {1, 2, 'a'}
print(f'first:{id(s)}')
s.add(3)
print(f'second:{id(s)}')
first:4480523848
second:4480523848

6.可變or不可變:可變數據類型

相關文章
相關標籤/搜索