page contents

来看看Python中的列表吧,超级详细哦!

Python列表基础知识

列表

1.列表简介

概念:列表是Python序列中的一种。所谓序列,指的是一块可存放多个值的连续内存空间,这些值按一定顺序排列,可通过每个值所在位置的编号(称为索引)访问它们

作用:在一个地方存储成组的信息

结构:在Python中,用方括号[ ]表示列表,并用逗号分隔其中的元素,示例如下:

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]

注意事项:

  1. 列表中的元素有特定的顺序,元素个数或顺序不同的两个列表,是不同的列表

  2. 一个列表中可以有相同的元素

  3. 同个列表中的元素可以没有任何关系

  4. 一个列表通常包含多个元素,建议给列表指定一个表示复数的名称(如letters、 names)

2.访问列表元素

  • 在访问列表元素之前,首先来了解一下索引

    索引:是指列表中元素的位置,索引从0开始,也就是第一个元素的索引是0而不是1

    负数索引:索引-1表示最后一个列表元素,索引-2表示倒数第二个列表元素,以此类推

  • 访问元素:列表名[被访问元素的索引]   比如bicycles[0]

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]

print("list1[1]: ", list1[0])
print("list2[-2]: ", list2[-2])

3,操作列表

  • 修改列表元素:列表名[被修改元素的索引] = 指定的新值

# 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles[1] = 'ducati'
print(motorcycles)
  • 添加元素:

    1.在末尾添加元素:使用方法append()在末尾附加新元素列表名.append(被添加的新元素)

    2.在列表中插入元素:使用方法insert()可在列表的任何位置添加新元素列表名.insert(新元素的索引, 被添加的新元素)

# 添加元素
# 1.使用方法append()
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles.append('ducati')
print(motorcycles)

# 使用append()方法创建列表
motorcycles = []
print(motorcycles)

motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')

print(motorcycles)

# 2.使用方法insert()可在列表的任何位置添加新元素
motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(1, 'ducati')
print(motorcycles)
  • 删除元素:

    1.使用del语句:del 列表名[被删除元素的索引]

    2.使用方法pop()(被删除元素能接着使用)

    列表名.pop():默认删除最后一个元素列表名.pop(被删除元素的索引):可删除任意元素

    3.使用remove()方法:根据元素值删除元素列表名.remove(被删除的元素)

    注:remove()方法只删除第一个指定的值,如果要删除的元素值在列表中出现多次,则需要多次调用remove()方法

  • 确定列表长度:

    1.(使用函数len()):len(列表名)

# 删除元素
# 1.使用del语句
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)

# 2.使用方法pop()
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles.pop(1)
print(motorcycles)

motorcycles.pop()
print(motorcycles)

# 3.使用remove()方法、确定列表长度
motorcycles = ['yamaha', 'yamaha', 'suzuki']
print(motorcycles)
print(len(motorcycles))

motorcycles.remove('yamaha')
print(motorcycles)

4.组织列表

  • 对列表排序

    1,使用sort()方法:对列表永久排序

    列表名.sort():默认按与字母顺序相同的顺序排序

    列表名.sort(reverse=True);向sort()方法传递参数reverse=True,将按与字母顺序相反的顺序排序

    2,使用函数sorted():对列表临时排序

    sorted(列表名):默认按与字母顺序相同的顺序排序

    sorted(列表名, reverse=True):  向函数sorted()传递参数reverse=True,将按与字母顺序相反的顺序排序

    3.使用reverse():倒着打印列表(永久性的)列表名.reverse()

# 对列表排序
# 1.使用sort()方法
cars = ['bwm', 'audi', 'toyota', 'subaru']
print(cars)

cars.sort()
print(cars)
print(cars)

cars.sort(reverse=True)
print(cars)
print(cars)

# 2.使用函数sorted()
cars = ['bwm', 'audi', 'toyota', 'subaru']
print("刚开始的列表:")
print(cars)

print("临时排序之后:")
print(sorted(cars))
print(sorted(cars, reverse=True))

print("又恢复原来的顺序:")
print(cars)

# 3.使用reverse()
cars = ['bwm', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)
print(cars)
  • 发表于 2021-04-30 16:33
  • 阅读 ( 529 )
  • 分类:Python开发

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Cara
Cara

36 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. Pack 1131 文章
  3. 小柒 1046 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章