Python3 列表 | 菜鸟教程
Python3 元组 | 菜鸟教程
# 列表定义
bickcles = ['trek', 'cannondale', 'redline', 'specialized']
# 打印列表
print(bickcles)
# 访问列表
print(bickcles[0])
print(bickcles[1])
print(bickcles[-1])
print(bickcles[-2])
# 修改列表
bickcles[-1] = 'newLast'
print(bickcles)
# 确定列表长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
# 添加元素(末尾)
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
# 添加元素(任意位置)
motorcycles.insert(0, 'dacati') # 插入在0位置
print(motorcycles)
# 删除元素(任意位置)
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[1]
print(motorcycles)
# 弹出元素(末尾)
popped_motorcycle = motorcycles.pop()
print(motorcycles) # 弹出后的列表
print(popped_motorcycle) # 被弹出的值
# 弹出元素(任意位置)
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop(1) # 弹出1位置的元素
print(motorcycles) # 弹出后的列表
print(popped_motorcycle) # 被弹出的值
# 删除元素(任意值)
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('ducati') # 删除列表中第一次出现的值为'ducati'的元素
print(motorcycles)
# 判定元素是否在列表中(in关键字)
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print('mushrooms' in requested_toppings)
print('mushrooms' not in requested_toppings)
# 查找元素在列表中首次出现的位置
requested_toppings = ['mushrooms', 'onions', 'pineapple']
print(requested_toppings.index('onions'))
# 计算元素出现次数
requested_toppings = ['mushrooms', 'onions', 'pineapple',
'mushrooms', 'onions', 'pineapple',
'mushrooms', 'onions', 'pineapple',
'mushrooms', 'pineapple',
'pineapple']
print(requested_toppings.count('mushrooms'))
print(requested_toppings.count('onions'))
print(requested_toppings.count('pineapple'))
print(requested_toppings.count('0')) # 0次
# 判定列表是否为空
requested_toppings = []
if requested_toppings:
print("do something.")
else:
print("do others.")
# 对列表永久排序并按字母逆序输出
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
# 对列表临时排序并按字母逆序输出
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
print(sorted(cars))
print(sorted(cars, reverse=True))
print(cars)
# 逆转列表存储
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.reverse()
print(cars)
# 遍历列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
# range()函数
for value in range(1, 5): # [1,5)
print(value)
# 使用range()生成数值列表
numbers = list(range(1, 6)) # [1,6),默认步长1
print(numbers)
even_numbers = list(range(2, 11, 2)) # [2,11),步长2
print(even_numbers)
# 使用range()充当for计数循环
squares = []
for value in range(1, 11):
squares.append(value**2)
print(squares)
##等价于
#列表解析
squares = [value**2 for value in range(1, 11)]
print(squares)
# 列表切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) # [0,3)
print(players[:4]) # [0,4)
print(players[2:]) # [2,4]
print(players[-3:]) # {-3,-2,-1}即最后3名
# 复制列表
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:] # 同时省略起始索引和终止索引
# 将my_foods的一个副本赋给friend_foods
# 若不使用切片,则仅仅将引用赋给了新变量,两者指向同一列表
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(" my_foods:", my_foods)
print("friend_foods:", friend_foods)
Python将不能修改的值称为不可变的
不可变的列表称为元组
# 元组定义
dimensions = (200, 50)
my_t = (3,) # 严格地说,元组是由逗号标识的
# 虽然不能修改元组的元素,但是可以重新给整个元组赋值
dimensions = (200, 50)
dimensions = (400, 100)
# 遍历元组,语法基本与列表相同
for dimension in dimensions:
print(dimension)