练习内容包括
创建并访问列表
列表的索引
使用列表中的各个值
修改列表中的元素
在列表中添加元素 append()
在列表中插入元素 insert()
在列表中删除元素 del,pop()
根据值删除列表中的元素 remove()
对列表中的元素进行排列
1)永久性修改 sort(),按字母表正向排列
2)永久性修改 sort(reverse=True),按字母表反向排列
3)临时修改 sorted(),按字母表正向排列
对列表中的元素进行反转打印 reverse()
计算列表长度 len()
Python中的列表,什么是列表?
列表由一系列按特定顺序排列的元素组成,通常列表包含多个元素。
创建并访问元素列表
-------------------------------------------------------------
bicycles = [ ‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘ ]
print(bicycles)
print(bicycles[0])
print(bicycles[0].title())
-------------------------------------------------------------
[‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘]
trek
Trek
-------------------------------------------------------------
#1 打印 bicycles 列表,显示全部列表
#2 bicycles[0] 这个 0 表示打印 bicycles 列表中索引位置为 0 的元素
#3 用上前面学过的内容,打印并且首字母大写
索引
索引是从 0 开始,而不是 1。不光是 Python,大多数编程语言都是如此。
访问列表中的第一个和第四个元素,可以使用索引 1和3。
-------------------------------------------------------------
bicycles = [ ‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘ ]
print(bicycles[1])
print(bicycles[3])
-------------------------------------------------------------
cannondale
specialized
指定 –1 为访问列表中的最后一个元素,同理 –2 代表访问倒数第二个元素
------------------------------------------------------------
bicycles = [ ‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘ ]
print(bicycles[-1])
print(bicycles[-3])
------------------------------------------------------------
specialized
cannondale
使用列表中的各个值
------------------------------------------------------------
bicycles = [ ‘trek‘, ‘cannondale‘, ‘redline‘, ‘specialized‘ ]
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
-------------------------------------------------------------
My first bicycle was a Trek.
修改列表元素
直接修改列表中的第一个元素。
---------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘]
print(motorcycles)
motorcycles[0] = ‘ducati‘
print(motorcycles)
---------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
[‘ducati‘, ‘yamaha‘, ‘suzuki‘]
在列表中添加元素
--------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘]
print(motorcycles)
motorcycles.append(‘ducati‘)
print(motorcycles)
--------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
[‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘]
.append() 可以在列表的添加元素,不影响列表本身的顺序
定义一个空列表,依次添加元素,并打印该列表
----------------------------
motorcycles = []
motorcycles.append(‘honda‘)
motorcycles.append(‘yamaha‘)
motorcycles.append(‘suzuki‘)
print(motorcycles)
-----------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
在列表中插入元素
在 motorcycles 列表中插入 ducati,插入位置的索引值为 0
---------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘ ]
motorcycles.insert(0, ‘ducati‘)
print(motorcycles)
---------------------------------------------
[‘ducati‘, ‘honda‘, ‘yamaha‘, ‘suzuki‘]
从列表中删除元素(del)
指定列表中索引值为 0 的元素
指定索引值可以删除,但是指定列表中的元素就无法删除
使用 del 语句可以删除任何位置的列表元素,条件是知道其索引,删除后将无法访问
---------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘ ]
print(motorcycles)
del motorcycles[0]
print(motorcycles)
----------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
[‘yamaha‘, ‘suzuki‘]
从列表中删除元素(pop())
pop()默认删除列表中的最后一个元素,和 del 不同的是被删除的元素还可以继续使用。
---------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘ ]
print(motorcycles)
popped_motorcycle = motorcycles.pop()
#定义被删除的元素
print(motorcycles)
#打印已经被删除后的列表
print(popped_motorcycle)
#打印被删除的元素
---------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
[‘honda‘, ‘yamaha‘]
suzuki
实例:打印最后购买的摩托车
---------------------------------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘ ]
last_owned = motorcycles.pop()
print("The last motorcycle I owned was a " + last_owned.title() + ".")
----------------------------------------------------------------------
The last motorcycle I owned was a Suzuki.
pop()函数也可以指定索引值进行打印
-----------------------------------------------------------------------
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.
根据值删除元素 remove()
从列表 motorcycles 中删除值 ’ducati’
-------------------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘ ]
print(motorcycles)
motorcycles.remove(‘ducati‘)
print(motorcycles)
--------------------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘]
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
被 remove() 删除后的元素还是可以被调用的
------------------------------------------------------------------
motorcycles = [ ‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘ ]
print(motorcycles)
too_expensive = ‘ducati‘
# 先将要删除的元素定义一个变量
motorcycles.remove(too_expensive)
# remove()删除
print(motorcycles)
print("\nA " + too_expensive.title() + " is too expensive for me.")
-------------------------------------------------------------------
[‘honda‘, ‘yamaha‘, ‘suzuki‘, ‘ducati‘]
[‘honda‘, ‘yamaha‘, ‘suzuki‘]
A Ducati is too expensive for me.
使用 sort() 对列表进行永久性排序
正向排序 sort()
反向排序 sort(reverse=True)
sort()函数会对列表进行永久性排序,排序规则为字母表顺序
--------------------------------------------
cars = [ ‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘ ]
cars.sort()
print(cars)
--------------------------------------------
[‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]
还可以在 sort() 函数中添加 参数,进行与字母表相反的排列顺序,同样也是永久的
--------------------------------------------
cars = [ ‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘ ]
cars.sort(reverse=True)
print(cars)
--------------------------------------------
[‘toyota‘, ‘subaru‘, ‘bmw‘, ‘audi‘]
使用 sorted() 对列表进行临时排序
--------------------------------------------
cars = [ ‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘ ]
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list agein:")
print(cars)
--------------------------------------------
Here is the original list:
[‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
Here is the sorted list:
[‘audi‘, ‘bmw‘, ‘subaru‘, ‘toyota‘]
Here is the original list agein:
[‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
反转打印列表(永久性修改)
是按照列表中元素顺序的相反顺序打印,而不是字母的相反顺序打印
--------------------------------------------
cars = [ ‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘ ]
print(cars)
cars.reverse()
print(cars)
--------------------------------------------
[‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘]
[‘subaru‘, ‘toyota‘, ‘audi‘, ‘bmw‘]
len()确定列表的长度
这个长度指的是列表中包含有几个元素
--------------------------------------------
cars = [ ‘bmw‘, ‘audi‘, ‘toyota‘, ‘subaru‘ ]
print(len(cars))
--------------------------------------------
4
本文出自 “LULU” 博客,请务必保留此出处http://aby028.blog.51cto.com/5371905/1964662
原文地址:http://aby028.blog.51cto.com/5371905/1964662