码迷,mamicode.com
首页 > 其他好文 > 详细

字典 元组 集合

时间:2020-03-30 15:54:39      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:user   个数   应该   集合   word   区别   pre   成员   python3   

一.字典 dict

  1.记录多个值,key是用来描述value的特征

    2.定义

 在{}内有多个key:value值,用逗号分开,
# value可以是任意数据类型,key必须是不可变类型,通常情况下为字符串
# a = {‘name‘:‘jason‘,‘password‘:123}
# a = dict({‘name‘:‘jason‘,‘password‘:123})
# print(a,type(a))
# print(a[‘name‘])
# print(a[‘password‘])

  3.用法

# 用法一
# a = dict(x =1,y = 2,z = 3)
# print(a)  # {‘x‘: 1, ‘y‘: 2, ‘z‘: 3}

# 用法二
# userinfo = [
#     [‘name‘,‘jason‘],
#     [‘age‘,18],
#     [‘sex‘,‘male‘]
# ]
# d = {}
# for k,v in userinfo:
#     d[k]=v
# print(d)
# {‘name‘: ‘jason‘, ‘age‘: 18, ‘sex‘: ‘male‘}

  4.常用操作和内置方法

    1.按key取值,可取可换,则有序的

# a = {‘name‘:‘jason‘,‘password‘:223}
# print(a[‘name‘])
# print(a[‘password‘])
# a[‘name‘] = ‘egon‘
# print(a)
# a[‘password‘] = 123
# print(a)

    2.长度len():统计的是key:value的个数

# a = {‘name‘:‘jason‘,‘password‘:123}
# # print(a)
# # print(len(a))

    3.成员运算in和not in

    字典中成员运算判读的是key

# a = {‘name‘:‘jason‘,‘password‘:123}
# print(‘name‘ in a)  # True
# print(123 in a)  # False

    4.删除

# a = {‘name‘:‘jason‘,‘password‘:123}
# 通用的del:删除整个key:value键值对
# del a[‘name‘]
# print(a)
# del a[‘age‘]
# print(a)  # 不存在报错

# pop 删除的key对应的值,返回value值.应该叫弹出
# a = {‘name‘:‘jason‘,‘password‘:123}
# res = a.pop(‘name‘)
# print(res)  # jason
# print(a)

# popitem 删除的是后面的key:value.应该叫弹出
# a = {‘name‘:‘jason‘,‘password‘:123}
# res = a.popitem()
# print(res)
# print(a)

    5.键keys(),值values(),键值对iiems()

    注意:在Python2 和Python3的区别

      在Python2中是列表,在Python3中是元组

# dic = {‘name‘:‘egon‘,‘age‘:18}
# print(dic.keys())
# print(dic.values())
# print(dic.items())
# dict_keys([‘name‘, ‘age‘])
# dict_values([‘egon‘, 18])
# dict_items([(‘name‘, ‘egon‘), (‘age‘, 18)])

    6.循环

# dic = {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}
# for k in dic.keys():
#     print(k,dic[k])
# name egon
# age 18
# sex male

    7.get() 和按key取值比较就是不报错

# dic = {‘name‘:‘egon‘,‘age‘:123,‘sex‘:‘male‘}
# # dic[‘aaa‘]  # 报错
# # v = dic.get(‘name‘)
# # print(v)
# #
# # v = dic.get(‘sss‘)
# # print(v)  # None 不报错

    8.总结:

      存多个值

      无序的

      可变

 

二.元组 tuple

  与列表相比也可存多个值,但元组内的元素不能够改变

  1,用途:存多个值,值没有被改的需要

 

  2.定义:

    ()内的元素是任意数据类型,元组之间用逗号隔开

# a = (2,2.2,‘jason‘,[1,2])  # a = tuple(2,2.2,‘jason‘,[1,2])
# print(a,type(a))
# a1 = tuple(‘hello‘)
# print(a1)  # (‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘)
# 元组内部for循环,得到是元组

 

  3.常用操作

    1,索引取值,不能够改.  有序的

# a = (1,1.1,‘jason‘,[1,2])
# print(id(a))  # 2777382289640
# print(id(a[0]))
# print(id(a[1]))
# print(id(a[2]))
# print(id(a[3]))
# a[3][0] = 3  # 整形替换整形
# print(id(a))  # 2777382289640

    2,切片(顾头不顾尾)

# a = (1,2,3,4)
# print(a[0:2])  # (1, 2)
# print(a)  # (1, 2, 3, 4)

    3,长度len()

# a = (1,2,3,4)
# a1 = len(a)
# print(a1)  # 4

    4.成员运算in和not in

a = (1,2,3)
print(1 in a)

    5.循环

# a = (1,2,3,4)
# for i in a:
#     print(i)

  4.内置方法

    1,count 统计count括号内元素在元组中的个数

# a = (1,2,3,4)
# res = a.count(1)
# print(res)
# print(a)

    2.index() 查找index括号内的元素在元组中的索引位置,可加范围查找

t=(a,b,c,a)
print(t.index(a,1,4))
# print(t.index(‘xxx‘,1,10))  # 报错

 

三.集合 set

  

# pythons = [‘李二丫‘,‘张金丹‘,‘李银丹‘,‘赵同丹‘,‘张铁蛋‘,‘alex‘,‘oldboy‘]
# linux = [‘lxx‘,‘egon‘,‘张金丹‘,‘张译丹‘,‘alex‘,‘陈独秀‘]
# l = []
# for students in pythons:
#     if students in linux:
#         l.append(students)
# print(l)

 

  1.用途:运算关系 去重

  2.定义方式:在{}内用逗号分隔各个元素

  3.集合的三大特性

    (1),每个值都是不可变的

    (2),元素不能重复

    (3),集合内的元素是无序的

# a = {1,1.1,‘Jason‘,(1,2)}  # a = ser{1,1.1,‘Jason‘,(1,2)
# print(a,type(a))
# {‘Jason‘, 1, (1, 2), 1.1} <class ‘set‘>

# a = {1,1,22,22,3,6,6}
# print(a)
# {1, 3, 22, 6}

# a = {1,‘a‘,2,‘s‘}
# a[0]  # 报错

  4.常用操作和内置方法

  

# a = {1,2,3,4,5}
# b = {3,4,5,6,7}
# # 提取a,b中相同的数字.集合
# print(a & b)  # {3, 4, 5}
# print(a.intersection(b))
#
# # 提取a和b的并集
# print(a | b)  # {1, 2, 3, 4, 5, 6, 7}
# print(a.union(b))
#
# # a 和 b的差集
# print(a - b)  # 1, 2}
# print(a.difference(b))
# print(b - a)  # {6, 7}
# print(b.difference(a))
#
# # a和b都有 交集
# print(a ^ b)  # {1, 2, 6, 7}
# print(a.symmetric_difference((b)))

# 是否相等
# a = {1,2,3,4,5}
# b = {1,2,3,5,4}
# print(a == b)  # True
# 无序的

# 子集
# a = {1,2,3,4}
# b = {1,2,3,5,4}
# print(b >= a)

 

  内置方法

# 1.update
# a = {1,2,3,4}
# a.update({3,4,5})
# print(a)

# 2.add
# a = {1,2,3,4}
# a.add(5)
# print(a)

# 3.pop
# a ={1,2,3,4}
# res = a.pop()
# print(a)
# print(res)

# 4.remove
# a = {1,2,3,4}
# res = a.remove(3)
# print(a)
# print(res)  # None
# 删除的元素不存在不报错

# 5.difference_update() 和difference相同
# a = {1,2,3,4}
# a1 = {1,2}
# a.difference_update(a1)
# print(a)
# a.difference(a1)
# print(a)  # {3, 4}

# 6.discard()
# a = {1,2,3,4}
# res = a.discard(3)
# # print(res)  # None
# print(a)  # {1, 2, 4}
# 删除的元素不存在不报错

# 7.isdisjoint()
# a = {1,2,3}
# a1 = {1,2,4}
# print(a.isdisjoint(a1))
# 两个集合没有交集这为真

  5.集合去重

   局限性

    1,无法保证原数据的顺序

    2,当一个数据包含多个值,每个值为不可变类型才能用集合去重

# names ={‘alex‘,‘egon‘,‘alex‘,‘alex‘,‘egon‘,‘lxx‘}
# s = set(names)
# print(s)
# a = list(s)
# print(a)

# students = [
#     {‘name‘:‘egon‘,‘age‘:18},
#     {‘name‘:‘jason‘,‘age‘:19},
#     {‘name‘:‘oldboy‘,‘age‘:20},
#     {‘name‘:‘egon‘,‘age‘:18},
#     {‘name‘:‘jason‘,‘age‘:19},
# ]
# # set(students)  # 报错
#
# a = []
# for info in students:
#     if info not in a:
#         a.append(info)
# # print(a)
# students = a
# print(students)

 

总结

  1,存多个值

  2,无序

  3,可变类型

字典 元组 集合

标签:user   个数   应该   集合   word   区别   pre   成员   python3   

原文地址:https://www.cnblogs.com/zhuangshenhao/p/11134891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!