2019-03-17
阅读量:
765
python如何列表切片(处理部分列表元素)?
与range()一样,指定要使用的第一个元素和最后一个元素的索引,到达指定的第二个索引值前面的元素后停止。
players = ['charles','martina','michael','florence','eli']
print(players[0:3])
>>>['charles', 'martina', 'michael']
未指定起始索引及终止索引的情况:
players = ['charles','martina','michael','florence','eli']
print(players[:4])
>>>['charles', 'martina', 'michael', 'florence']
players = ['charles','martina','michael','florence','eli']
print(players[1:])
>>>['martina', 'michael', 'florence', 'eli']
players = ['charles','martina','michael','florence','eli']
print(players[-3:])
>>>['michael', 'florence', 'eli']






评论(0)


暂无数据
推荐帖子
2条评论
6条评论
7条评论