日期:2017-12-30
作者:秋的懵懂
# coding = utf-8
# ***********************************************************
# @brief 操作列表
# @author 魏文应
# @date 2017-12-26
# ***********************************************************
# ---------------------------------------------------------
# 遍历列表
print('\n\n_______________________________________________')
print("遍历列表:")
magicians = ['David', 'alice', 'carolina']
for magician in magicians:
print(magician)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 创建数值列表
print('\n\n_______________________________________________')
print("创建数值列表:")
# range()函数
for value in range(1, 5):
print(value)
# 创建列表
numbers = list(range(1, 5))
print(numbers)
# 指定步长为3
numbers = list(range(2, 10, 3))
print(numbers)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 简单数据统计
print('\n\n_______________________________________________')
print("简单数据统计:")
digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 最小值
print(min(digits))
# 最大值
print(max(digits))
# 求和
print(sum(digits))
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 列表解析
print('\n\n_______________________________________________')
print("列表解析:")
squares = [value**2 for value in range(1, 11)]
print(squares)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 切片
print('\n\n_______________________________________________')
print("切片:")
# 打印前面三个元素
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:3])
# 不指定,从头开始
print(players[:4])
# 不指定,一直到结束
print(players[2:])
# 打印最后三个元素
print(players[-3:])
# 遍历切片
for player in players[2:]:
print(player)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 复制列表
print('\n\n_______________________________________________')
print("复制列表:")
# 复制产生新的存储单元
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print('My favorite foods are:')
print(my_foods)
print('My friend favorite foods are:')
print(friend_foods)
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
# 关联到已有存储单元
friend_foods = my_foods
print('My favorite foods are:')
print(my_foods)
print('My friend favorite foods are:')
print(friend_foods)
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
print('_______________________________________________')
# ---------------------------------------------------------
# ---------------------------------------------------------
# 元组(不可变的列表)
print('\n\n_______________________________________________')
print("元组(不可变的列表):")
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
# 不能这样dimensions[1] = 34
# 虽然不能修改元组的元素, 但可以给存储元组的变量赋值
dimensions = (400, 10)
print(dimensions)
print('_______________________________________________')
# ---------------------------------------------------------