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()
相关推荐
ValhallaCoder15 小时前
hot100-栈
数据结构·python·算法·
xuzhiqiang072418 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
MediaTea18 小时前
Python:生成器表达式详解
开发语言·python
-To be number.wan19 小时前
Python数据分析:SciPy科学计算
python·学习·数据分析
Dxy123931021619 小时前
DataFrame数据修改:从基础操作到高效实践的完整指南
python·dataframe
overmind20 小时前
oeasy Python 115 列表弹栈用pop删除指定索引
开发语言·python
Never_Satisfied21 小时前
在c#中,使用windows自带功能将文件夹打包为ZIP
开发语言·windows·c#
hnxaoli21 小时前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股
电饭叔21 小时前
文本为 “ok”、前景色为白色、背景色为红色,且点击后触发 processOK 回调函数的 tkinter 按钮
开发语言·python
雷电法拉珑1 天前
财务数据批量采集
linux·前端·python