pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/JustDoPython/python-100-day/commit/bace1b2acd8ca7e133a447fe307ab85b32ba5d97

ous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-5efd63e783ac04bb.css" /> add python set · JustDoPython/python-100-day@bace1b2 · GitHub
Skip to content

Commit bace1b2

Browse files
committed
add python set
add python set
1 parent e1f9d38 commit bace1b2

12 files changed

Lines changed: 198 additions & 0 deletions

day-015/__init__.py

Whitespace-only changes.

day-015/set.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
set1 = {'hello', 'hello', 'word', 'word'}
3+
print(set1)
4+
5+
# 创建空集合
6+
empty_set = set()
7+
8+
# 创建空字典
9+
empty_dict = {}
10+
11+
# 集合添加元素
12+
s = set(('hello','world'))
13+
print(s)
14+
15+
# 向集合 s 中添加元素
16+
s.add('!')
17+
18+
print('添加元素后的集合是:%s' % s)
19+
20+
# 使用 update() 方法向集合中添加新元素
21+
# 1)添加列表
22+
s.update([1,3],[2,4])
23+
print('添加元素后的集合是:%s' % s)
24+
25+
# 2)添加元组
26+
s.update(('h', 'j'))
27+
print('添加元素后的集合是:%s' % s)
28+
29+
# 移除元素
30+
# 1) remove(x)
31+
s.remove(2)
32+
print('移除元素 2 后的集合是:%s' % s)
33+
34+
# 移除集合中不存在的集合
35+
#s.remove('hi')
36+
#print('移除元素后的集合是:%s' % s)
37+
38+
# discard(x) 此方法移除集合中不存在的元素不会报错
39+
s.discard('hi')
40+
print('移除元素后的集合是:%s' % s)
41+
42+
43+
# 随机删除集合中的一个元素
44+
print(s)
45+
46+
s.pop()
47+
48+
print('移除元素后的集合是:%s' % s)
49+
50+
51+
# 计算集合个数
52+
print('集合 s 的长度是:%s' % len(s))
53+
54+
55+
# 清空集合
56+
s.clear()
57+
print('集合清空后的结果是:%s' % s)
58+
59+
60+
# 判断元素是否存在
61+
s = {'hello', 'word'}
62+
# 判断元素 hello 是否在集合 s 中
63+
64+
print('hello' in s)

day-015/set_difference.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# 求两个集合的差集,元素在 x 中不在 y 中
3+
x = {"apple", "banana", "cherry"}
4+
y = {"google", "microsoft", "apple"}
5+
6+
z = x.difference(y)
7+
8+
print('两个集合的差集是:%s' % z)

day-015/set_difference_update.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# 移除两个集合中都存在的元素,无返回值,将 x 中存在的 y 中不存在的留下,将 x 和 y 中都存在的删除,y 中存在和不存在的删除
3+
x = {"apple", "banana", "cherry"}
4+
y = {"google", "microsoft", "apple"}
5+
6+
x.difference_update(y)
7+
8+
print(x)
9+
10+
11+
x1 = {1,2,3,4}
12+
y1 = {1,2,3}
13+
14+
x1.difference_update(y1)
15+
16+
print(x1)

day-015/set_intersection.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
# 返回两个或者多个集合的交集
3+
x = {"apple", "banana", "cherry"}
4+
y = {"google", "runoob", "apple"}
5+
6+
z = x.intersection(y)
7+
8+
print(z)
9+
10+
# 返回三个集合的交集
11+
x = {"a", "b", "c"}
12+
y = {"c", "d", "e"}
13+
z = {"f", "g", "c"}
14+
15+
result = x.intersection(y, z)
16+
print('三个集合的差集是:%s' % result)

day-015/set_intersection_update.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 返回一个无返回值的集合交集
2+
x = {"apple", "banana", "cherry"}
3+
y = {"google", "runoob", "apple"}
4+
5+
x.intersection_update(y)
6+
7+
print(x)
8+
9+
x = {"a", "b", "c"}
10+
y = {"c", "d", "e"}
11+
z = {"f", "g", "c"}
12+
13+
x.intersection_update(y, z)
14+
15+
print(x)

day-015/set_isdisjoint.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
x = {"apple", "banana", "cherry"}
2+
y = {"google", "runoob", "apple"}
3+
# 判断集合 y 中是否包含集合 x 中的元素,如果没有返回 True, 有则返回 False
4+
z = x.isdisjoint(y)
5+
# 结果返回 False,说明集合 y 中有和 x 中相同的元素
6+
print(z)
7+
8+
9+
10+
x = {"apple", "banana", "cherry"}
11+
y = {"google", "runoob", "baidu"}
12+
# 判断集合 y 中是否包含集合 x 中的元素,如果没有返回 True, 有则返回 False
13+
z = x.isdisjoint(y)
14+
# 结果返回 True,说明集合 y 中没有和 x 中相同的元素
15+
print(z)

day-015/set_issubset.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# 判断集合 x 的所有元素是否都包含在集合 y 中:
3+
x = {"a", "b", "c"}
4+
y = {"f", "e", "d", "c", "b", "a"}
5+
6+
z = x.issubset(y)
7+
8+
print(z)
9+
10+
x = {"a", "b", "c"}
11+
y = {"f", "e", "d", "c", "b","y"}
12+
13+
z = x.issubset(y)
14+
15+
print(z)

day-015/set_issuperset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
x = {"f", "e", "d", "c", "b", "a"}
2+
y = {"a", "b", "c"}
3+
4+
z = x.issuperset(y)
5+
6+
print(z)
7+
8+
# 如果没有全部包含返回 False:
9+
x = {"f", "e", "d", "c", "b"}
10+
y = {"a", "b", "c"}
11+
12+
z = x.issuperset(y)
13+
14+
print(z)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# 返回两个集合组成的新集合,但会移除两个集合的重复元素:
3+
x = {"apple", "banana", "cherry"}
4+
y = {"google", "runoob", "apple"}
5+
6+
z = x.symmetric_difference(y)
7+
8+
print(z)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy