美文网首页
day08-列表、元组和字典的基本使用和内置方法

day08-列表、元组和字典的基本使用和内置方法

作者: 天行_b6d0 | 来源:发表于2020-07-15 14:56 被阅读0次

    一、列表类型

                基本使用

        1、用途:按照索引存放多个任意类型的值,索引反映的是位置/顺序

        2、定义方式:在[]内用逗号分割开多个任意类型的元素

                l=[111,1.1,"aaa",[2222,3333]]  # l=list([111,1.1,"aaa",[2222,3333]])

                print(type(l))

        数据类型转换

                res=list("hello")

                res=list({"k1":1,"k2":2,"k3":3})

                print(res)

        3、常用操作+内置的方法

            3.1 优先掌握的操作:

                1、按索引存取值(正向存取+反向存取):即可以取可以改值,不能加值

                    l1 = [11,22,33,44,55]

                    print(l1[0])

                    print(l1[10])

                    print(l1[-1])

                    print(id(l1))

                    l1[0] = 100

                    print(id(l1))

                    l1[5] = 11111  # 不能加值,会报错

                2、切片(顾头不顾尾,步长)

                    l1 = [11,22,[66,77]]

                    res=l1[2:4]    #  取第三到第四个值

                    print(res)

                    print(l1)

                    2.1 浅copy:

                        l1 = [11,22,[66,77]]

                        l2=l1[:]

                        l2=l1.copy()

                        print(id(l1[0]),id(l1[1]),id(l1[2]))

                        print(id(l2[0]),id(l2[1]),id(l2[2]))  # id相同

                        l1[0] = "aaa"

                        l1[1] = "bbb"      #  列表内不可变类型改变时,id跟着改变,但l2值不变

                        l1[2] = "ccc"

                        # l1[2][0] = 8888  #  可变类型改变时,id不变,l2中相应的值跟着改变

                        print(l2) 

                    深copy:

                        from copy import deepcopy

                        l3=deepcopy(l1)

                        print(id(l3[0]),id(l3[1]),id(l3[2]))

                        l1[2][0]=6666

                        print(l1)

                        print(l3)

                    示范1:

                        l1[0]=1000

                        print(l1)

                        print(l2)

                    示范2:

                        l1[-1][0]=666

                        print(l1)

                        print(l2)

                    了解:

                        res=l1[-1:-4:-1]

                        res=l1[::-1]

                        print(res)

                3、长度

                    l1 = [11,22,[66,77]]

                    print(len(l1))

                4、成员运算in和not in

                    l1 = [111,22,[66,77]]

                    print([66,77] in l1)

                    print(66 in l1)

                    print(66 not in l1)

                5、追加

                    l1=[11,222]

                    l1.append(33)

                    l1.append(44)

                    l1.append([55,66])

                    print(l1)

                    l1.insert(1,"aaa")

                    print(l1)

                6、删除

                    l1=[11,222,[1,2]]

                    6.1 万能删除,没有返回值,代表的是单纯的删除

                        del l1[0]

                        print(l1)

                    6.2 指定元素删除,没有返回值,代表的是单纯的删除

                            res=l1.remove([1,2])

                            print(l1)

                            print(res)

                    6.3 指定索引删除,有返回值,代表的是取走操作

                            res=l1.pop(1)

                            print(l1)

                            print(res)

                7、循环

                    l1=[11,222,[1,2]]

                    for x in l1:

                        print(x)

            需要掌握的操作:

                全部清除 clear:

                    l1=[11,22,333,333,333,]

                    l1.clear()               

                    print(l1)

                查找某个值的数目:

                    print(l1.count(333333333))

                增加值:

                    l2=[444,555]

                    # for item in l2:

                    #      l1.append(item)

                    l1.extend(l2)

                    l1.extend("hello")

                        print(l1)

                查找某个值是否在指定范围中:

                    l1=[11,22,333,333,333,]

                    res=l1.index(333,0,2)

                    res=l1.index(444)

                    print(res)

                倒序排列

                    l1=[11,22,"aaa",'bbb']

                    l1.reverse()

                    #  l1=l1[::-1]  #可以起到一样的效果

                    print(l1)

                排序sort(默认是从小到大)

                    l1 = [-3,99,12,-5,93]

                    l1.sort(reverse=True)  # 从大到小排序

                    print(l1)

        该类型总结

                存多个值;有序;可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

    二、元组类型

            什么是元组?

                元组就是一个不可变的列表

            基本使用

            1、用途: 按照索引存放多个任意类型的值,索引反应的是位置/顺序

            2、定义方式:在()内用逗号分隔开多个任意类下的值/元素

                t=(10)

                print(type(t))

            强调:当元组内只有一个元素时,必须加逗号

                t=(10,)

                print(type(t))

            类型转换

                tuple(所有可以被for循环遍历的类型)

                t=()  # 空元组

                print(t)

                print(type(t))

            3、常用操作+内置的方法

            优先掌握的操作:

                1、按索引取值(正向取+反向取):只能取

                        t=(10,1.1,"aaa",[11,22])

                        print(t[0])

                2、切片(顾头不顾尾,步长)

                        t=(11,22,33,44,55)

                        print(t[0:3])

                3、长度

                        t=(11,22,33,44,55)

                        print(len(t))

                4、成员运算in和not in

                        t=(11,22,33,44,55)

                        print(11 in t)

                5、循环

                        for x in t:

                                print(x)

            该类型总结:存多个值;有序;不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

    三、字典类型

            基本使用

                1、用途:按key:value的形式存放多个任意类型的value,key反映的是value的属性

                2、定义方式:在{}内用逗号分隔开多个key:value,其中value可以是任意类型

                          而key必须是不可变的类型,通常是字符串类型

                    d={'k1':111,'k1':222,'k1':3333}  # key重复,只保留一个

                    print(d)

            类型转换

                l=[("name","yan"),("age",18),["gender","male"]]

                res=dict(l)

                print(res)

                d=dict(x=1,y=2,z=3)

                print(d)

            空字典

                d={}

                print(type(d))

                dic={}.fromkeys(["name",'age',"gender"],None)

                dic={}.fromkeys("hello",None)

                print(dic)

            3、常用操作+内置的方法

                ==========>优先掌握的操作:

                1、按key存取值:可存可取

                        d={'k1':111}

                        print(d['k1'])

                        d['k2']=222

                        print(d)

                2、长度len

                    d={'k1':111,'k2':222}

                        print(len(d))

                3、成员运算in和not in

                    d={'k1':111,'k2':222}

                    print('k1' in d)

                    print(111 in d)

                4、删除

                    d={'k1':111,'k2':222}

                    4.1 万能删除,没有返回值,代表的是单纯的删除

                            del d['k1']

                            print(d)

                    4.2 随机删,返回一个元组

                            res=d.popitem()

                                print(d)

                                print(res)

                    4.3 指定key删除,有返回值,代表的是取走操作

                        res=d.pop('k1')

                        print(d)

                        print(res)

                5、键keys(),值values(),键值对items()

                    """

                        >>> d={'k1':111,'k2':222}

                        >>> d.keys()

                            ['k2', 'k1']

                        >>> d.values()

                                [222, 111]

                        >>> d.items()

                            [('k2', 222), ('k1', 111)]

                    """

                6、循环

                        d={'k1':111,'k2':222}

                        for k in d:

                            print(k)

                        for v in d.values():

                            print(v)

                        for k,v in d.items():

                            print(k,v)

                        keys=[]

                        for k in d:

                            keys.append(k)

                            keys=list(d.keys())

                            print(keys)

                该类型总结:存多个值;无序;可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)

    相关文章

      网友评论

          本文标题:day08-列表、元组和字典的基本使用和内置方法

          本文链接:https://www.haomeiwen.com/subject/exzacktx.html