字典和集合

it2024-07-18  37

字典和集合

一、字典

        1、特点:字典不支持下标

    l  符号为大括号

    l  数据为键值对形式出现

    l  个键值对之间用逗号隔开

        2、语法:

     有数据字典:dic1  = {'name':'tom','age':'10','gender':'男'}

     无数据字典:dic2 = {};dic3 = dict()

        3、字典的常见操作:字典是可变数据类型

                1)、增,改:

                        增:字典序列[key] = 值;如果key存在则修改key对应的值,否则新增键值对

                2)、删:

                        del(字典序列):删除字典

                                # del(dic1)

                                # print(dic1)#报错

                        # del 字典序列[key]:删除指定键值对,如果不存在ley报错

                                del dic1['name']

                                print(dic1)#{'age': '10', 'gender': '男', 'id': 110}

                        # 字典序列.clear():清空字典

                                dic1.clear()

                                print(dic1)#{}

                3)、查

                        dic1  = {'name':'tom','age':'10','gender':'男'}

          l  指定key查找

                                print(dic1['name'])#hgs

          l  函数查找

                        # 字典序列.get(key):如果当前查找的key存在则返回  值,如果没有默认值则返回None

                                dicxx  = {'name':'tom','age':'10','gender':'男'}

                                print(dicxx.get('name'))#tom

                                print(dicxx.get('names'))#None

                        # 字典序列.keys():查找字典中的键

                                print(dicxx.keys())#dict_keys(['name', 'age', 'gender'])

                        # 字典序列.values():查找字典中的键值

                                print(dicxx.values())#dict_values(['tom', '10', '男'])

                        # 字典序列.items():查找字典中的元素

                                print(dicxx.items())#dict_items([('name', 'tom'), ('age', '10'), ('gender', '男')])

        4、字典中的循环遍历

                dict1 = {'name':'tom','age':20,'gender':'男'}

                1)、# 遍历字典的key

                        for key in dict1.keys():

                              print(key)

                2)、# 遍历字典的value

                        for value in dict1.values():

                               print(value)

                3)、# 遍历字典的元素(items)

                        for item in dict1.items():

                            print(item)

                4)、# 遍历字典的键值对

                        for key , value in dict1.items():

                            print(f'{key} = {value}')

                    print('%s = %s' %(key,value))

二、集合

1、特点:集合具有去重功能、不支持下标、无序、集合为可变数据类型

2、创建集合

                1)、# 创建有数据的集合,

                        s1 = {10 ,20,30,40,50}

                        print(s1)#{40, 10, 50, 20, 30}

                        s2 = {10 ,20,30,40,50,20,30,40,50}

                        print(s2)#{40, 10, 50, 20, 30}

                        s3 = set('abcdefg')

                        print(s3)#{'f', 'c', 'd', 'g', 'b', 'a', 'e'}

                2)、# 创建空集合只能用set()创建

                        s4 = set()

                        print(type(s4))#<class 'set'>

                        s5 = {}

                        print(type(5))#<class 'int'>

3、# 集合常见操作方法

                        s6 = {10,103}

                 1)、# 增加数据

                         # 集合.add():增加单一数据,如果增加多个则报错,如果重复,则什么也不做

                                        s6.add(100)

                                        print(s6)#{10, 100, 103}

                          # 集合.update():增加的数据是序列,不能增加单一数据

                                        s6.update([15,50,60])

                                        print(s6)#{100, 103, 10, 15, 50, 60}

                2)、# 删除

                        s7 = {10,50,90,70}

                        # 集合.remove(指定数据):删除指定数据,如果不存在则报错

                                s7.remove(10)

                                print(s7)#{50, 90, 70}

                        # 集合.discard(指定数据):删除指定数据,如果不存在则不报错,什么也不做

                                s7.discard(50)

                                print(s7)#{90, 70}

                                s7.discard(50)

                                print(s7)#{90, 70}

                        # 集合.pop():随即删除数据,返回这个数据

                                s7.pop()

                                print(s7)#{70}

                3)、# 查找数据

                        s8 = {10,50,90,70}

                        # in:判断数据在集合里面,存在True,不在返回False

                                print(10 in s8)#True

                                print(15 in s8)#False

                        # not in :判断数据不在集合里面,不存在True,在返回False

                                print(10 not in s8)#False

                                  print(15 not in s8)#True


最新回复(0)