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()
相关推荐
勤奋的大熊猫12 小时前
Python 双层 enumerate 循环选取最后一层循环的优雅写法
python
透明的玻璃杯12 小时前
VS2015+QT 多线程
开发语言·qt
JYCJ_12 小时前
星愿池·子项清单
python
Wang's Blog12 小时前
Java 项目实战: 交友项目 - 动态查询开工:昨日回顾与今日任务
java·python·交友
2601_9679791212 小时前
0 到 1 学爬虫:用 Python 爬取网页数据指南一(附实战案例)
爬虫·python·实战案例·html解析·网页数据
码匠许师傅12 小时前
【C++三方组件】mbedTLS:面向嵌入式的轻量 TLS
开发语言·c++
云和数据.ChenGuang12 小时前
光哥说Python 异常深度解析:从异常模型到工程化实践
java·前端·人工智能·python·langchain
SelectDB12 小时前
云数仓月度账单总是超预算:Apache Doris 与 Snowflake 五类成本项拆解与对账脚本
python
迪菲赫尔曼12 小时前
【基础篇 12】把环境与源码接起来:检查 GPU,再安装服务器上的项目
运维·服务器·开发语言·人工智能·python·yolo·目标检测
2601_9628857213 小时前
2026 Python 股票数据接口选型指南(横评 + 决策树)
开发语言·python·区块链