目录
Python是一种高级编程语言,由Guido van Rossum于1991年创建。它以简洁、易读的语法而闻名,并且具有强大的功能和广泛的应用领域。Python具有丰富的标准库和第三方库,可以用于开发各种类型的应用程序,包括Web开发、数据分析、人工智能、科学计算、自动化脚本等。
Python本身是一种伟大的通用编程语言,在一些流行的库(numpy,scipy,matplotlib)的帮助下,成为了科学计算的强大环境。本系列将介绍Python编程语言和使用Python进行科学计算的方法,主要包含以下内容:
Python 3.7
运行下述命令检查Python版本
python --version
Python中的容器是用于存储和组织数据的对象。常见的容器包括列表(List)、元组(Tuple)、集合(Set)和字典(Dictionary)。
my_list = [1, 2, 3, 'a', 'b', 'c']
my_tuple = (1, 2, 3, 'a', 'b', 'c')
my_set = {1, 2, 3, 'a', 'b', 'c'}
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
这些容器都提供了不同的方法和操作,用于对数据进行存储、访问和处理。可以根据具体的需求选择适合的容器类型。
集合(Set)是一种常见的数据结构。集合是无序且包含唯一元素的容器。它的特点是不允许重复的元素,并且可以进行交集、并集、差集等各种集合操作。集合(Set)不支持直接的拆包操作,因为集合是无序的,无法通过索引来确定元素的位置。
集合可以使用大括号{}或set()函数进行创建。
- my_set = {1, 2, 3} # 使用大括号创建集合
- my_set = set([1, 2, 3]) # 使用set()函数创建集合
与列表(List)和元组(Tuple)不同,集合中的元素是无序的,因此无法通过索引来访问集合中的元素。此外,集合中的元素必须是可哈希的(即不可变的),因为集合本身是基于哈希表实现的。我们可以使用循环或将集合转换为其他可索引的数据结构来访问元素:
a. 使用循环遍历集合中的元素
- my_set = {1, 2, 3, 4, 5}
- for element in my_set:
- print(element)
这将输出集合中的每个元素。
b. 转换为其他数据结构
将集合转换为列表(List)或元组(Tuple),然后通过索引访问元素。
- my_set = {1, 2, 3, 4, 5}
-
- my_list = list(my_set)
- print(my_list[0]) # 访问第一个元素
-
- my_tuple = tuple(my_set)
- print(my_tuple[2]) # 访问第三个元素
a. 添加单个元素(add)
my_set.add(5) # 添加单个元素
b. 添加多个元素(update)
my_set.update([6, 7, 8]) # 添加多个元素
c. 删除
移除集合中的元素可以使用remove()方法,如果元素不存在会引发KeyError异常;或使用discard()方法,如果元素不存在则不会引发异常。
- my_set.remove(3) # 移除指定元素,如果不存在会引发KeyError异常
- my_set.discard(4) # 移除指定元素,如果不存在不会引发异常
d. 判断元素是否存在于集合中
- my_set = {1, 2, 3}
- element = 3
- if element in my_set:
- print("元素存在于集合中")
这些操作可以使用相应的方法(如intersection()、union()、difference()、symmetric_difference())或运算符(如&、|、-、^)进行。
a. 交集
集合的交集是指包含同时存在于两个或多个集合中的所有元素的新集合。可以使用交集运算符(&)或intersection()方法来计算交集。
- set1 = {1, 2, 3, 4}
- set2 = {3, 4, 5, 6}
-
- # 使用交集运算符
- intersection = set1 & set2
- print(intersection)
-
- # 使用intersection()方法
- intersection = set1.intersection(set2)
- print(intersection)
输出结果为:
{3, 4}
b. 并集
集合的并集是指包含所有属于两个或多个集合的唯一元素的新集合。可以使用并集运算符(|)或union()方法来计算并集。
- set1 = {1, 2, 3}
- set2 = {3, 4, 5}
-
- # 使用并集运算符
- union = set1 | set2
- print(union)
-
- # 使用union()方法
- union = set1.union(set2)
- print(union)
输出结果为:
{1, 2, 3, 4, 5}
c. 差集
集合的差集是指从一个集合中去除属于另一个集合的所有元素后得到的新集合。可以使用差集运算符(-)或difference()方法来计算差集。
- set1 = {1, 2, 3, 4}
- set2 = {3, 4, 5}
-
- # 使用差集运算符
- difference = set1 - set2
- print(difference)
-
- # 使用difference()方法
- difference = set1.difference(set2)
- print(difference)
输出结果为:
{1, 2}
d. 对称差集
集合的对称差集是指包含属于两个集合中的唯一元素,但不包含同时存在于两个集合中的元素的新集合。可以使用对称差集运算符(^)或symmetric_difference()方法来计算对称差集。
- set1 = {1, 2, 3}
- set2 = {3, 4, 5}
-
- # 使用对称差集运算符
- symmetric_difference = set1 ^ set2
- print(symmetric_difference)
-
- # 使用symmetric_difference()方法
- symmetric_difference = set1.symmetric_difference(set2)
- print(symmetric_difference)
输出结果为:
{1, 2, 4, 5}
e. 总结
- set1 = {1, 2, 3}
- set2 = {3, 4, 5}
-
- intersection1 = set1.intersection(set2) # 交集
- intersection2 = set1 & set2 # 交集
-
- union1 = set1.union(set2) # 并集
- union2 = set1 | set2 # 并集
-
- difference1 = set1.difference(set2) # 差集
- difference2 = set1 - set2 # 差集
-
- symmetric_difference1 = set1.symmetric_difference(set2) # 对称差集
- symmetric_difference2 = set1 ^ set2 # 对称差集
