列表简介

一.列表是什么

  1. bicycles = ['trek', 'cannondale', 'redline', 'specialized']
  2. print(bicycles)
  1. ['trek', 'cannondale', 'redline', 'specialized']

1.访问列表元素

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

2.索引从0而不是1开始

  1. bicycles = ['trek', 'cannondale', 'redline', 'specialized']
  2. print(bicycles[1])
  3. print(bicycles[3])
  1. cannondale
  2. specialized

Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1 ,可让Python返回最后一个列表元素:

  1. bicycles = ['trek', 'cannondale', 'redline', 'specialized']
  2. print(bicycles[-1])

3.使用列表中的各个值

  1. bicycles = ['trek', 'cannondale', 'redline', 'specialized']
  2. message = "My first bicycle was a " + bicycles[0].title() + "."
  3. print(message)
  1. My first bicycle was a Trek.

二.修改、添加和删除元素

1.修改列表元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. print(motorcycles)
  3. motorcycles[0] = 'ducati'
  4. print(motorcycles)
  1. ['honda', 'yamaha', 'suzuki']
  2. ['ducati', 'yamaha', 'suzuki']

2.在列表中添加元素

①在列表末尾添加元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. print(motorcycles)
  3. motorcycles.append('ducati')
  4. print(motorcycles)
  1. ['honda', 'yamaha', 'suzuki']
  2. ['honda', 'yamaha', 'suzuki', 'ducati']

②在列表中插入元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. motorcycles.insert(0, 'ducati')
  3. print(motorcycles)
  1. ['ducati', 'honda', 'yamaha', 'suzuki']

3.从列表中删除元素

①使用del语句删除元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. print(motorcycles)
  3. del motorcycles[0]
  4. print(motorcycles)
  1. ['honda', 'yamaha', 'suzuki']
  2. ['yamaha', 'suzuki']

②使用方法pop()删除元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. print(motorcycles)
  3. popped_motorcycle = motorcycles.pop()
  4. print(motorcycles)
  5. print(popped_motorcycle)
  1. ['honda', 'yamaha', 'suzuki']
  2. ['honda', 'yamaha']
  3. suzuki

③弹出列表中任何位置处的元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki']
  2. first_owned = motorcycles.pop(0)
  3. print('The first motorcycle I owned was a ' + first_owned.title() + '.')
  1. The first motorcycle I owned was a Honda.

④根据值删除元素

  1. motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
  2. print(motorcycles)
  3. motorcycles.remove('ducati')
  4. print(motorcycles)
  1. ['honda', 'yamaha', 'suzuki', 'ducati']
  2. ['honda', 'yamaha', 'suzuki']

三.组织列表

1.使用方法sort()对列表进行永久性排序

  1. cars = ['bmw', 'audi', 'toyota', 'subaru']
  2. cars.sort()
  3. print(cars)
  1. ['audi', 'bmw', 'subaru', 'toyota']

2.使用函数sorted()对列表进行临时排序

  1. cars = ['bmw', 'audi', 'toyota', 'subaru']
  2. print("Here is the original list:")
  3. print(cars)
  4. print("\nHere is the sorted list:")
  5. print(sorted(cars))
  6. print("\nHere is the original list again:")
  7. print(cars)
  1. Here is the original list:
  2. ['bmw', 'audi', 'toyota', 'subaru']
  3. Here is the sorted list:
  4. ['audi', 'bmw', 'subaru', 'toyota']
  5. Here is the original list again:
  6. ['bmw', 'audi', 'toyota', 'subaru']

3.倒着打印列表

  1. cars = ['bmw', 'audi', 'toyota', 'subaru']
  2. print(cars)
  3. cars.reverse()
  4. print(cars)
  1. ['bmw', 'audi', 'toyota', 'subaru']
  2. ['subaru', 'toyota', 'audi', 'bmw']

4.确定列表的长度

  1. >>> cars = ['bmw', 'audi', 'toyota', 'subaru']
  2. >>> len(cars)
  3. 4

操作列表

一.遍历整个列表

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

1.在for循环中执行更多的操作

  1. magicians = ['alice', 'david', 'carolina']
  2. for magician in magicians:
  3. print(magician.title() + ", that was a great trick!"
  1. Alice, that was a great trick!
  2. David, that was a great trick!
  3. Carolina, that was a great trick!

2.在for循环结束后执行一些操作

  1. magicians = ['alice', 'david', 'carolina']
  2. for magician in magicians:
  3. print(magician.title() + ", that was a great trick!")
  4. print("I can't wait to see your next trick, " + magician.title() + ".\n")
  5. print("Thank you, everyone. That was a great magic show!")
  1. Alice, that was a great trick!
  2. I can't wait to see your next trick, Alice.
  3. David, that was a great trick!
  4. I can't wait to see your next trick, David.
  5. Carolina, that was a great trick!
  6. I can't wait to see your next trick, Carolina.
  7. Thank you, everyone. That was a great magic show!

二.创建数值列表

1.使用函数range()

  1. for value in range(1,5):
  2. print(value)
  1. 1
  2. 2
  3. 3
  4. 4

2.使用range()创建数字列表

要创建数字列表,可使用函数list()将range()的结果直接转换为列表。如果将range() 作为list() 的参数,输出将为一个数字列表。

  1. numbers = list(range(1,6))
  2. print(numbers)
  1. [1, 2, 3, 4, 5]
  1. even_numbers = list(range(2,11,2))
  2. print(even_numbers)

在这个示例中,函数range() 从2开始数,然后不断地加2,直到达到或超过终值(11),因此输出如下:

  1. [2, 4, 6, 8, 10]

3.对数字列表执行简单的统计运算

  1. >>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
  2. >>> min(digits)
  3. 0
  4. >>> max(digits)
  5. 9
  6. >>> sum(digits)
  7. 45

4.列表解析

  1. squares = [value**2 for value in range(1,11)]
  2. print(squares)
  1. [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

三.使用列表的一部分

1.切片

  1. players = ['charles', 'martina', 'michael', 'florence', 'eli']
  2. print(players[0:3])
  1. ['charles', 'martina', 'michael']
  1. players = ['charles', 'martina', 'michael', 'florence', 'eli']
  2. print(players[:4])
  1. ['charles', 'martina', 'michael', 'florence']
  1. players = ['charles', 'martina', 'michael', 'florence', 'eli']
  2. print(players[2:])
  1. ['michael', 'florence', 'eli']

2.遍历切片

  1. players = ['charles', 'martina', 'michael', 'florence', 'eli']
  2. print("Here are the first three players on my team:")
  3. for player in players[:3]:
  4. print(player.title())
  1. Here are the first three players on my team:
  2. Charles
  3. Martina
  4. Michael

3.复制列表

  1. my_foods = ['pizza', 'falafel', 'carrot cake']
  2. friend_foods = my_foods[:]
  3. print("My favorite foods are:")
  4. print(my_foods)
  5. print("\nMy friend's favorite foods are:")
  6. print(friend_foods)
  1. My favorite foods are:
  2. ['pizza', 'falafel', 'carrot cake']
  3. My friend's favorite foods are:
  4. ['pizza', 'falafel', 'carrot cake']

四.元组

1.定义元组

  1. dimensions = (200, 50)
  2. print(dimensions[0])
  3. print(dimensions[1])
  1. 200
  2. 50
  1. dimensions = (200, 50)
  2. dimensions[0] = 250
  1. Traceback (most recent call last):
  2. File "dimensions.py", line 3, in <module>
  3. dimensions[0] = 250
  4. TypeError:'tuple' object does not support item assignment

2.遍历元组中的所有值

  1. dimensions = (200, 50)
  2. for dimension in dimensions:
  3. print(dimension)
  1. 200
  2. 50

3.修改元组变量

  1. dimensions = (200, 50)
  2. print("Original dimensions:")
  3. for dimension in dimensions:
  4. print(dimension)
  5. dimensions = (400, 100)
  6. print("\nModified dimensions:")
  7. for dimension in dimensions:
  8. print(dimension)
  1. Original dimensions:
  2. 200
  3. 50
  4. Modified dimensions:
  5. 400
  6. 100