zoukankan      html  css  js  c++  java
  • 列表


    1.创建列表

         把逗号分隔的不同数据项使用方括号括([])起来即可,列表的索引也是从0开始

         eg: list1 = ['google',12,'abc']


    2.访问列表中的值

            
    list1 = ['google','firefox','IE','edge','360']

    print(list1[1]) # firefox
    print(list1[1:len(list1)]) #['firefox', 'IE', 'edge', '360']
    for item in list1:print(item)


    3.列表运算符


    4.嵌套列表


    5.列表函数与方法

    list1 = [1,2,3,5,3,2,4,5,6,6,7,7]
    tuple1 = (1,3,'hell','world')
    str = 'hello world'

    函数

    len(list) # 列表元素个数
    max(list) # 列表元素最大值
    min(list) # 列表元素最小值
    list(seq) # 将序列转化成列表


    print(len(list1)) #12
    print(max(list1)) # 7
    print(min(list1)) # 1
    print(list(tuple1)) # [1, 3, 'hell', 'world']
    print(list(str)) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']


    方法

    list.append(obj) # 在列表末尾添加新的对象
    list.count(obj) # 统计某个元素在列表中出现的次数
    list.extend(seq) # 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表),该方法没有返回值
    list.index(obj) # 从列表中找出某个值第一个匹配项的索引位置
    list.insert(index, obj) # 将对象插入列表
    list.pop(obj=list[-1]) # 移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
    list.remove(obj) # 移除列表中某个值的第一个匹配项
    list.reverse() # 反向列表中元素
    list.sort([func]) # 对原列表进行排序
    list.clear() # 清空列表
    list.copy() # 复制列表

         eg:
    li_chorme = ['google','IE']
    li_num = [1,32,23,14,5,3]

    li_chorme.append('Firefox')
    print(li_chorme) # ['google', 'IE', 'Firefox']

    li_chorme.append('Firefox')
    print(li_chorme.count('Firefox')) # 2

    li_chorme.extend(li_num)
    print(li_chorme) # ['google', 'IE', 'Firefox', 'Firefox', 1, 32, 23, 14, 5, 3]

    print(li_chorme.index('Firefox')) # 2

    li_chorme.insert(0,'360') # ['360', 'google', 'IE']
    print(li_chorme)

    li_chorme.pop()
    print(li_chorme) # ['google']

    li_chorme.remove('google')
    print(li_chorme) # ['IE']

    li_chorme.reverse()
    print(li_chorme) # ['IE', 'google']

    li_num.sort()
    print(li_num) # [1, 3, 5, 14, 23, 32]

    li_chorme.clear()
    print(li_chorme) # []

    copy_chorme = li_chorme.copy()
    print(copy_chorme) # ['google', 'IE']



    6.删除列表元素
          
         del 语句来删除列表的元素  

    list1 = ['李世民','长孙无忌','尉迟恭']
    del list1[1]
    print(list1) # ['李世民', '尉迟恭']



    
    
  • 相关阅读:
    2018年最新整理ios APP审核被拒的常见原因
    在线一键生成安卓证书keystore文件
    iOS证书的类型功能和申请介绍
    【2018】ios app真机调试到上架App Store完整教程
    预防SQL注入
    Python模块——HashLib(摘要算法)与base64
    Python加密与解密
    PostgreSQL常用命令
    二级子目录(后台目录)设置二级域名
    积累
  • 原文地址:https://www.cnblogs.com/lovuever/p/6575638.html
Copyright © 2011-2022 走看看