• Python的容器类型的遍历汇总


    # 遍历列表
    # l = ["a","b","1",2]
    # print(l)
    # for i in l:
    #     print(i)
    #     print(type(i))
    
    """
    ['a', 'b', '1', 2]
    a
    <class 'str'>
    b
    <class 'str'>
    1
    <class 'str'>
    2
    <class 'int'>
    """
    # 遍历字符串
    # s = "abcde"
    # print(s)
    # for si in s:
    #     print(si)
    #     print(type(si))
    """
    abcde
    a
    <class 'str'>
    b
    <class 'str'>
    c
    <class 'str'>
    d
    <class 'str'>
    e
    <class 'str'>
    """
    # 遍历字典
    # dic = {"name":"lsj","age":18}
    # print(dic,type(dic))
    # for d in dic:
    #     print(d,dic[d])
    #     print(type(d))
    """
    {'name': 'lsj', 'age': 18} <class 'dict'>
    name lsj
    <class 'str'>
    age 18
    <class 'str'>
    """
    
    # 遍历集合
    # se = {"a","1","ab",1,2,4}
    # print(set(se),type(se))
    # for se1 in se:
    #     print(se1)
    #     print(type(se1))
    """
    {'a', 'ab', 2, 1, 4, '1'} <class 'set'>
    a
    <class 'str'>
    ab
    <class 'str'>
    2
    <class 'int'>
    1
    <class 'int'>
    4
    <class 'int'>
    1
    <class 'str'>
    """
    
    # 遍历元组
    # t = ("a","1","ab",1,2,4,)
    # print(t,type(t))
    # for t1 in t:
    #     print(t1)
    #     print(type(t1))
    """
    ('a', '1', 'ab', 1, 2, 4) <class 'tuple'>
    a
    <class 'str'>
    1
    <class 'str'>
    ab
    <class 'str'>
    1
    <class 'int'>
    2
    <class 'int'>
    4
    <class 'int'>
    """
  • 相关阅读:
    bat 批处理编写
    dos 命令
    反射
    反爬机制和破解方法汇总
    pandas
    谷歌历史浏览器下载
    python-----pip安装源选择(亲测有效)
    deepin 20.1 系统未安装pip
    python自带库-----os.path
    python 自带库---os库
  • 原文地址:https://www.cnblogs.com/liunaixu/p/12493277.html
Copyright © 2020-2023  润新知