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()
相关推荐
AC赳赳老秦13 小时前
数据安全合规:OpenClaw 敏感信息脱敏、操作日志审计、权限精细化管控方案,符合等保要求
网络·数据库·python·安全·web安全·oracle·openclaw
AIFQuant13 小时前
贵金属 API 避坑:黄金/白银行情接口常见陷阱(数据漂移、断点、延迟)
开发语言·python·websocket·金融·restful·贵金属
财经资讯数据_灵砚智能13 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年5月9日
人工智能·python·信息可视化·自然语言处理·ai编程
shehuiyuelaiyuehao13 小时前
算法21,搜索插入位置
python·算法·leetcode
加号313 小时前
【C#】 HTTP 请求通讯实现指南
开发语言·http·c#
Volunteer Technology13 小时前
携程智能体项目
人工智能·python·numpy
平安的平安13 小时前
Python实现RAG检索增强生成:让大模型拥有你的私有知识库
开发语言·python
昵称小白13 小时前
栈与单调栈专题
开发语言·算法
code bean13 小时前
【LangChain】少样本提示(Few-Shot Prompting)实战指南
开发语言·python·langchain
心.c13 小时前
RAG文档解析 - pypdf、LlamaParse、DeepDoc、SimpleDirectoryReader到底怎么选?
python·算法·ai