第六天
元组
元组是有序且不可更改(不可变)的不同数据类型的集合。元组用圆括号 () 书写。一旦元组被创建,我们就不能改变它的值。我们不能在元组中使用添加、插入、删除方法,因为它是不可修改的(可变的)。与列表不同,元组的方法很少。与元组相关的方法:
- tuple():创建一个空元组
- count():统计元组中指定项的个数
- index():查找元组中指定项的索引
- 运算符:连接两个或多个元组并创建一个新元组
创建元组
空元组:创建一个空元组
# syntax
empty_tuple = ()
# or using the tuple constructor
empty_tuple = tuple()
具有初始值的元组
# syntax
tpl = ('item1', 'item2','item3')
fruits = ('banana', 'orange', 'mango', 'lemon')
元组长度
我们使用len()方法来获取元组的长度。
# syntax
tpl = ('item1', 'item2', 'item3')
len(tpl)
访问元组项
正索引与列表数据类型类似,我们使用正索引或负索引来访问元组项。
# Syntax
tpl = ('item1', 'item2', 'item3')
first_item = tpl[0]
second_item = tpl[1]
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[0]
second_fruit = fruits[1]
last_index =len(fruits) - 1
last_fruit = fruits[las_index]
负索引 负索引表示从末尾开始,-1 表示最后一项,-2 表示倒数第二项,列表/元组长度的负数表示第一项。
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
first_item = tpl[-4]
second_item = tpl[-3]
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[-4]
second_fruit = fruits[-3]
last_fruit = fruits[-1]
切片元组
我们可以通过在元组中指定从哪里开始和从哪里结束的索引范围来切出一个子元组,返回值将是一个具有指定项的新元组。
-
正指数范围
# Syntax tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[0:4] # all items all_items = tpl[0:] # all items middle_two_items = tpl[1:3] # does not include item at index 3
fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[0:4] # all items all_fruits= fruits[0:] # all items orange_mango = fruits[1:3] # doesn't include item at index 3 orange_to_the_rest = fruits[1:]
-
负指标范围
# Syntax tpl = ('item1', 'item2', 'item3','item4') all_items = tpl[-4:] # all items middle_two_items = tpl[-3:-1] # does not include item at index 3 (-1)
fruits = ('banana', 'orange', 'mango', 'lemon') all_fruits = fruits[-4:] # all items orange_mango = fruits[-3:-1] # doesn't include item at index 3 orange_to_the_rest = fruits[-3:]
将元组更改为列表
我们可以将元组更改为列表,将列表更改为元组。元组是不可变的,如果我们想修改元组,我们应该将其更改为列表。
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
lst = list(tpl)
fruits = ('banana', 'orange', 'mango', 'lemon')
fruits = list(fruits)
fruits[0] = 'apple'
print(fruits) # ['apple', 'orange', 'mango', 'lemon']
fruits = tuple(fruits)
print(fruits) # ('apple', 'orange', 'mango', 'lemon')
检查元组中的项目
我们可以使用in检查一个项目是否存在于元组中,它返回一个布尔值。
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
'item2' in tpl # True
fruits = ('banana', 'orange', 'mango', 'lemon')
print('orange' in fruits) # True
print('apple' in fruits) # False
fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment
加入元组
我们可以使用 + 运算符连接两个或多个元组
# syntax
tpl1 = ('item1', 'item2', 'item3')
tpl2 = ('item4', 'item5','item6')
tpl3 = tpl1 + tpl2
fruits = ('banana', 'orange', 'mango', 'lemon')
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
fruits_and_vegetables = fruits + vegetables
删除元组
不可能删除元组中的单个项目,但可以使用del删除元组本身。
# syntax
tpl1 = ('item1', 'item2', 'item3')
del tpl1
fruits = ('banana', 'orange', 'mango', 'lemon')
del fruits
🌕你真勇敢,你能走到这一步。您刚刚完成了第 6 天的挑战,您已经迈出了通往卓越之路的 6 步。现在为您的大脑和肌肉做一些锻炼。
练习: 第六天
练习:1级
- 创建一个空元组
- 创建一个包含你姐妹和兄弟名字的元组(想象中的兄弟姐妹都可以)
- 加入兄弟姐妹元组并分配给兄弟姐妹
- 你有几个兄弟姐妹?
- 修改siblings元组,加入你爸爸妈妈的名字赋给family_members
练习:2级
- 从 family_members 中解压兄弟姐妹和父母
- 创建水果、蔬菜和动物产品元组。连接三个元组并将其分配给一个名为 food_stuff_tp 的变量。
- 将 about food_stuff_tp 元组更改为 food_stuff_lt 列表
- 从 food_stuff_tp 元组或 food_stuff_lt 列表中切出一个或多个中间项。
- 从 food_staff_lt 列表中切出前三项和后三项
- 完全删除 food_staff_tp 元组
- 检查一个项目是否存在于元组中:
-
检查“Estoni”是否为北欧国家
-
检查“Iceland”是否为北欧国家
nordic_countries = ('Denmark', 'Finland','Iceland', 'Norway', 'Sweden')