python-基础(4)-list

python专栏地址
上一篇:python-基础(3)-字符串操作
下一篇:python-基础(5)-字典操作

List结构

本节将学习以下内容

  • list初识
  • list的操作

一、List初识

1、创建

  • 直接创建
python 复制代码
# 例1
gufen = []
gufen
复制代码
[]
python 复制代码
# 例2
gufen = [1, 2, "gufen"]
gufen
复制代码
[1, 2, 'gufen']
  • 通过list(\[\])创建

即给list()函数中传入一个\[\]

python 复制代码
# 例2
gufen = list()
type(gufen)
复制代码
list
python 复制代码
gufen = list([1, 2, 3, 4])
type(gufen)
复制代码
list
python 复制代码
gufen
复制代码
[1, 2, 3, 4]
python 复制代码
gufen[3:100]
复制代码
[4]

二、List的操作

1、求长度

  • 函数len()
python 复制代码
gufen = list([1, 2, 3, 4])
len(gufen)
复制代码
4

2、+ 和 *

  • +:在一个列表的后面续上另外一个list
python 复制代码
a = [123, 456]
b = ["gufeng", "de", "world"]
c = a + b
c
复制代码
[123, 456, 'gufeng', 'de', 'world']
python 复制代码
type(c)
复制代码
list
  • *:这里说的乘是乘以常数n,也就是把原来的list重复n遍
python 复制代码
a * 3
复制代码
[123, 456, 123, 456, 123, 456]

3、元素引用与切分

python的索引切分,am:n列举出来的是从am到an-1的数

  • 引用
python 复制代码
a = [1, 2, 3, 4, 5, 6, 7, 8]
python 复制代码
a[1]
复制代码
2
python 复制代码
a[-1]  # -1表示倒数第一个
复制代码
8
python 复制代码
a[-2]
复制代码
7
  • 切分
python 复制代码
a[1:3]  # 表示a[1],a[2]
复制代码
[2, 3]
python 复制代码
a[0:]  # 后面的全部元素
复制代码
[1, 2, 3, 4, 5, 6, 7, 8]
python 复制代码
a[0:1]
复制代码
[1]
python 复制代码
a[:-1]
复制代码
[1, 2, 3, 4, 5, 6, 7]

4、替换

  • 这里的替换是赋值性的替换
python 复制代码
a = [1, 2, 3, 4, 5, 6, 7, 8]
python 复制代码
a[0] = 789
a
复制代码
[789, 2, 3, 4, 5, 6, 7, 8]
python 复制代码
a[:] = ["gufen", "world"]
a
复制代码
['gufen', 'world']

5、删除部分元素

  • 操作语句: del a\[\]
python 复制代码
a = [1, 2, 3, 4, 5, 6, 7, 8]
python 复制代码
del a[0]  # 定点删除
a
复制代码
[2, 3, 4, 5, 6, 7, 8]
python 复制代码
del a[3:]  # 范围性删除,3及后面的全部
python 复制代码
a
复制代码
[2, 3, 4]

6、指定元素在list中吗

  • 操作语句 A in B 。A在B中吗
python 复制代码
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
8 in a
复制代码
True
python 复制代码
8 not in a
复制代码
False
python 复制代码
gufen = "gufen de world"
"gufen" in gufen
复制代码
True
python 复制代码
type = [30001, 30002, 30003]
for item in type:
    type = str(item)
    if type not in ["30001", "30002"]:
        type = "other"
    print(type)
复制代码
30001
30002
other
python 复制代码
x = 1
y = 3
z = 4
z1 = [x, y, z]
sum(z1)
复制代码
8

7、list嵌套

  • 这里要注意的是,list的嵌套结构,其引用方式,就像其他语言的多维数组一样
  • 已经几次提到数组了,这里单独解释以下什么是数组。数组其实就是线性代数中的向量。没有嵌套的list就相当于矩阵中的列向量,嵌套的list相当与一个向量组。下面是一个4*5的数组,就是4行5列的。

\begin{matrix} a & b & c & d & e\ f & g & h & i & j \ k & l & m & n & o \ p & q & r & s & t \end{matrix}

python 复制代码
a = [1, 2, [3, 4]]
python 复制代码
a
复制代码
[1, 2, [3, 4]]
python 复制代码
a[2]
复制代码
[3, 4]
python 复制代码
a[2][0]
复制代码
3
python 复制代码
a[2][1]
复制代码
4

8、计数与定位

  • count()函数,统计出现的次数
  • index(),获得索引值
python 复制代码
tang = ["apple", "banana", "apple", "apple", "banana"]
tang.count("banana")
复制代码
2
python 复制代码
tang = ["apple", "1", "2", "3", "4", "5"]
tang.index("apple")
复制代码
0
python 复制代码
# 没有的元素找不到
tang.index("banana")
复制代码
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

Cell In[39], line 2
      1 # 没有的元素找不到
----> 2 tang.index("banana")


ValueError: 'banana' is not in list

9、list在数据结构中的基本操作

  • 添加函数 append() 不限制添加元素的树结构

  • 指定位置插入元素 insert(n,list) ,

  • 删除指定元素remove(list)

  • 弹出指定元素pop()

  • list排序sort()

  • 反转reverse()

  • 添加一个普通元素

python 复制代码
gufen = []
gufen.append("gufen")
gufen
复制代码
['gufen']
  • 添加了一个列表
python 复制代码
gufen.append(["gufen", "world"])
gufen
复制代码
['gufen', ['gufen', 'world'], ['gufen', 'world']]
  • 插入
python 复制代码
gufen.insert(2, "gufen")  # 指定位置插入
gufen
复制代码
['gufen', ['gufen', 'world'], 'gufen', ['gufen', 'world']]
python 复制代码
gufen.insert(0, "43434")
gufen
复制代码
['43434', 'gufen', ['gufen', 'world'], 'gufen', ['gufen', 'world']]
  • 删除
    remove
python 复制代码
# 默认去掉这个元素的第一个
gufen.remove(['gufen', 'world'])
gufen
复制代码
['43434', 'gufen', 'gufen', ['gufen', 'world']]
  • 弹出
python 复制代码
gufen.pop()  # 默认弹出的是list的最后一个
复制代码
['gufen', 'world']
python 复制代码
# 弹出后会改变原来的list。
gufen.pop(1) 
复制代码
'gufen'
python 复制代码
gufen
复制代码
['43434', 'gufen']
  • 排序
    • 自己变还是没变,没变的一种是直接调用sort(),不变的是sorted(list)
python 复制代码
gufen = [1, 3, 5, 6, 2, 57, 32]
gufen.sort()
gufen
复制代码
[1, 2, 3, 5, 6, 32, 57]
python 复制代码
gufen = [1, 3, 5, 6, 2, 57, 32]
gufen2 = sorted(gufen)
gufen2
复制代码
[1, 2, 3, 5, 6, 32, 57]
  • reverse取反函数
python 复制代码
gufen2.reverse()
gufen2
复制代码
[57, 32, 6, 5, 3, 2, 1]

python专栏地址
上一篇:python-基础(3)-字符串操作
下一篇:python-基础(5)-字典操作

点个关注呗 🤪😝

相关推荐
顾林海4 小时前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱6 小时前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django
曲幽11 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码11 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱21 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵1 天前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio1 天前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户0332126663671 天前
使用 Python 从零创建 Word 文档
python
Csvn1 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽1 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate