Python编程:从入门到实践—列表

访问列表中的元素

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

修改、添加和删除元素

修改列表元素

>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> motorcycles[0] = 'ducati'
>>> print(motorcycles)
['ducati', 'yamaha', 'suzuki']

在列表中添加元素

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

在列表中插入元素

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

从列表中删除元素

>>> motorcycles = ['honda','yamaha','suzuki']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> del motorcycles[0]
>>> print(motorcycles)
['yamaha', 'suzuki']

使用方法pop()删除元素

方法pop()删除列表末尾的元素,并可以使用它

>>> motorcycles = ['honda','yamaha','suzuki']
>>> popped_motorcycle = motorcycles.pop()
>>> print(motorcycles)
['honda', 'yamaha']
>>> print(popped_motorcycle)
suzuki

应用:

>>> motorcycles = ['honda','yamaha','suzuki']
>>> last_owned = motorcycles.pop()
>>> print("The last motorcycles I owned was a " + last_owned.title() + ".")
The last motorcycles I owned was a Suzuki.

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

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

每当使用pop(),被弹出的元素就不再在列表中了。

使用值删除元素

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

使用remove()从列表中删除的元素,也可以接着使用它的值。

>>> motorcycles = ['honda','yamaha','suzuki','ducati']
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
>>> too_expensive = 'ducati'
>>> motorcycles.remove(too_expensive)
>>> print(motorcycles)
['honda', 'yamaha', 'suzuki']
>>> print(" A " + too_expensive.title() + " is too expensive for me.")

A Ducati is too expensive for me.

方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

组织索引

>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.sort()
>>> print(cars)
['audi', 'bmw', 'subaru', 'toyoda']
方法sort()永久性地修改了列表元素的排列顺序,cars按字母顺序排列

倒序排序方式:

>>> cars = ['bmw','audi','toyoda','subaru']
>>> cars.sort(reverse=True)
>>> print(cars)
['toyoda', 'subaru', 'bmw', 'audi']

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

>>> cars = ['bmw','audi','toyoda','subaru']
>>> print("Here is the original list:",cars)
Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']
>>> print("Here is the original list: ",sorted(cars))
Here is the original list:
['audi', 'bmw', 'subaru', 'toyoda']
>>> print(" Here is the original list:",cars)

Here is the original list: ['bmw', 'audi', 'toyoda', 'subaru']

倒着打印列表

>>> cars = ['bmw','audi','toyoda','subaru']
>>> print(cars)
['bmw', 'audi', 'toyoda', 'subaru']
>>> cars.reverse()
>>> print(cars)
['subaru', 'toyoda', 'audi', 'bmw']

方法reverse()永久性地修改列表元素的排列顺序

确定列表长度:

>>> cars = ['bmw','audi','toyoda','subaru']
>>> len(cars)

操作列表

 遍历整个列表:

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

>>> for magician in magicians:
... print(magician.title() + ",that was a great trick!")
...
Alice,that was a great trick!
David,that was a great trick!
Carolina,that was a great trick!

 在for循环之后执行一些操作

在for循环后面,没有缩进的代码只执行一次,而不会重复执行,Python根据缩进代码行与前一个代码的关系

#!/usr/bin/env python
# -*- encoding:utf-8 -*-
magicians = ['alice','elon','carolian']
for magician in magicians:
print(magician.title() + ",that was a great trick!")
print("I can't wait to see your next trick," + magician.title() + ". ")
print("Thank you,everyone.That was a great magic show!")

执行结果:

Alice,that was a great trick!
I can't wait to see your next trick,Alice.

Elon,that was a great trick!
I can't wait to see your next trick,Elon.

Carolian,that was a great trick!
I can't wait to see your next trick,Carolian.

Thank you,everyone.That was a great magic show!

创建数值列表:

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

使用range()创建列表

将range()作为list()的参数,输出将为一个数字列表

>>> numbers = list(range(1,5))
>>> print(numbers)
[1, 2, 3, 4]

使用函数range()时,还可以指定步长,函数range()从2开始数,然后不断加2,直到达到或超过终值11

>>> even_numbers = list(range(2,11,2))
>>> print(even_numbers)
[2, 4, 6, 8, 10]

使用函数range()几乎能够创建任何需求的数字集,例如:创建一个列表,其中包含前10个整数的平方。

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

为了代码简洁,可不使用临时变量square

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

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

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

使用列表的一部分

切片

原文地址:https://www.cnblogs.com/elontian/p/9998525.html