后盾网原创实战网站建设教程,怎么能自己做网站,汕头关键词排名,网页设计网站建设流程一、列表
列表#xff08;List#xff09;是一种有序且可变的容器数据类型。 与集合#xff08;Set#xff09;不同#xff0c;列表允许重复的元素。 它方便保存数据序列并对其进行进一步迭代。 列表用方括号创建。
my_list [banana, cherry, …一、列表
列表List是一种有序且可变的容器数据类型。 与集合Set不同列表允许重复的元素。 它方便保存数据序列并对其进行进一步迭代。 列表用方括号创建。
my_list [banana, cherry, apple]
Python中基本的内置容器数据类型的比较 列表List是一个有序且可变的数据类型。 允许重复的成员。 元组Tuple是有序且不可变的数据类型。 允许重复的成员。 集合Set是无序和未索引的数据类型。 不允许重复的成员。 字典Dict是无序可变和可索引的数据类型。 没有重复的成员。 字符串是Unicode代码的不可变序列。
1、创建列表
列表使用方括号创建或者内置的 list 函数。
list_1 [banana, cherry, apple]
print(list_1)# 或者使用 list 函数创建空列表
list_2 list()
print(list_2)# 列表允许不同的数据类
list_3 [5, True, apple]
print(list_3)# 列表允许重复元素
list_4 [0, 0, 1, 1]
print(list_4) [banana, cherry, apple][][5, True, apple][0, 0, 1, 1]2、访问元素
可以通过索引号访问列表项。 请注意索引从0开始。
item list_1[0]
print(item)# 你也可以使用负索引比如 -1 表示最后一个元素
# -2 表示倒数第二个元素以此类推
item list_1[-1]
print(item) bananaapple3、修改元素
只需访问索引并分配一个新值即可。
# 列表创建之后可以被修改
list_1[2] lemon
print(list_1)
[banana, cherry, lemon]4、列表方法
查看Python文档以查看所有列表方法5. Data Structures — Python 3.13.0 documentation
my_list [banana, cherry, apple]# len() : 获取列表的元素个数
print(Length:, len(my_list))# append() : 添加一个元素到列表末尾
my_list.append(orange)# insert() : 添加元素到特定位置
my_list.insert(1, blueberry)
print(my_list)# pop() : 移除并返回特定位置的元素默认为最后一个
item my_list.pop()
print(Popped item: , item)# remove() : 移除列表中的元素
my_list.remove(cherry) # 如果元素没有在列表中则触发 Value error
print(my_list)# clear() : 移除列表所有元素
my_list.clear()
print(my_list)# reverse() : 翻转列表
my_list [banana, cherry, apple]
my_list.reverse()
print(Reversed: , my_list)# sort() : 升序排列元素
my_list.sort()
print(Sorted: , my_list)# 使用 sorted() 得到一个新列表原来的列表不受影响
# sorted() 对任何可迭代类型起作用不只是列表
my_list [banana, cherry, apple]
new_list sorted(my_list)# 创建具有重复元素的列表
list_with_zeros [0] * 5
print(list_with_zeros)# 列表拼接
list_concat list_with_zeros my_list
print(list_concat)# 字符串转列表
string_to_list list(Hello)
print(string_to_list)
输出结果
Length: 3 [banana, blueberry, cherry, apple, orange] Popped item: orange [banana, blueberry, apple] [] Reversed: [apple, cherry, banana] Sorted: [apple, banana, cherry] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0, banana, cherry, apple] [H, e, l, l, o]
5、复制列表
复制引用references时要小心。
list_org [banana, cherry, apple]# 这只是将引用复制到列表中要小心
list_copy list_org# 现在修改复制的列表也会影响原来的列表
list_copy.append(True)
print(list_copy)
print(list_org)# 使用 copy(), 或者 list(x) 来真正复制列表
# 切片slicing也可以复制list_copy list_org[:]
list_org [banana, cherry, apple]list_copy list_org.copy()
# list_copy list(list_org)
# list_copy list_org[:]# 现在修改复制的列表不会影响原来的列表
list_copy.append(True)
print(list_copy)
print(list_org) [banana, cherry, apple, True][banana, cherry, apple, True][banana, cherry, apple, True][banana, cherry, apple]6、迭代
# 使用for循环迭代列表
for i in list_1:print(i) banana
cherry
lemon7、检查元素是否存在
if banana in list_1:print(yes)
else:print(no) yes8、切片
和字符串一样使用冒号 :访问列表的子部分。
# a[start:stop:step], 默认步长为 1
a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b a[1:3] # 注意最后一个索引不包括
print(b)
b a[2:] # 直到最后
print(b)
b a[:3] # 从第一个元素开始
print(b)
a[0:3] [0] # 替换子部分需要可迭代
print(a)
b a[::2] # 从头到为每隔两个元素
print(b)
a a[::-1] # 使用负步长翻转列表
print(a)
b a[:] # 使用切片复制元素
print(b) [2, 3][3, 4, 5, 6, 7, 8, 9, 10][1, 2, 3][0, 4, 5, 6, 7, 8, 9, 10][0, 5, 7, 9][10, 9, 8, 7, 6, 5, 4, 0][10, 9, 8, 7, 6, 5, 4, 0]9、列表推导
一种从现有列表创建新列表的简便快捷方法。
列表推导方括号内包含一个表达式后跟for语句。
a [1, 2, 3, 4, 5, 6, 7, 8]
b [i * i for i in a] # 每个元素平方
print(b) [1, 4, 9, 16, 25, 36, 49, 64]10、嵌套列表
a [[1, 2], [3, 4]]
print(a)
print(a[0]) [[1, 2], [3, 4]][1, 2]
二、元组
元组Tuple是对象的集合它有序且不可变。 元组类似于列表主要区别在于不可变性。 在Python中元组用圆括号和逗号分隔的值书写。
my_tuple (Max, 28, New York)使用元组而不使用列表的原因 通常用于属于同一目标的对象。 将元组用于异构不同数据类型将列表用于同类相似数据类型。 由于元组是不可变的因此通过元组进行迭代比使用列表进行迭代要快一些。 具有不可变元素的元组可以用作字典的键。 使用列表做为键是不可能的。 如果你有不变的数据则将其实现为元组将确保其有写保护。
1、创建元组
用圆括号和逗号分隔的值创建元组或使用内置的 tuple 函数。
tuple_1 (Max, 28, New York)
tuple_2 Linda, 25, Miami # 括弧可选# 特殊情况只有一个元素的元组需要在在最后添加逗号否则不会被识别为元组
tuple_3 (25,)
print(tuple_1)
print(tuple_2)
print(tuple_3)# 或者使用内置 tuple 函数将可迭代对象listdictstring转变为元组
tuple_4 tuple([1,2,3])
print(tuple_4) (Max, 28, New York)(Linda, 25, Miami)(25,)(1, 2, 3)2、访问元素
可以通过引用索引号访问元组项。 请注意索引从0开始。
item tuple_1[0]
print(item)
# 你也可以使用负索引比如 -1 表示最后一个元素-2 表示倒数第二个元素以此类推
item tuple_1[-1]
print(item) MaxNew York3、添加或者修改元素
不可能会触发 TypeError 错误。
tuple_1[2] Boston---------------------------------------------------------------------------TypeError Traceback (most recent call last)ipython-input-5-c391d8981369 in module---- 1 tuple_1[2] BostonTypeError: tuple object does not support item assignment4、删除元组
del tuple_25、迭代
# 使用 for 循环迭代元组
for i in tuple_1:print(i) Max28New York6、检查元素是否存在
if New York in tuple_1:print(yes)
else:print(no) yes7、元祖方法
my_tuple (a,p,p,l,e,)# len() : 获取元组元素个数
print(len(my_tuple))# count(x) : 返回与 x 相等的元素个数
print(my_tuple.count(p))# index(x) : 返回与 x 相等的第一个元素索引
print(my_tuple.index(l))# 重复
my_tuple (a, b) * 5
print(my_tuple)# 拼接
my_tuple (1,2,3) (4,5,6)
print(my_tuple)# 将列表转为元组以及将元组转为列表
my_list [a, b, c, d]
list_to_tuple tuple(my_list)
print(list_to_tuple)tuple_to_list list(list_to_tuple)
print(tuple_to_list)# convert string to tuple
string_to_tuple tuple(Hello)
print(string_to_tuple) 523(a, b, a, b, a, b, a, b, a, b)(1, 2, 3, 4, 5, 6)(a, b, c, d)[a, b, c, d](H, e, l, l, o)8、切片
和字符串一样使用冒号:访问列表的子部分。
# a[start:stop:step], 默认步长为 1
a (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
b a[1:3] # 注意最后一个索引不包括
print(b)
b a[2:] # 知道最后
print(b)
b a[:3] # 从最前头开始
print(b)
b a[::2] # 从前往后没两个元素
print(b)
b a[::-1] # 翻转元组
print(b) (2, 3)(3, 4, 5, 6, 7, 8, 9, 10)(1, 2, 3)(1, 3, 5, 7, 9)(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)9、元组解包
# 变量个数必需与元组元素个数相同tuple_1 (Max, 28, New York)
name, age, city tuple_1
print(name)
print(age)
print(city)# 提示: 使用 * 解包多个元素到列表
my_tuple (0, 1, 2, 3, 4, 5)
item_first, *items_between, item_last my_tuple
print(item_first)
print(items_between)
print(item_last) Max28New York0[1, 2, 3, 4]510、嵌套元组
a ((0, 1), (age, height))
print(a)
print(a[0]) ((0, 1), (age, height))(0, 1)11、比较元组和列表
元组的不可变性使Python可以进行内部优化。 因此在处理大数据时元组可以更高效。
# 比较大小
import sys
my_list [0, 1, 2, hello, True]
my_tuple (0, 1, 2, hello, True)
print(sys.getsizeof(my_list), bytes)
print(sys.getsizeof(my_tuple), bytes)# 比较列表和元组创建语句的执行时间
import timeit
print(timeit.timeit(stmt[0, 1, 2, 3, 4, 5], number1000000))
print(timeit.timeit(stmt(0, 1, 2, 3, 4, 5), number1000000)) 104 bytes88 bytes0.124749817000008530.014836141000017733三、字典
字典是无序可变和可索引的集合。 字典由键值对的集合组成。 每个键值对将键映射到其关联值。 字典用大括号书写。 每对键值均以冒号 : 分隔并且各项之间以逗号分隔。
my_dict {name:Max, age:28, city:New York}创建字典
使用大括号或者内置的 dict 函数创建。
my_dict {name:Max, age:28, city:New York}
print(my_dict)# 或者使用字典构造器注意键不需要引号。
my_dict_2 dict(nameLisa, age27, cityBoston)
print(my_dict_2) {name: Max, age: 28, city: New York}{name: Lisa, age: 27, city: Boston}访问元素
name_in_dict my_dict[name]
print(name_in_dict)# 如果键没有找到引发 KeyError 错误
# print(my_dict[lastname])Max添加或修改元素
只需添加或访问键并分配值即可。
# 添加新键
my_dict[email] maxxyz.com
print(my_dict)# 覆盖已经存在的键
my_dict[email] coolmaxxyz.com
print(my_dict) {name: Max, age: 28, city: New York, email: maxxyz.com}{name: Max, age: 28, city: New York, email: coolmaxxyz.com}删除元素
# 删除键值对
del my_dict[email]# pop 返回值并删除键值对
print(popped value:, my_dict.pop(age))# 返回并移除最后插入的价值对
# 在 Python 3.7 之前移除任意键值对
print(popped item:, my_dict.popitem())print(my_dict)# clear() : 移除所有键值对
# my_dict.clear()popped value: 28popped item: (city, New York){name: Max}
检查键my_dict {name:Max, age:28, city:New York}
# 使用 if .. in ..
if name in my_dict:print(my_dict[name])# 使用 try except
try:print(my_dict[firstname])
except KeyError:print(No key found) MaxNo key found遍历字典
# 遍历键
for key in my_dict:print(key, my_dict[key])# 遍历键
for key in my_dict.keys():print(key)# 遍历值
for value in my_dict.values():print(value)# 遍历键和值
for key, value in my_dict.items():print(key, value) name Maxage 28city New YorknameagecityMax28New Yorkname Maxage 28city New York复制字典
复制索引时请注意。
dict_org {name:Max, age:28, city:New York}# 这只复制字典的引用需要小心
dict_copy dict_org# 修改复制字典也会影响原来的字典
dict_copy[name] Lisa
print(dict_copy)
print(dict_org)# 使用 copy() 或者 dict(x) 来真正复制字典
dict_org {name:Max, age:28, city:New York}dict_copy dict_org.copy()
# dict_copy dict(dict_org)# 现在修改复制字典不会影响原来的字典
dict_copy[name] Lisa
print(dict_copy)
print(dict_org) {name: Lisa, age: 28, city: New York}{name: Lisa, age: 28, city: New York}{name: Lisa, age: 28, city: New York}{name: Max, age: 28, city: New York}合并两个字典
# 使用 update() 方法合两个字典
# 存在的键会被覆盖新键会被添加
my_dict {name:Max, age:28, email:maxxyz.com}
my_dict_2 dict(nameLisa, age27, cityBoston)my_dict.update(my_dict_2)
print(my_dict) {name: Lisa, age: 27, email: maxxyz.com, city: Boston}可能的键类型
任何不可变的类型例如字符串或数字都可以用作键。 另外如果元组仅包含不可变元素则可以使用它作为键。
# 使用数字做键但要小心
my_dict {3: 9, 6: 36, 9:81}
# 不要将键误认为是列表的索引例如在这里无法使用 my_dict[0]
print(my_dict[3], my_dict[6], my_dict[9])# 使用仅包含不可变元素例如数字字符串的元组
my_tuple (8, 7)
my_dict {my_tuple: 15}print(my_dict[my_tuple])
# print(my_dict[8, 7])# 不能使用列表因为列表是可变的会抛出错误
# my_list [8, 7]
# my_dict {my_list: 15} 9 36 8115嵌套字典
值也可以是容器类型例如列表元组字典。
my_dict_1 {name: Max, age: 28}
my_dict_2 {name: Alex, age: 25}
nested_dict {dictA: my_dict_1,dictB: my_dict_2}
print(nested_dict) {dictA: {name: Max, age: 28}, dictB: {name: Alex, age: 25}}四、 字符串
字符串是字符序列。 Python中的字符串用双引号或单引号引起来。
my_string HelloPython字符串是不可变的这意味着它们在创建后就无法更改。
创建
# 使用单引号后者双引号
my_string Hello
my_string Hello
my_string I m a Geek# 转义反斜杠
my_string I\ m a Geek
my_string I\ m a \Geek\
print(my_string)# 多行字符串使用三个引号
my_string Hello
World
print(my_string)# 如果需要字符串在下一行继续使用反斜杠
my_string Hello \
World
print(my_string) I m a GeekHelloWorldHello World访问字符和子字符串
my_string Hello World# 使用索引获取字符
b my_string[0]
print(b)# 通过切片获取子字符串
b my_string[1:3] # 注意最后一个索引不包括
print(b)
b my_string[:5] # 从第一个元素开始
print(b)
b my_string[6:] # 直到最后
print(b)
b my_string[::2] # 从头到为每隔两个元素
print(b)
b my_string[::-1] # 使用负步长翻转列表
print(b) HelHelloWorldHloWrddlroW olleH连接两个或多个字符串
# 使用 拼接字符串
greeting Hello
name Tom
sentence greeting name
print(sentence)
Hello Tom迭代
# 使用for循环迭代列表
my_string Hello
for i in my_string:print(i) Hello检查字符或子字符串是否存在
if e in Hello:print(yes)
if llo in Hello:print(yes) yesyes有用的方法
my_string Hello World # 去除空格
my_string my_string.strip()
print(my_string)# 字符的个数
print(len(my_string))# 大小写
print(my_string.upper())
print(my_string.lower())# startswith 和 endswith
print(hello.startswith(he))
print(hello.endswith(llo))# 找到子字符串的第一个索引没有则返回 -1
print(Hello.find(o))# 计算字符或者子字符串的个数
print(Hello.count(e))# 使用其他字符串代替子字符串当且仅当子字符串存在时
# 注意原字符串保持不变
message Hello World
new_message message.replace(World, Universe)
print(new_message)# 将字符串切分为为列表
my_string how are you doing
a my_string.split() # default argument is
print(a)
my_string one,two,three
a my_string.split(,)
print(a)# 将列表拼接为字符串
my_list [How, are, you, doing]
a .join(my_list) # 给出的字符串是分隔符比如在每个元素之间添加
print(a) Hello World11HELLO WORLDhello world[how, are, you, doing][one, two, three]TrueTrue41Hello UniverseHow are you doing格式化
新样式使用 format() 方法旧样式使用 % 操作符。
# 使用大括号做占位符
a Hello {0} and {1}.format(Bob, Tom)
print(a)# 默认顺序时位置可以不写
a Hello {} and {}.format(Bob, Tom)
print(a)a The integer value is {}.format(2)
print(a)# 一些数字的特殊格式化规则
a The float value is {0:.3f}.format(2.1234)
print(a)
a The float value is {0:e}.format(2.1234)
print(a)
a The binary value is {0:b}.format(2)
print(a)# old style formatting by using % operator
# 旧的方式使用 % 操作符
print(Hello %s and %s % (Bob, Tom)) # 多个参数时必需是元组
val 3.14159265359
print(The decimal value is %d % val)
print(The float value is %f % val)
print(The float value is %.2f % val) Hello Bob and TomHello Bob and TomThe integer value is 2The float value is 2.123The float value is 2.123400e00The binary value is 10Hello Bob and TomThe decimal value is 10The float value is 10.123450The float value is 10.12f-Strings
从 Python 3.6 起可以直接在花括号内使用变量。
name Eric
age 25
a fHello, {name}. You are {age}.
print(a)
pi 3.14159
a fPi is {pi:.3f}
print(a)
# f-Strings 在运行时计算可以允许表达式
a fThe value is {2*60}
print(a) Hello, Eric. You are 25.Pi is 3.142The value is 120更多关于不变性和拼接
# 因为字符串不可变所以使用 或者 拼接字符串总是生成新的字符串
# 因此多个操作时更加耗时。使用 join 方法更快。
from timeit import default_timer as timer
my_list [a] * 1000000# bad
start timer()
a
for i in my_list:a i
end timer()
print(concatenate string with : %.5f % (end - start))# good
start timer()
a .join(my_list)
end timer()
print(concatenate string with join(): %.5f % (end - start)) concat string with : 0.34527concat string with join(): 0.01191# a[start:stop:step], 默认步长为 1
a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
b a[1:3] # 注意最后一个索引不包括
print(b)
b a[2:] # 直到最后
print(b)
b a[:3] # 从第一个元素开始
print(b)
a[0:3] [0] # 替换子部分需要可迭代
print(a)
b a[::2] # 从头到为每隔两个元素
print(b)
a a[::-1] # 使用负步长翻转列表
print(a)
b a[:] # 使用切片复制元素
print(b)
五、集合
集合是无序的容器数据类型它是无索引的可变的并且没有重复的元素。 集合用大括号创建。
my_set {apple, banana, cherry}创建集合
使用花括号或内置的 set 函数。
my_set {apple, banana, cherry}
print(my_set)# 或者使用 set 函数从可迭代对象创建比如列表元组字符串
my_set_2 set([one, two, three])
my_set_2 set((one, two, three))
print(my_set_2)my_set_3 set(aaabbbcccdddeeeeeffff)
print(my_set_3)# 注意一个空的元组不能使用 {} 创建这个会识别为字典
# 使用 set() 进行创建
a {}
print(type(a))
a set()
print(type(a)) {banana, apple, cherry}{three, one, two}{b, c, d, e, f, a}class dictclass set添加元素add方法
my_set set()# 使用 add() 方法添加元素
my_set.add(42)
my_set.add(True)
my_set.add(Hello)# 注意顺序不重要只会影响打印输出
print(my_set)# 元素已经存在是没有影响
my_set.add(42)
print(my_set) {True, 42, Hello}{True, 42, Hello}移除元素remove方法
# remove(x): 移除 x, 如果元素不存在则引发 KeyError 错误
my_set {apple, banana, cherry}
my_set.remove(apple)
print(my_set)# KeyError:
# my_set.remove(orange)# discard(x): 移除 x, 如果元素不存在则什么也不做
my_set.discard(cherry)
my_set.discard(blueberry)
print(my_set)# clear() : 移除所有元素
my_set.clear()
print(my_set)# pop() : 移除并返回随机一个元素
a {True, 2, False, hi, hello}
print(a.pop())
print(a) {banana, cherry}{banana}set()False{True, 2, hi, hello}检查元素是否存在
my_set {apple, banana, cherry}
if apple in my_set:print(yes) yes迭代
# 使用 for 循环迭代集合
# 注意顺序不重要
my_set {apple, banana, cherry}
for i in my_set:print(i) bananaapplecherry并集和交集
odds {1, 3, 5, 7, 9}
evens {0, 2, 4, 6, 8}
primes {2, 3, 5, 7}# union() : 合并来自两个集合的元素不重复
# 注意这不会改变两个集合
u odds.union(evens)
print(u)# intersection(): 选择在两个集合中都存在的元素
i odds.intersection(evens)
print(i)i odds.intersection(primes)
print(i)i evens.intersection(primes)
print(i) {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}set(){3, 5, 7}{2}集合的差
setA {1, 2, 3, 4, 5, 6, 7, 8, 9}
setB {1, 2, 3, 10, 11, 12}# difference() : 返回集合 setA 中不在集合 setB 中的元素的集合
diff_set setA.difference(setB)
print(diff_set)# A.difference(B) 与 B.difference(A) 不一样
diff_set setB.difference(setA)
print(diff_set)# symmetric_difference() : 返回集合 setA 和 setB 中不同时在两个集合中的元素的集合
diff_set setA.symmetric_difference(setB)
print(diff_set)# A.symmetric_difference(B) B.symmetric_difference(A)
diff_set setB.symmetric_difference(setA)
print(diff_set) {4, 5, 6, 7, 8, 9}{10, 11, 12}{4, 5, 6, 7, 8, 9, 10, 11, 12}{4, 5, 6, 7, 8, 9, 10, 11, 12}更新集合
setA {1, 2, 3, 4, 5, 6, 7, 8, 9}
setB {1, 2, 3, 10, 11, 12}# update() : 通过添加其他集合的元素进行更新
setA.update(setB)
print(setA)# intersection_update() : 通过保留共同的元素进行更新
setA {1, 2, 3, 4, 5, 6, 7, 8, 9}
setA.intersection_update(setB)
print(setA)# difference_update() : 通过移除与其他集合中相同的元素进行更新
setA {1, 2, 3, 4, 5, 6, 7, 8, 9}
setA.difference_update(setB)
print(setA)# symmetric_difference_update() : 通过保留只出现在一个集合而不出现在另一个集合中的元素进行更新
setA {1, 2, 3, 4, 5, 6, 7, 8, 9}
setA.symmetric_difference_update(setB)
print(setA)# 注意所有的更新方法同时适用于其他可迭代对象作为参数比如列表元组
# setA.update([1, 2, 3, 4, 5, 6]) {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}{1, 2, 3}{4, 5, 6, 7, 8, 9}{4, 5, 6, 7, 8, 9, 10, 11, 12}复制
set_org {1, 2, 3, 4, 5}# 只是引用的复制需要注意
set_copy set_org# 修改复制集合也会影响原来的集合
set_copy.update([3, 4, 5, 6, 7])
print(set_copy)
print(set_org)# 使用 copy() 真正复制集合
set_org {1, 2, 3, 4, 5}
set_copy set_org.copy()# 现在修改复制集合不会影响原来的集合
set_copy.update([3, 4, 5, 6, 7])
print(set_copy)
print(set_org) {1, 2, 3, 4, 5, 6, 7}{1, 2, 3, 4, 5, 6, 7}{1, 2, 3, 4, 5, 6, 7}{1, 2, 3, 4, 5}子集超集和不交集
setA {1, 2, 3, 4, 5, 6}
setB {1, 2, 3}
# issubset(setX): 如果 setX 包含集合返回 True
print(setA.issubset(setB))
print(setB.issubset(setA)) # True# issuperset(setX): 如果集合包含 setX返回 True
print(setA.issuperset(setB)) # True
print(setB.issuperset(setA))# isdisjoint(setX) : 如果两个集合交集为空比如没有相同的元素返回 True
setC {7, 8, 9}
print(setA.isdisjoint(setB))
print(setA.isdisjoint(setC)) FalseTrueTrueFalseFalseTrueFrozenset
Frozenset 只是普通集和的不变版本。 尽管可以随时修改集合的元素但 Frozenset 的元素在创建后保持不变。 创建方式
my_frozenset frozenset(iterable)a frozenset([0, 1, 2, 3, 4])# 以下操作不允许
# a.add(5)
# a.remove(1)
# a.discard(1)
# a.clear()# 同时更新方法也不允许
# a.update([1,2,3])# 其他集合操作可行
odds frozenset({1, 3, 5, 7, 9})
evens frozenset({0, 2, 4, 6, 8})
print(odds.union(evens))
print(odds.intersection(evens))
print(odds.difference(evens)) frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})frozenset()frozenset({1, 3, 5, 7, 9})