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. python列表

    2023-12-14 20:46:04       21 阅读
  2. Python-07-循环、嵌套循环

    2023-12-14 20:46:04       9 阅读
  3. js循环

    2023-12-14 20:46:04       31 阅读
  4. 对dict类型python

    2023-12-14 20:46:04       31 阅读
  5. Python map

    2023-12-14 20:46:04       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-14 20:46:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-14 20:46:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-14 20:46:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-14 20:46:04       20 阅读

热门阅读

  1. 【tcmalloc】(二)整体设计和thread cache(申请)

    2023-12-14 20:46:04       42 阅读
  2. 文件版本的通讯录

    2023-12-14 20:46:04       35 阅读
  3. SQLAlchemy 第三篇

    2023-12-14 20:46:04       27 阅读
  4. 33.Spring有哪几种配置方式

    2023-12-14 20:46:04       39 阅读
  5. linux中的网络知识

    2023-12-14 20:46:04       36 阅读
  6. IT应用运维最常用指标体系

    2023-12-14 20:46:04       40 阅读