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()
相关推荐
l1258656 分钟前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain
zoujiahui_20189 分钟前
别再只写 np.random.seed() 了:default_rng 与 np.random、random 到底差在哪?
python
Patrick在香港11 分钟前
Claude Agent 进阶:4 种工具调用编排模式,让 0820 那 13 个 endpoint 并行起来
python·ai·ai编程
qq_513728041 小时前
StaleElementReferenceException(陈旧元素引用异常)
python
千里码aicood1 小时前
基于Python的电商数据采集系统(大数据+爬虫)
开发语言·python
战略性的菠萝1 小时前
研究生基础-Python快速入门(终)——面向对象
python
陈皮波比茶1 小时前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
winfredzhang2 小时前
从零构建家庭照片管理系统:Django 模块化单体实战,以及我踩到的 4 个真实坑
python·postgresql·django·sqlite·bootsrap
jyOverQ2 小时前
LangGraph 流式执行详解:stream、astream 与 stream_mode 怎么选?
python·langchain
法哥20122 小时前
用 C++ 控制 CMW100,到底在干什么?
开发语言·c++