python列表的循环遍历

数据容器:一个可以存储多个元素的Python数据类型

有哪些数据容器:list(列表),tuple(元组),str(字符串),set(集合),dict(字典)

可以对数据容器中的元素作增(insert)删改(index【1】="哈哈")操作

append元素:在列表尾部增加单个新元素

列表.extend(另一个数据容器):在列表尾部增加另一个列表

删除元素:1.del 2.pop 3.remove(删除列表中的第一个元素)

复制代码
del list[2]
print(list)

list=["apple","orange","banana"]
element=list.pop(2)
print(list)

列表.count(元素):统计元素个数

len(列表):统计列表元素个数

遍历:将容器内的元素依次取出并处理

使用python的时候,要特别注意缩进

定义一个列表1, 2, 3, 4, 5, 6, 7, 8, 9, 10

遍历列表,取出偶数,并存入一个新的列表对象中

复制代码
#通过append增加元素
list=[]
i=0
for i in range(1,11):
    list.append(i)
    i+=1
print(list)

list1=[]
def one():
    index=0
    while index<=len(list)-1:
        if list[index]%2==0:
            a=list[index]
            list1.append(a)
        index+=1
    print(list1)
def two():
    for i in list:
        if i%2==0:
            list1.append(i)
        i+=1
    print(list1)

one()
# two()
相关推荐
染指11102 分钟前
152.Agent-LangGraph-Command的参数
人工智能·python·langchain·agents
第404号工位22 分钟前
“已彻底删除”的照片与文件到底去哪了?我用底层扇区扫描,扒出手机和电脑的幽灵数据
linux·python·架构
智购科技自动售卖机厂家23 分钟前
双摄像头方案F1-score只有0.85,排查发现是上下视角的融合逻辑写反了~YH
python·单片机·嵌入式硬件·算法·eclipse
田里的水稻26 分钟前
FA_CNN卷积神经网络二---PyTorch
人工智能·pytorch·python·深度学习·cnn
code2cat43 分钟前
Java高级篇之Socket:从建立连接到完整收发一条消息
java·开发语言
小肝一下1 小时前
1.大模型介绍
python·langchain·大模型·ollama·langgrap
xiangjiaodalishi1 小时前
从 0 到 1 搭建 AI 友好的网站结构:信息架构与 GEO 的交叉地带
人工智能·python
凯瑟琳.奥古斯特1 小时前
OCR文字检测算法对比解析
开发语言·python·职场和发展
Misnice1 小时前
Python 内置函数大全
开发语言·windows·python
美狐美颜SDK开放平台1 小时前
直播APP集成美颜SDK,如何梳理第三方组件的数据权限?
开发语言·人工智能·深度学习·音视频·第三方美颜sdk