python之列表

列表介绍:

列表 由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有 任何关系。鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如letters 、digits 或names )是个不错的主意。

在Python中,用方括号([] )来表示列表,并用逗号来分隔其中的元素。下面是一个简单的列表示例,这个列表包含几个学生姓名:

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)

如果你让Python将列表打印出来,Python将打印列表的内部表示,包括方括号:
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
[Finished in 1.0s]

 第一部分:访问列表元素

列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
print(students[0])
print(students[0].title())

这里演示了访问列表元素的语法。当你请求获取列表元素时,Python只返回该元素,而不包括方括号和引号:
['zhangsan', 'lisi', 'wangwu', 'zhaoliu'] zhangsan Zhangsan [Finished in 0.4s]

索引从0而不是1开始

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
print(students[1])
print(students[3])
print(students[-1])

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
lisi
zhaoliu
zhaoliu
[Finished in 0.3s]

使用列表中的各个值

students = ['zhangsan','lisi','wangwu','zhaoliu']
message = " My first student was a " + students[1].title() + "."
print(message)

可使用方法title() 让元素'lisi' 的格式更整洁:
 My first student was a Lisi.
[Finished in 0.3s]

第二部分:修改、添加和删除元素

修改列表元素的语法与访问列表元素的语法类似。要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。

修改:

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students[0] = 'xiaoming'
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['xiaoming', 'lisi', 'wangwu', 'zhaoliu']
[Finished in 0.2s]

添加:(末尾添加)

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students.append('xiaoming')
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'xiaoming']
[Finished in 0.1s]

添加:(动态创建列表)

方法append() 让动态地创建列表易如反掌,例如,你可以先创建一个空列表,再使用一系列的append() 语句添加元素。下面来创建一个空列表,再在其中添加元 素:

students = []
students.append('zhangsan')
students.append('lisi')
students.append('wangwu')
students.append('zhaoliu')
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
[Finished in 0.1s]

添加:(在列表中插入元素)

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students.insert(0,'xiaoming')
print(students)
students.insert(2,'xiaoming')
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['xiaoming', 'zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['xiaoming', 'zhangsan', 'xiaoming', 'lisi', 'wangwu', 'zhaoliu']
[Finished in 0.1s]

从列表中删除元素:

注意:使用del 语句将值从列表中删除后,你就无法再访问它了。

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
del students[0]
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['lisi', 'wangwu', 'zhaoliu']
[Finished in 0.2s]  

使用方法pop() 删除元素

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students_del = students.pop()
print(students)
print(students_del)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['zhangsan', 'lisi', 'wangwu']
zhaoliu
[Finished in 0.1s]

假设列表中的学生是按时间存储的,就可使用方法pop() 打印一条消息,指出最后的是哪个学生:

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students_del = students.pop()
print("The last students i owned was a " + students_del.title() + ".")

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
The last students i owned was a Zhaoliu.
[Finished in 0.1s]

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

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students_del = students.pop(2)
print("The last students i owned was a " + students_del.title() + ".")

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
The last students i owned was a Wangwu.
[Finished in 0.1s]

如果你不确定该使用del 语句还是pop() 方法,下面是一个简单的判断标准:如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del 语句;如果你要在删除元 素后还能继续使用它,就使用方法pop()

根据值删除元素

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students.remove('zhaoliu')
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['zhangsan', 'lisi', 'wangwu']
[Finished in 0.1s]

使用remove() 从列表中删除元素时,也可接着使用它的值。下面删除值'zhaoliu' ,并打印一条消息,指出要将其从列表中删除的原因:

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)

too_expensive = 'zhaoliu'
students.remove('zhaoliu')
print(students)
print("\nA " + too_expensive.title() + "is too expensive for me.")

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['zhangsan', 'lisi', 'wangwu']

A Zhaoliuis too expensive for me.
[Finished in 0.1s]

 第三部分:组织列表

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)
students.sort()
print(students)

可以按字母排序排列,是永久的排序。 ['zhangsan', 'lisi', 'wangwu', 'zhaoliu'] ['lisi', 'wangwu', 'zhangsan', 'zhaoliu'] [Finished in 0.1s]

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

students = ['zhangsan','lisi','wangwu','zhaoliu']
print("here is the student list")
print(students)

print("\nHere is the student list:")
print(sorted(students))

print("\nhere is the studennt list again:")
print(students)

here is the student list
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']

Here is the student list:
['lisi', 'wangwu', 'zhangsan', 'zhaoliu']

here is the studennt list again:
['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
[Finished in 0.1s]

倒着打印列表

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students)

students.reverse()
print(students)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']
['zhaoliu', 'wangwu', 'lisi', 'zhangsan']
[Finished in 0.1s]

  使用列表时避免索引错误

students = ['zhangsan','lisi','wangwu','zhaoliu']
print(students[4])

Traceback (most recent call last):
  File "D:\Sublime Text3\Sublime Text3\test.py", line 2, in <module>
    print(students[4])
IndexError: list index out of range
[Finished in 0.1s]

仅当列表为空时,这种访问最后一个元素的方式才会导致错误

students = []
print(students[-1])

Traceback (most recent call last):
  File "D:\Sublime Text3\Sublime Text3\test.py", line 2, in <module>
    print(students[-1])
IndexError: list index out of range
[Finished in 0.1s]

  

 

posted @ 2018-05-15 11:40  大风歌兮  阅读(199)  评论(0编辑  收藏  举报