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()
相关推荐
智能体与具身智能1 小时前
TVA具身智能的概念、架构与应用(19)
人工智能·python·具身智能
小灰灰搞电子1 小时前
Rust+Slint 实现动态消息提示框源码分享
开发语言·后端·rust
2601_962294612 小时前
python中range函数怎么用
python·for循环·可迭代对象·range函数·整数列表
传奇开心果编程3 小时前
【Rust入门知识点学与练】第21课:Trait 进阶 Advanced Traits
开发语言·学习·rust
青 春 记 忆3 小时前
零基础入门python70:Docker Compose 编排完整后端
python·后端开发
新时代牛马3 小时前
字符设备驱动完整篇:从 cdev_add、file_operations 到chrdev_open 与排障
开发语言·python
swordbob3 小时前
ReentrantLock 与 AQS 完整学习手册
java·开发语言
白山编程大哥4 小时前
Java OutputStreamWriter 详解:从字符到字节的桥梁
java·开发语言·python
落羽的落羽4 小时前
【AI】快速理解AI应用的相关名词概念
linux·c++·人工智能·python·计算机网络·算法
Chasing__Dreams4 小时前
大模型应用开发--13--RAG 查询优化策略
python