Python 元组与集合详解
·
元组(Tuple)
什么是元组
元组是Python中不可变的有序序列,用圆括号表示。
创建元组
python
thistuple = ("apple", "banana", "cherry")
print(thistuple)
访问元组元素
python
# 通过索引访问
thistuple = ("apple", "banana", "cherry")
print(thistuple[1]) # 输出: banana
# 负索引(从末尾开始)
print(thistuple[-1]) # 输出: cherry
# 索引范围切片
thistuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple[2:5]) # 输出: ('cherry', 'orange', 'kiwi')
# 负索引范围
print(thistuple[-4:-1]) # 输出: ('orange', 'kiwi', 'melon')
元组的不可变性
元组创建后不能修改,但可以通过转换间接修改:
python
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x) # 输出: ('apple', 'kiwi', 'cherry')
遍历元组
python
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
检查元素是否存在
python
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
print("Yes, 'apple' is in the fruits tuple")
元组操作
python
# 获取长度
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
# 单元素元组(必须加逗号)
thistuple = ("apple",)
print(type(thistuple)) # <class 'tuple'>
thistuple = ("apple")
print(type(thistuple)) # <class 'str'>
# 连接元组
tuple1 = ("a", "b", "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3) # 输出: ('a', 'b', 'c', 1, 2, 3)
# 删除整个元组
thistuple = ("apple", "banana", "cherry")
del thistuple
元组构造函数和方法
python
# 使用tuple()构造函数
thistuple = tuple(("apple", "banana", "cherry"))
print(thistuple)
# 元组方法
my_tuple = (1, 2, 3, 2, 4, 2)
print(my_tuple.count(2)) # 输出: 3(计算2出现的次数)
print(my_tuple.index(3)) # 输出: 2(查找3的位置)
集合(Set)
什么是集合
集合是无序、不重复元素的集合,用大括号表示。
创建集合
python
thisset = {"apple", "banana", "cherry"}
print(thisset)
访问集合元素
由于集合无序,不能通过索引访问:
python
thisset = {"apple", "banana", "cherry"}
# 遍历集合
for x in thisset:
print(x)
# 检查元素是否存在
print("banana" in thisset) # 输出: True
添加元素
python
thisset = {"apple", "banana", "cherry"}
# 添加单个元素
thisset.add("orange")
print(thisset)
# 添加多个元素
thisset.update(["orange", "mango", "grapes"])
print(thisset)
集合操作
python
# 获取集合长度
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
# 删除元素
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana") # 如果元素不存在会报错
thisset.discard("banana") # 如果元素不存在不会报错
# pop()删除任意元素
x = thisset.pop()
print(x)
print(thisset)
# 清空集合
thisset.clear()
print(thisset) # 输出: set()
# 删除整个集合
del thisset
集合运算
python
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
# 并集
set3 = set1.union(set2)
print(set3) # 输出: {1, 2, 3, 'a', 'b', 'c'}
# 使用update()合并
set1.update(set2)
print(set1) # 输出: {1, 2, 3, 'a', 'b', 'c'}
集合构造函数
python
thisset = set(("apple", "banana", "cherry"))
print(thisset)
集合方法总结
| 方法 | 描述 |
|---|---|
| add() | 添加元素到集合 |
| clear() | 移除集合所有元素 |
| copy() | 返回集合的副本 |
| difference() | 返回两个集合的差集 |
| difference_update() | 移除同时在两个集合中的元素 |
| discard() | 删除指定元素 |
| intersection() | 返回两个集合的交集 |
| intersection_update() | 移除不在其他指定集合中的元素 |
| isdisjoint() | 判断两个集合是否无交集 |
| issubset() | 判断是否为子集 |
| issuperset() | 判断是否为超集 |
| pop() | 移除任意元素 |
| remove() | 移除指定元素 |
| symmetric_difference() | 返回两个集合的对称差集 |
| symmetric_difference_update() | 插入两个集合的对称差集 |
| union() | 返回两个集合的并集 |
| update() | 用并集更新集合 |
总结
-
元组:有序、不可变序列,适合存储不应修改的数据
-
集合:无序、不重复元素集合,适合成员检测和去重操作
两者都是Python中重要的数据结构,根据具体需求选择使用。元组保证数据完整性,集合提供高效的成员检测和数学集合运算。
更多推荐



所有评论(0)