• python 集合 列表list


    Python 集合

    Python 编程语言中有四种集合数据类型:

    • 列表(List)是一种有序和可更改的集合。允许重复的成员。
    • 元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
    • 集合(Set)是一个无序和无索引的集合。没有重复的成员。
    • 词典(Dictionary)是一个无序,可变和有索引的集合。没有重复的成员。

    选择集合类型时,了解该类型的属性很有用。

    为特定数据集选择正确的类型可能意味着保留含义,并且可能意味着提高效率或安全性。

    集合

    列表基础用法

    #创建集合
    thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
    print(thislist)#['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    
    #访问项目
    print(thislist[0])#apple
    
    #-1表示最后一个项目 -2 倒数第二个项目
    print(thislist[-1],thislist[-2])#mango melon
    
    #索引范围索引0到2  不包括2
    print(thislist[0:2])#['apple', 'banana']
    
    #将返回索引-4(包括)到索引-1(不包括)的项目
    print(thislist[-4:-1])#['orange', 'kiwi', 'melon']
    
    #更改项目值
    thislist[1]='wahaha'
    print(thislist)#
    ['apple', 'wahaha', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    
    
    #遍历列表  可以使用for循环遍历列表项
    for x in thislist:
        print(x)
    
    
    #in  关键字用来检测子项目是否存在于列表中
    print( 'apple' in thislist)#True
    
    #len() 方法用来检测列表长度
    print(len(thislist))#7
    View Code

    列表添加 /删除  清空  彻底删除的方法

    #复制代码
    #append()向列表末尾追加方法
    arr=['hello']
    arr.append('world')
    print(arr)#['hello', 'world']
    
    #insert(index,item) 向列表中某一个位置插入项目
    arr.insert(1,'happy')
    print(arr)#['hello', 'happy', 'world']
    
    #删除项目
    #remove(item) 删除指定的项目
    arr.remove('happy')
    print(arr)#['hello', 'world']
    
    #pop(index)删除指定的索引位置的子项目 如果没指定索引,则删除最后一项
    arr.pop()
    print(arr)#['hello']
    arr.pop(0)
    print(arr)#[]
    
    
    #del list(index) 关键字删除指定的索引;
    #del list 也可以完整的删除列表
    arrList=['apple', 'wahaha', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    del arrList[0]
    print(arrList)#['wahaha', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    del arrList
    # print(arrList)#NameError: name 'arrList' is not defined
    
    #clear() 方法清空列表
    AList=['apple', 'wahaha', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    AList.clear()
    print(AList)#[]
    
    
    #copy()方法复制列表 内存地址一样
    BList=['apple', 'wahaha', 'cherry', 'orange', 'kiwi', 'melon', 'mango']
    myList=BList.copy()
    print(myList==BList)#True
    
    #list(List)方法复制列表List 内存地址不一样
    #== 比较值   is  比较引用地址
    CList=['hello','world']
    youList=list(CList)
    print(youList==CList)#True
    print(youList is CList)#False
    View Code

    合并列表的方法

    #合并两个列表 在Python中 有几种方法可以连接或串联两个或多个列表
    #最简单的方法之一 就是使用+运算符
    A=['hello']
    B=['world']
    C=A+B
    print(C)#['hello', 'world']
    
    #for循环 使用 append()方法 把B追加到A中
    for x in B:
        A.append(x)
    print(A)#['hello', 'world']
    
    #使用extend()方法将C添加到B的末尾
    B.extend(C)
    print(B)#['world', 'hello', 'world']
    
    #list()#构造函数创建列表
    D=list(('hello','world','happy'))
    print(D)#['hello', 'world', 'happy']
    View Code

  • 相关阅读:
    MySQL 日志管理
    nginx 日志分割
    Canvas 动态小球重叠效果
    Canvas制作动态进度加载水球
    js 多张爆炸效果轮播图
    js 多张图片加载 环形进度条
    INSTALL_FAILED_CONFLICTING_PROVIDER
    安卓 文件管理器 各种应用 源码
    android 静音
    android studio 查看大纲
  • 原文地址:https://www.cnblogs.com/lvlisn/p/15120827.html
Copyright © 2020-2023  润新知