Python Learning(4) 操作列表

Python编程从入门到实践:

# -*- coding: utf-8 -*-

"""
chapter4 操作列表

"""

import math

#  4.1 遍历列表 for循环
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

#  4.1.1 深入的研究循环
#  4.1.2 在for循环中执行更多的操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ', that was a great trick!')

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print("i can't wait to see your next trick" + magician.title() + '.\n')

# 4.1.3 在for循环结束后执行一些操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician.title() + ', that was a great trick!')
    print("i can't wait to see your next trick" + magician.title() + '.\n')
print('thanks everyone,that was a great show')  # 不缩进 将在循环结束后执行

# 4.2 避免缩进错误
'''
一些常见的缩进错误:
1.忘记缩进-python使用缩进代码行与前一个代码行的关系;
2.忘记缩进额外的代码行-注意你的逻辑,你要对便利出来的每个元素执行操作,还是要在循环结束后,执行操作;
3.不必要的缩进
4.遗忘了for之后的冒号
'''

# 4.3 创建数值列表
# 4.3.1 使用函数range()生成一系列数字
for number in range(1, 5):  # 左闭右开 ,实际上会打印1,2,3,4 而不会打印出5
    print(number)
for number2 in range(5):  # 会打印1,2,3,4
    print(number2)

# 4.3.2 使用range() 创建数字列表
numbers = list(range(6))  # list() 函数将range()转换为列表
print(range(6))
print(numbers)

# 使用range()可以指定步长
even_number = list(range(2, 11, 2))
print(even_number)

# 将1-10的整数的平方放到一个列表中
squars = []
for value in range(1, 10):
    squar = value ** 2
    squars.append(squar)
print(squars)

# 更简洁的写法
squars = []
for value in range(1, 10):
    squars.append(value ** 2)  # 不使用临时变量
print(squars)

# 4.3.3 对数字列表执行更简单的统计计算
digit = range(1, 11)
digit1 = list(range(1, 11))
print(digit)  # range(1, 11)
print(digit1)  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(min(digit1))  # 1
print(max(digit1))  # 10
print(sum(digit1))  # 55
print(min(digit))  # 1
print(max(digit))  # 55
print(sum(digit))  # 55

# 4.3.4 列表解析
# 列表解析将for循环和创建新元素的代码合并为一行,并且自动附加新元素
squars2 = [value ** 3 for value in list(range(1, 11))]
print(squars2)

# 使用for打印1-20 .
number = [value * 1 for value in range(1, 21)]
print(number)

# 打印1-1000000
million = [value for value in range(1, 50)]
print(million)

# 打印1-20之间的奇数
odd_number = list(range(1, 21, 2))
print(odd_number)

# 3的倍数
Triples = []
for value in range(1, 31):
    if value % 3 == 0:
        Triples.append(value)
print([value for value in Triples])

# 4.4 使用列表的一部分 (列表的部分元素 - 切片)
# 4.4.1 切片
players = ['赵四', '刘能', '谢广坤', '王大脑袋', '菜虚鲲']
print(players[0:3])  # 切片包含索引为0,1,2的元素
print(players[:4])  # 如果没有指定开始索引,将从头开始
print(players[2:])  # 如果么有指定结束索引,将一直到末尾
print(players[:])  # [:] 切片将包含列表所有元素
print(players[-3:])  # 输出列表最后三个元素

# 4.4.2 遍历切片
players = ['赵四', '刘能', '谢广坤', '王大脑袋', '菜虚鲲']
print('有请前三位出场:')
for superStar in players[:3]:
    print(superStar)

# 4.4.3 复制列表
my_course = ['语文', '数学', '英语', '科学']
firends_course = my_course[:]
print(my_course)
print(firends_course)
my_course.append('养猪')
firends_course.append('篮球')
print(my_course)
print(firends_course)  # 说明我们确实通过切片的方式获得了两个列表

print('The first three items in the list are:')
print(my_course[:3])
print('Three items from the middle of the list are:')
print(math.ceil(len(my_course) / 2))
print(my_course[(math.ceil(len(my_course) / 2)) - 2: math.ceil(len(my_course) / 2) + 1])
print(' The last three items in the list are:')
print(my_course[-3:])

# 4.5 元组
# 列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的;
# Python将不能修改的值称为不可变的,而不可变的列表被称为元组。

# 4.5.1 定义元组
# 元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来.访问其元素,就像访问列表元素一样。
dimensions = (200, 50)
for value in dimensions:
    print(value)
print([value for value in dimensions])  # [200,50]
print((value for value in dimensions))  # [200,50]  <generator object <genexpr> at 0x0106FA30>
'''
错误(或者说是提示,因为这可能不是一个错误) :  <generator object <genexpr> at 0x0106FA30>    
参考1:https://stackoverflow.com/questions/23566431/get-an-error-generator-object-genexpr-at-0x0000000002731828-using-pydev
参考2:https://www.cnblogs.com/MnCu8261/p/6410594.html
reason:
In Python 3, print() is a function, not a statement.
A generator expression is like a list comprehension, except it creates an object that produces results when you iterate 
over it, not when you create it. For example,
>[i*i for i in range(5)]
produces a list, [0, 1, 4, 9, 16], while
>(i*i for i in range(5))
produces a generator object that will produce those numbers when you iterate over it.
'''
# dimensions[0] = 100  # TypeError: 'tuple' object does not support item assignment

# 4.5.2 遍历元组中的所有值
dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)

# 4.5.3 修改元组变量
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
    print(dimension)

dimensions = (20, 30)
print("\nModified dimensions:")  # 虽然不能修改元组的元素,但可以给存储元组的变量赋值。即dimensions是可以修改的.
for dimension in dimensions:  # 如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组
    print(dimension)


#  4.6 设置代码格式
# 若要提出Python语言修改建议,需要编写Python改进提案(Python Enhancement Proposal,
# PEP)。 PEP 8是最古老的PEP之一,它向Python程序员提供了代码格式设置指南
# PEP 8建议每级缩进都使用四个空格
posted @ 2019-04-18 23:27  XueXueLai  阅读(177)  评论(0编辑  收藏  举报