aidd

导航

 

一、集合类型内置方法(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}")

default

pythoners and linuxers: ['nick']

上述的列表方式求两个集合体的关系运算非常复杂,因此有了我们的集合数据类型。

1.用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序。

2.定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型。

python

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

print(f"s: {s}")

default

s: {1, 2, 'a'}

python

s = {1, 2, 1, 'a', 'c'}

for i in s:
    print(i)

default

1
2
c
a

python

s = set('hello')

print(f"s: {s}")

default

s: {'e', 'o', 'h', 'l'}

3.常用操作+内置方法:常用操作和内置方法分为优先掌握(今天必须得记住)、需要掌握(一周内记住)两个部分。

1.1 优先掌握(*****)

  1. 长度len
  2. 成员运算in和not in
  3. |并集、union
  4. &交集、intersection
  5. -差集、difference
  6. ^对称差集、symmetric_difference
  7. ==
  8. 父集:>、>= 、issuperset
  9. 子集:<、<= 、issubset

1.长度len

python

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

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

default

len(s): 3

2.成员运算in和not in

python

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

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

default

1 in s: True

img

3.|并集

python

# str之|并集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}

print(f"pythoners|linuxers: {pythoners|linuxers}")
print(f"pythoners.union(linuxers): {pythoners.union(linuxers)}")

default

pythoners|linuxers: {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
pythoners.union(linuxers): {'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}

4.&交集

python

# str之&交集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}

print(f"pythoners&linuxers: {pythoners&linuxers}")
print(f"pythoners.intersection(linuxers): {pythoners.intersection(linuxers)}")

default

pythoners&linuxers: {'nick'}
pythoners.intersection(linuxers): {'nick'}

5.-差集

python

# str之-差集
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}

print(f"pythoners-linuxers: {pythoners-linuxers}")
print(f"pythoners.difference(linuxers): {pythoners.difference(linuxers)}")

default

pythoners-linuxers: {'tank', 'jason', 'sean'}
pythoners.difference(linuxers): {'tank', 'jason', 'sean'}

6.^对称差集

python

# 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)}")

default

pythoners^linuxers: {'egon', 'tank', 'kevin', 'jason', 'sean'}
pythoners.symmetric_difference(linuxers): {'egon', 'tank', 'kevin', 'jason', 'sean'}

7.==

python

# 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}")

default

pythoners==linuxers: False
javers==linuxers: True

8.父集:>、>=

python

# 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)}")

default

pythoners>linuxers: False
pythoners>=linuxers: False
pythoners>=javaers: True
pythoners.issuperset(javaers): True

9.子集:<、<=

python

# 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)}")

default

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

python

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

print(s)

default

{1, 2, 3, 'a'}

2.remove()

python

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

print(s)

default

{2, 'a'}

3.difference_update()

python

# str之difference_update()
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.difference_update(linuxers)

print(f"pythoners.difference_update(linuxers): {pythoners}")

default

pythoners.difference_update(linuxers): {'tank', 'jason', 'sean'}

4.discard()

python

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

print(s)

default

{1, 2, 'a'}

5.isdisjoint()

python

# set之isdisjoint(),集合没有共同的部分返回True,否则返回False
pythoners = {'jason', 'nick', 'tank', 'sean'}
linuxers = {'nick', 'egon', 'kevin'}
pythoners.isdisjoint(linuxers)

print(f"pythoners.isdisjoint(linuxers): {pythoners.isdisjoint(linuxers)}")

default

pythoners.isdisjoint(linuxers): False

二、练习

有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序

python

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

python

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)

default

{'name': 'nick', 'age': 19, 'sex': 'male'}
{'name': 'egon', 'age': 18, 'sex': 'male'}
{'name': 'tank', 'age': 20, 'sex': 'female'}

4.存一个值or多个值:多个值,且值为不可变类型。

5.有序or无序:无序

python

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

default

first:4480523848
second:4480523848

6.可变or不可变:可变数据类型

posted on 2023-05-31 10:46  开源共享  阅读(5)  评论(0编辑  收藏  举报