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的索引切分,a[m:n]列举出来的是从a[m]到a[n-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)-字典操作

点个关注呗 🤪😝

相关推荐
tjjucheng7 小时前
靠谱的小程序定制开发哪个好
python
num_killer7 小时前
小白的Langchain学习
java·python·学习·langchain
WangYaolove13147 小时前
基于深度学习的中文情感分析系统(源码+文档)
python·深度学习·django·毕业设计·源码
SunkingYang7 小时前
QT编译报错:使用Lambda表达式作为槽函数,报错‘xxx‘ in capture list does not name a variable
qt·list·报错·lambda表达式·槽函数·in capture list·does not name
你怎么知道我是队长7 小时前
C语言---头文件
c语言·开发语言
期待のcode8 小时前
Java虚拟机的运行模式
java·开发语言·jvm
hqwest8 小时前
码上通QT实战25--报警页面01-报警布局设计
开发语言·qt·qwidget·ui设计·qt布局控件
a程序小傲8 小时前
京东Java面试被问:动态规划的状态压缩和优化技巧
java·开发语言·mysql·算法·adb·postgresql·深度优先
HellowAmy8 小时前
我的C++规范 - 玩一个小游戏
开发语言·c++·代码规范
自学不成才8 小时前
深度复盘:一次flutter应用基于内存取证的黑盒加密破解实录并完善算法推理助手
c++·python·算法·数据挖掘