chap3&4列表

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
print(bicycles[0].title())

1.索引

通过将索引指定为-1,可让Python返 回最后一个列表元素。索引-2返回倒数第二个列表元素, 索引-3返回倒数第三个列表元素,以此类推。

essage = "My first bicycle was a " + bicycles[0].title() + "."

2.修改

在列表末尾添加元素

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles)
motorcycles.append('ducati') 
print(motorcycles)

为控制用户,可首先创建一个空列表,用于存储用户将要输入的值,然后将用户提供的 每个新值附加到列表中。

3.删除

del删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]

pop删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()  #删除末尾元素
first_owned = motorcycles.pop(0)  #删除任意位置元素

如果你要从列表 中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续 使用它,就使用方法pop()。

根据值删除元素

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')

4.整理排序

sort()永久性排序,按首字母(假设全是小写

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort()
cars.sort(reverse=True)    #反向

sorted()临时性排序
reverse()反转顺序
len()确定列表长度
发生索引错误却找不到解决办法时,请尝试将列表或其长度打印出来。列表可能与你以为的截然不同,在程序对其进行了动态处理时尤其如此。通过查看列表或其包含的元素数,可帮助你找出这种逻辑错误。

5.遍历

for循环

magicians = ['alice', 'david', 'carolina'] 
for magician in magicians:
	print(magician)

对于用于存储列表中每个值的临时变量,可指定任何名称。
使用单数和复数式名称, 可帮助你判断代码段处理的是单个列表元素还是整个列表。

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")

在for循环后面,没有缩进的代码都只执行一次,而不会重复执行。
为避免意外缩进错误,请只缩进需要缩进的代码。
注意for后面的冒号

6.数值

range()

for value in range(1,5):
	print(value)

输出数字1 2 3 4

将输出的数字直接转换成列表list()

numbers = list(range(1,6))
print(numbers)
even_numbers = list(range(2,11,2))  #指定步长

square.py

squares = []
for value in range(1,11): 
	square = value**2
	squares.append(square)
print(squares)

或者

squares = []
for value in range(1,11):
	squares.append(value**2)

或者

squares = [value**2 for value in range(1,11)] 
print(squares)

min() max() sum()计算

7.列表的部分

切片

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])   #第1 2 3个元素
print(players[1:4])   #第2 3 4个元素
print(players[:4])   #前4个元素
print(players[2:])   #到末尾
print(players[-3:])   #最后3个元素

for player in players[:3]:
         print(player.title())   #遍历
         
friend_foods = my_foods[:]   #复制

复制和赋值的区别:将my_foods赋给friend_foods,而不是将my_foods的副本存储到friend_foods。 这种语法实际上是让Python将新变量friend_foods关联到包含在my_foods中的列表,因此这两个 变量都指向同一个列表。鉴于此,当我们将’cannoli’添加到my_foods中时,它也将出现在 friend_foods中;同样,虽然’ice cream’好像只被加入到了friend_foods中,但它也将出现在这 两个列表中。

8.元组

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

for dimension in dimensions:
print(dimension)   #遍历

试图修改元组的操作是被禁止的,给元组变量赋值是合法的
相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都 不变,可使用元组。

9.格式

PEP 8建议每级缩进都使用四个空格
建议 注释的行长都不超过72字符
https://www.python.org/dev/peps/pep-0008/

4-3 数到 20:使用一个 for 循环打印数字 1~20(含)。
4-4 一百万:创建一个列表,其中包含数字 1~1 000 000,再使用一个 for 循环将这 些数字打印出来(如果输出的时间太长,按 Ctrl + C 停止输出,或关闭输出窗口)。
4-5 计算 1~1 000 000 的总和:创建一个列表,其中包含数字 1~1 000 000,再使用 min()和 max()核实该列表确实是从 1 开始,到 1 000 000 结束的。另外,对这个列表调 用函数 sum(),看看 Python 将一百万个数字相加需要多长时间。
4-6 奇数:通过给函数 range()指定第三个参数来创建一个列表,其中包含 1~20 的 奇数;再使用一个 for 循环将这些数字都打印出来。
4-7 3 的倍数:创建一个列表,其中包含 3~30 内能被 3 整除的数字;再使用一个 for 循环将这个列表中的数字都打印出来。
4-8 立方:将同一个数字乘三次称为立方。例如,在 Python 中,2 的立方用 2**3 表示。请创建一个列表,其中包含前 10 个整数(即 1~10)的立方,再使用一个 for 循 环将这些立方数都打印出来。
4-9 立方解析:使用列表解析生成一个列表,其中包含前 10 个整数的立方。
4-10 切片:选择你在本章编写的一个程序,在末尾添加几行代码,以完成如下任务。 打印消息“The first three items in the list are:”,再使用切片来打印列表的前三个元素。
打印消息“Three items from the middle of the list are:”,再使用切片来打印列表中
间的三个元素。
打印消息“The last three items in the list are:”,再使用切片来打印列表末尾的三
个元素。
4-11 你的比萨和我的比萨:在你为完成练习 4-1 而编写的程序中,创建比萨列表的
副本,并将其存储到变量 friend_pizzas 中,再完成如下任务。
 在原来的比萨列表中添加一种比萨。
在列表 friend_pizzas 中添加另一种比萨。 核实你有两个不同的列表。为此,打印消息“My favorite pizzas are:”,再使用一个 for 循环来打印第一个列表;打印消息“My friend’s favorite pizzas are:”,再使用一个 for 循环来打印第二个列表。核实新增的比萨被添加到了正确的列表中。
4-12 使用多个循环:在本节中,为节省篇幅,程序 foods.py 的每个版本都没有使用 for 循环来打印列表。请选择一个版本的 foods.py,在其中编写两个 for 循环,将各个
食品列表都打印出来。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值