如何把元组变成列表_【python零基础入门】基础语法之使用列表(下)

1. 使用列表的一部分

1.1 切片

要创建切片,可指定要使用的第一个元素的索引和最后一个元素的索引加1。与函数range()一样,python在达到你指定的第二个索引前面的元素后停止。要输出列表中的前三个元素,需要指定索引0-3,这将分别输出索引为0,1,2的元素。
示例,输出由NBA球员列表的前三个球员所组成的新列表。
输入:

1players=['Durant','LeBron','Michael','Kawhi','James']
2print(players[0:3])

输出:

1['Durant', 'LeBron', 'Michael']

同理,要提取列表的第2-4个元素,可将起始索引指定为1,终止索引指定为4:

1players=['Durant','LeBron','Michael','Kawhi','James']
2print(players[1:4])

输出:

1['LeBron', 'Michael', 'Kawhi']

如果没有指定起始索引,python将从列表的第一个开始切片:

1players=['Durant','LeBron','Michael','Kawhi','James']
2print(players[:4])

输出:

1['Durant', 'LeBron', 'Michael', 'Kawhi']

同理,如果没有指定终止索引,python则会默认到最后一个元素结尾:

1players=['Durant','LeBron','Michael','Kawhi','James']
2print(players[2:])

输出:

1['Michael', 'Kawhi', 'James']

在前面学过,负数索引返回离列表末尾相应距离的元素,例如还可以通过下面这种方式输出名单上的最后三名球员:

1players=['Durant','LeBron','Michael','Kawhi','James']
2print(players[-3:])

输出:

1['Michael', 'Kawhi', 'James']

1.2 遍历切片

有时候,我们只想要遍历列表中的部分元素,可以在for循环中使用切片。在下面的实例中,遍历前三个球员的名字,并打印他们的名字。

1players=['Durant','LeBron','Michael','Kawhi','James']
2print('Here are the top3 players:')
3for player in players[0:3]:
4    print(player)

输出:

1Here are the top3 players:
2Durant
3LeBron
4Michael

2. 复制列表

复制列表指的是将列表1中的元素复制到列表2中,例如假设有一个列表A,包含了我喜欢的NBA球员,现在创建一个列表B,包含我朋友喜欢的NBA球员,由于我喜欢的球员恰好我朋友也喜欢,于是可以将列表A中的元素全部复制到列表B中。具体的操作方法是,创建一个切片,但同时省略起始索引和终止索引([:])
输入:

1my_favourite_players=['Durant','LeBron','Michael','Kawhi','James']
2friend_favourite_players=my_favourite_players[:]
3print(my_favourite_players)
4print(friend_favourite_players)

输出:

1['Durant', 'LeBron', 'Michael', 'Kawhi', 'James']
2['Durant', 'LeBron', 'Michael', 'Kawhi', 'James']

为了核实我们确实有两个列表,下面在每个列表下面都添加一位球员,并核实每个列表都记录了相应人员喜欢的球员:

1my_favourite_players=['Durant','LeBron','Michael','Kawhi','James']
2friend_favourite_players=my_favourite_players[:]
3my_favourite_players.append('Kobe')
4friend_favourite_players.append('Yao Ming')
5print(my_favourite_players)
6print(friend_favourite_players)

输出:

1['Durant', 'LeBron', 'Michael', 'Kawhi', 'James', 'Kobe']
2['Durant', 'LeBron', 'Michael', 'Kawhi', 'James', 'Yao Ming']

复制列表要使用切片才行,下面这个例子展示了在不使用切片的情况下复制列表的情况。

1my_favourite_players=['Durant','LeBron','Michael','Kawhi','James']
2friend_favourite_players=my_favourite_players
3my_favourite_players.append('Kobe')
4friend_favourite_players.append('Yao Ming')
5print(my_favourite_players)
6print(friend_favourite_players)

输出:

1['Durant', 'LeBron', 'Michael', 'Kawhi', 'James', 'Kobe', 'Yao Ming']
2['Durant', 'LeBron', 'Michael', 'Kawhi', 'James', 'Kobe', 'Yao Ming']

可见两个列表的输出结果一样,这是因为事实上由于没有使用切片,现在只有一个列表存在。因此往friend_favourite_players列表和my_favourite_players列表中增加元素实际上是在往同一个列表增加元素。

3. 元组

列表是可以修改的,因此列表非常适合用于存储一些可能发生变化的动态值,然而有时候我们需要创建一系列不可以修改的元素,这就是元组。元组指的就是不可变的列表。

3.1 定义元组

元组看起来和列表很像,但是使用的是圆括号而不是方括号来标识。定义元组后,也可以使用索引来访问其中的元素。
例如有一个大小不应该改变的矩阵,可以将其长度和宽度存储在一个元组中,从儿确保它们是无法被修改的:

1dimensions=(200,50)
2print(dimensions[0])
3print(dimensions[1])

输出:

1200
250

下面尝试修改元组中的一个元素,看看结果如何:

1dimensions=(200,50)
2dimensions[0]=10
3print(dimensions[0])

输出:

1Traceback (most recent call last):
2  File "C:/Users/user/PycharmProjects/test1/learn3.py", line 41, in <module>
3    dimensions[0]=10
4TypeError: 'tuple' object does not support item assignment

可以看到,是无法被修改的。

3.2 遍历元组中的所有值

像列表一样,使用for循环来遍历元组中的所有值:

1dimensions=(200,50)
2for dimension in dimensions:
3    print(dimension)

输出:

1200
250

3.3 修改元组变量

元组中的元素是无法被修改的,但是可以给存储元组的变量赋值,也就是说重新定义。例如现在想要修改整个矩阵的尺寸:

1dimensions=(200,50)
2print('Original dimensions:')
3for dimension in dimensions:
4    print(dimension)
5
6dimensions=(200,80)
7print('\nModified dimensions:')
8for dimension in dimensions:
9    print(dimension)

输出:

1Original dimensions:
2200
350
4
5Modified dimensions:
6200
780

可见,给元组变量赋值是合法的。

end

1ad1aede2e7eebd73c7b9208ebec218a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值