第3章 列表简介

1 列表概述

  • 列表是一系列按特定顺序的元素组成,其中的元素之间可以没有任何关系
  • 在Python中,用方括号([ ])来表示列表,并用逗号分隔其中的元素
a = [1, 2, 3]
b = [1, 'abc', 2.0, ['a', 'b', 'c']]

print(a)
print(b)
print(a[0], a[1], a[2], sep='-')

c = b[1:3]

print(c)
print(type(c))

s = 'abcdef'

print(s[1:3], s[-1])

print(b[-1])

2 访问列表元素

  • 因为列表是有序元素的集合,所以我们要向访问列表元素,只需要将元素的位置或索引告诉Python即可。

要访问列表元素,可支出列表的名称,在指出元素的索引,并将索引放在方括号内。

  • 值得注意的是,在Python中,第一个列表元素的索引为0。因此要访问列表的任何元素,都可以将其位置减去1,得到结果才是该元素正确的索引。
# 获取列表的一些基本信息
list1 = [9, 1, -4, 3, 7, 11, 3]
# 打印列表的长度
print("list1的长度 = ", len(list1))
# 打印列表中的最大值
print("list1的最大值 = ", max(list1))
# 打印列表中的最小值
print("list1的最小值 = ", min(list1))
# 打印列表中的3这个元素一共出现了几次
print("list1的3这个元素出现了{}次".format(list1.count(3)))
# 列表的改变
list2 = ['a', 'c', 'd']
# 给list2结尾添加一个新元素‘e’
list2.append('e')
print(list2)
# 在list2的‘a’和‘c’之间插入一个‘b’
list2.insert(1, 'b')
print(list2)
# 删除list2中的‘b’
list2.remove('b')
print(list2)
# 列表翻转
list3 = [1, 2, 3]
list3.reverse()
print(list3)
# 列表排序
list1.sort()
print(list1)
list1.sort(reverse=True)
print(list1)

2.1 负数索引

  • Python为访问最后一个列表元素应该了一个特殊语法。通过将索引制定为-1,可让Python返回最后一个列表元素。
  • 这个语法很有用,因为我们需要在不知道列表长度的情况下访问最后一个元素。同样的,这个语法也适用于其他的负数索引,以此类推。
  • 仅当列表为空时,这种访问最后一个元素的方式才会导致错误。
  • 即使列表的长度发生了变化,我们仍然可以用负数索引访问最后一个列表元素。

3 修改列表元素

要修改列表元素,可指定列表名和要修改的索引,再指定该元素的新值

4 增加列表元素

4.1 方法append()

append()将新元素附加到列表末尾

我们可以先创建一个空列表,再在其中利用append()添加新元素。这种创建列表的方式机器常见,因为经常等程序运行后,我们才知道用户要在程序中存储哪些数据。

4.2 方法insert()

insert()可在列表的任何位置添加新元素,但是需要指定新元素的索引和值

在执行insert()方法后,这种操作将列表中既有的每个元素都移一个位置。

5 删除列表元素

5.1 语句del

倘若我们知道要删除的元素在列表中的位置,我们可以使用del语句。

  • 如果我们知道要删除的列表元素的索引,那么我们就可以使用del语句可删除任何位置的列表元素
  • 使用del语句删除列表元素后,我们就无法再访问该元素。

5.2 方法pop()

  • 倘如我们将元素从列表中删除后,并接着使用该元素的值,便可以使用pop()

弹出(pop):列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。

  • 我们仍然可以使用pop()删除列表中任何位置的元素,只需要在括号中只当要删除元素的索引

5.3 使用del语句和pop()方法的判断标准

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

5.4 方法remove()

当我们不知道从列表中删除的值所处的位置,但是我们知道要删除元素的,我们就可以使用方法remove()

  • remove()从列表中删除元素,可接着使用它的值。
  • remove()只删除第一个只能指定的值,如果要删除的值在列表中多次出现,那么就要使用循环判断。

6 组织列表

我们经常需要以特定的顺序呈现信息。

6.1 方法sort()

  • sort()对列表元素进行永久性排序。
  • 按照与列表元素顺序相反的顺序排列列表元素。我们只需要向sort()方法传递参数reverse=True

6.2 方法sorted()

sorted()对列表元素进行临时排序。(不影响列表元素在列表中原始排列顺序。)

  • 要保留列表元素原来的排列顺序,同时以特定的顺序呈现列表元素。
  • 按照与列表元素顺序相反的顺序排列列表元素。我们只需向sorted()方法传递参数reverse=True

6.3 方法reverse()

reverse()反转列表元素的排列顺序

  • reverse()只是反转列表元素的排列顺序。
  • reverse()永久性修改列表元素的排列顺序,但是可以随时恢复到原来的排列顺序,我们只需要对列表再次调用reverse()方法即可。

6.4 方法len()

len()可获得列表的长度

7 索引错误

Python无法理解程序员指定的索引。

  • 解决方式:程序发生索引错误时,尝试将我们指定的索引减去1,然后再次运行。

动手试一试

3-1 姓名:

names = ["cat", "fish", "dog", "bird", "monkey"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
print(names[4])

问候语:

names = ["cat", "fish", "dog", "bird", "monkey"]
message1 = "Hello, " + names[0].title() + "."
message2 = "Hello, " + names[1].title() + "."
message3 = "Hello, " + names[2].title() + "."
message4 = "Hello, " + names[3].title() + "."
message5 = "Hello, " + names[4].title() + "."
print(message1)
print(message2)
print(message3)
print(message4)
print(message5)

3-3 自己的列表

commutingTools = ["train", "bus", "taxi", "boat"]
message1 = "I would like to own a " + commutingTools[0]
message2 = "I would like to own a " + commutingTools[1]
message3 = "I would like to own a " + commutingTools[2]
message4 = "I would like to own a " + commutingTools[3]
print(message1)
print(message2)
print(message3)
print(message4)

3-4 嘉宾名单:

people = ['dog', 'cat', 'fish']
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
print(people[2].title() + ", I would like to invite you to have dinner.")

3-5 修改嘉宾名单:

people = ['dog', 'cat', 'fish']
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
people_who_can_not_come = people[2]
people[2] = "bird"
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people_who_can_not_come.title() + " can't make it.")

3-6 添加嘉宾:

people = ['dog', 'cat', 'fish']
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
people_who_can_not_come = people[2]
people[2] = "bird"
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people_who_can_not_come.title() + " can't make it.")
print("I found a bigger table.")
people.insert(0, 'tiger')
people.insert(2, 'monkey')
people.append('cow')
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people[3].title() + ", I would like to invite you to have dinner.")
print(people[4].title() + ", I would like to invite you to have dinner.")
print(people[5].title() + ", I would like to invite you to have dinner.")

3-7 缩减名单:

people = ['dog', 'cat', 'fish']
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
people_who_can_not_come = people[2]
people[2] = "bird"
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people_who_can_not_come.title() + " can't make it.")
print("I found a bigger table.")
people.insert(0, 'tiger')
people.insert(2, 'monkey')
people.append('cow')
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people[3].title() + ", I would like to invite you to have dinner.")
print(people[4].title() + ", I would like to invite you to have dinner.")
print(people[5].title() + ", I would like to invite you to have dinner.")
print("I've just been told that your new table won't arrive in time, so I can only invite two guests")
people1 = people.pop()
print(people1.title() + ", I am sorry.")
people2 = people.pop()
print(people2.title() + ", I am sorry.")
people3 = people.pop()
print(people3.title() + ", I am sorry.")
people4 = people.pop()
print(people4.title() + ", I am sorry.")
print(people[0].title() + ", you're still on the list.")
print(people[2].title() + ", you're still on the list.")
del people[0]
del people[1]
print(people)

3-8 放眼世界:

tourist_attractions = ['The potala palace', 'Cape of Good Hope', 'Prague', 'The Great Wall', 'Hongkong']
print(tourist_attractions)
print(sorted(tourist_attractions))
print(tourist_attractions)
print(sorted(tourist_attractions, reverse=True))
print(tourist_attractions)
tourist_attractions.reverse()
print(tourist_attractions)
tourist_attractions.reverse()
print(tourist_attractions)
tourist_attractions.sort()
print(tourist_attractions)
tourist_attractions.sort(reverse=True)
print(tourist_attractions)

3-9 晚餐嘉宾:

people = ['dog', 'cat', 'fish']
print("I invited " + str(len(people)) + " people.")
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
people_who_can_not_come = people[2]
people[2] = "bird"
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people_who_can_not_come.title() + " can't make it.")
print("I found a bigger table.")
people.insert(0, 'tiger')
people.insert(2, 'monkey')
people.append('cow')
print(people[0].title() + ", I would like to invite you to have dinner.")
print(people[1].title() + ", I would like to invite you to have dinner.")
print(people[2].title() + ", I would like to invite you to have dinner.")
print(people[3].title() + ", I would like to invite you to have dinner.")
print(people[4].title() + ", I would like to invite you to have dinner.")
print(people[5].title() + ", I would like to invite you to have dinner.")
print("I've just been told that your new table won't arrive in time, so I can only invite two guests")
people1 = people.pop()
print(people1.title() + ", I am sorry.")
people2 = people.pop()
print(people2.title() + ", I am sorry.")
people3 = people.pop()
print(people3.title() + ", I am sorry.")
people4 = people.pop()
print(people4.title() + ", I am sorry.")
print(people[0].title() + ", you're still on the list.")
print(people[1].title() + ", you're still on the list.")
del people[1]
del people[0]
print(people)

尝试使用各个函数:

items = []
items.append('dog')
items.insert(1, 'bird')
items.append('cat')
del items[0]
items.reverse()
print(items)
print(sorted(items))
print(sorted(items, reverse=True))
items.sort()
print(items)
items.sort(reverse=True)
print(items)
print("There are " + str(len(items)) + " items in the list.")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值