<Python基础第2集>速通list+tuple+string+序列+set+dict容器

已有C++基础

数据容器

list 列表

python 复制代码
list() 空列表
temp=[元素,元素,元素]
list.append(元素) 在结尾追加元素
list.extend(容器) 在列表结尾追加一个容器
list.index(数据) 返回数据的下标
list.insert(index,元素) 在index位置添加元素
del list[index] 删除list1的index位置元素
list.pop(index) 取出删除list的index位置元素
list.remove(元素) 删除从前往后的第一个
list.clear() 清空列表
list.count(元素) 统计元素在列表中的数量
len(list) 统计容器有多少个元素

元组tuple(不可修改)

python 复制代码
tuple() 空元组
temp=(元素,元素,元素)
tuple.index(ele) 查找下标
tuple.count(ele) 统计元素数量
len(tuple) 返回元组数量
元组不可以修改
元组里嵌套list,list可以修改

字符串string

python 复制代码
string() 空字符串
temp="sbcka"
str.index(子串) 返回子串的起始下标
str.split()  分割字符串
str.split("it") 按照给定的字符串,对str进行分割
str.count("it") 统筹及某个字串出现的次数
len(str) 统计str中字符数量

序列

python 复制代码
list[beginIndex:endIndex:step] 默认从头开始,到尾部结束,步长为1
str[;;2]
tuple[1:3:1]

set 集合(互异性)

python 复制代码
set1=set() 空集合
set1={元素,元素,元素}
set.add(元素)  添加元素
set.remove(元素)  移除元素
set.pop()  随机弹出一个元素
set.clear() 清空集合
set1.difference(set2) 取出set1有,set2无的元素
set1.difference_update(set2) set1删除和set2相同的元素
set1.union(set2) 取出交集
len(set) 统计元素个数

dict 字典

python 复制代码
temp=dict()
dict={"scsc":232,"syhj":7889,"23":234}
dict={
      "scss3":{"scscww":224,"afer":890},
      "54g":{"43t":89,"234":789}
}
dict.keys() 取出所有key
dict.values() 取出所有value
dict[键] 取出键对应的值
dict.clear() 清空dict
dict.items() 取出dict,得到的是元组
相关推荐
孟健1 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞3 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽5 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程10 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪10 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook10 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python