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)-字典操作

点个关注呗 🤪😝

相关推荐
a7963lin33 分钟前
PHP怎么实现单例模式_PHP常用设计模式之单例模式【方法】
jvm·数据库·python
Aision_4 小时前
从工具调用到 MCP、Skill完整学习记录
java·python·gpt·学习·langchain·prompt·agi
辞旧 lekkk8 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode8 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19978 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
2301_809204709 小时前
JavaScript中严格模式use-strict对引擎解析的辅助.txt
jvm·数据库·python
zjy277779 小时前
mysql如何选择合适的索引类型_mysql索引设计实战
jvm·数据库·python
Aaswk9 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite10 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Cyber4K11 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php