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()
相关推荐
love530love6 小时前
OpenClaw Windows Companion 桌面客户端 连接 LM Studio 完整配置指南
人工智能·windows·python·openclaw
程序喵大人8 小时前
【C++进阶】STL容器与迭代器 - 01 STL 容器先解决元素放在哪里
开发语言·c++·stl
cui_ruicheng8 小时前
Python从入门到实战(十六):多进程编程
开发语言·python
wdfk_prog9 小时前
嵌入式面试真题第 15 题:不可恢复异常后的通用崩溃快照、调用栈保存与离线分析架构
linux·开发语言·面试·架构
_Jimmy_10 小时前
FastAPI + SQLAlchemy全局事务管理
python·fastapi
晴空了无痕10 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线
开发语言·后端·golang·kubernetes
用户83562907805110 小时前
如何使用 Python 在 Excel 中添加、编辑和删除超链接
后端·python
神明不懂浪漫10 小时前
【第五章】Java中的继承与多态
java·开发语言
神州世通11 小时前
解密企业通信安全防线:Avaya Aura 三层安全架构深度解析
开发语言·php
AC赳赳老秦11 小时前
企业工商公开信息采集分析:OpenClaw 批量查询企业工商信息,生成企业画像报告
大数据·开发语言·python·自动化·php·deepseek·openclaw