• Python学习(五)—— 集合和字符格式化


    数据类型和变量的总结

    字符串

    数字

    列表

    元组

    字典

    分类

    1、可变不可变: 

    可变(即修改变量值以后id不改变):列表、字典

    不可变(即修改变量值以后id改变):字符串、数字、元组

    2、访问顺序:

    直接访问:数字

    顺序访问:字符串、列表、元组

    映射访问:字典

     3、存放元素个数:

    容器类型:列表、元组、字典

    原子类型:数字、字符串

    集合

    定义:由不同元素组成的集合,集合中是一组无序排列的可hash值(元素必须都是不可变类型:字符串、数字、元组,可以作为字典的key

    特点:集合的目的是将不同值存放在一起,不同的集合间用","分开

     1 s = {1,3,5,2,6,5}
     2 print(s)
     3 #{1, 2, 3, 5, 6}
     4 for i in s:
     5     print(i)
     6 """
     7 1
     8 2
     9 3
    10 5
    11 6
    12 """
    13 s1 = set("alexx")
    14 print(s1)
    15 #{'l', 'a', 'x', 'e'}

     注意set()定义的使用方式:

    1 s2 = set(["alex","alex","sb"])
    2 #s2 = set("alex","alex","sb")=>Wrong
    3 print(s2)
    4 #{'alex', 'sb'}

    注意:另一种定义方式可以将集合定义为不可变类型

    1 s = frozenset("hello")
    2 print(s)
    3 #frozenset({'e', 'o', 'l', 'h'})

    集合的运算关系

    .intersection()    & 

    求交集

     1 English = ["Lily", "Sally", "Tom", "Paul"]
     2 Math = ["Lily", "Sally", "John"]
     3 English_s = set(English)
     4 Math_s = set(Math)
     5 print(English_s, Math_s)
     6 print(English_s.intersection(Math_s))
     7 """
     8 {'Sally', 'Paul', 'Lily', 'Tom'} {'Sally', 'Lily', 'John'}
     9 {'Sally', 'Lily'}
    10 """

    .union()      | 

    求并集

    1 English = ["Lily", "Sally", "Tom", "Paul"]
    2 Math = ["Lily", "Sally", "John"]
    3 English_s = set(English)
    4 Math_s = set(Math)
    5 print(English_s|Math_s)
    6 """
    7 {'Sally', 'Paul', 'John', 'Tom', 'Lily'}
    8 """

    .difference()    - 

    求差集

    1 Math = ["Lily", "Sally", "John"]
    2 English_s = set(English)
    3 Math_s = set(Math)
    4 print(English_s-Math_s)
    5 print(Math_s-English_s)
    6 """
    7 {'Paul', 'Tom'}
    8 {'John'}
    9 """

    .symmetric_difference()   ^

    求交叉补集(就是把各个集合独有的元素都取出来)

    1 English = ["Lily", "Sally", "Tom", "Paul"]
    2 Math = ["Lily", "Sally", "John"]
    3 English_s = set(English)
    4 Math_s = set(Math)
    5 print(English_s.symmetric_difference(Math_s))
    6 """
    7 {'John', 'Tom', 'Paul'}
    8 """

    集合的类中功能

    .add()

      添加元素

     .clear() 

      清空

    .copy() 

      拷贝

    .pop() .remove(x) .discard(x)  

      第一个任意删除,第二个删除指定的x,若不存在则报错,第三个删除不存在元素不报错

    1 s = {1,3,5,2,6,5,"sad"}
    2 s.pop()
    3 print(s)
    4 #{2, 3, 5, 6, 'sad'}
    5 s.remove(5)
    6 print(s)
    7 #{2, 3, 6, 'sad'}
    8 s.discard("sadddd")
    9 print(s)

    .difference_update() 

    求完差集后更新

    1 English = ["Lily", "Sally", "Tom", "Paul"]
    2 Math = ["Lily", "Sally", "John"]
    3 English_s = set(English)
    4 Math_s = set(Math)
    5 English_s.difference_update(Math_s)
    6 print(English_s)
    7 #{'Paul', 'Tom'}

    .isdisjoint() 

    判断两个集合是否没有交集,没有则返回True,有则返回False

    1 s1 = {1,2,3}
    2 s2 = {2,3,4}
    3 print(s1.isdisjoint(s2))
    4 #False

    s1.issubset(s2)

    判断s1是否是s2的子集

    1 s1 = {1,2,3}
    2 s2 = {2,3}
    3 print(s2.issubset(s1))
    4 #True

    s1.issuperset(s2)

    判断s1是否是s2的父集 

    1 s1 = {1,2,3}
    2 s2 = {2,3}
    3 print(s1.issuperset(s2))
    4 #True

     s1.update(s2) 

    将s1更新为s2

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

    .add()

    与update区别在于该功能只能添加一个值,而update不限

    1 s1 = {1,2,3}
    2 s1.add(5)
    3 print(s1)
    4 #{1, 2, 3, 5}
    1 s1 = {1,2,3}
    2 s1.update({5,6})
    3 print(s1)
    4 #{1, 2, 3, 5, 6}

    字符串格式化

    %s

    1 msg = "I am %s and I like eating"%"Jenny"  #%是固定格式,s代表是字符串形式
    2 print(msg)
    3 #I am Jenny and I like eating
    4 info = "I am %s and I like %s" %("Kit", "dancing")
    5 print(info)
    6 #I am Kit and I like dancing

    注意:虽然是%s,但其实可以接受任何类型

       但是使用%d,只能接受数字类型

    %(key)s

    可以使用key的形式来拼接值

    %.xf

    打印x位浮点数

    1 perct = 95.76778978757875378538
    2 msg = "the percent is %.2f"%perct
    3 print(msg)
    4 #the percent is 95.77

    %.xf  %%

    打印百分比

    1 perct = 95.76778978757875378538
    2 msg = "the percent is %.2f %%"%perct
    3 print(msg)
    4 #the percent is 95.77 %

    .format() 格式化

    空的{}来传值,不一一对应则报错

    {0},{1},{2}...可以打乱顺序重新对应

    字典形式

    可以用字典形式传值,前面加**

    可以用元组形式传值,前面加*

     1 s1 = "I am {}, I hate {}".format("Kit","sleeping")
     2 print(s1)
     3 #I am Kit, I hate sleeping
     4 s2 = "I am {0}, I like{2}  but hate {1}".format("Kit","sleeping","eating")
     5 print(s2)
     6 #I am Kit, I likeeating  but hate sleeping
     7 s3 = "I am {name}, I like {hobby}, but hate {fruit}".format(name="Kit",hobby="music",fruit="banana")
     8 print(s3)
     9 #I am Kit, I like music, but hate banana
    10 s4 = "I am {name}, I like {hobby}, but hate {fruit}".format(**{"name":"Kit","hobby":"music","fruit":"banana"})
    11 print(s4)
    12 #I am Kit, I like music, but hate banana
    13 s5 = "I am {}, I like {}, but hate {}".format(*("Kit","music","banana"))
    14 print(s5)
    15 #I am Kit, I like music, but hate banana
    16 s6 = "I am {:s}, my age is {:d}, and my score is {:%}".format(*("Kit",18,0.15))
    17 print(s6)
    18 #I am Kit, my age is 18, and my score is 15.000000%
  • 相关阅读:
    二分查找算法
    js 分享QQ、QQ空间、微信、微博
    linux安装redis
    redis linux开机启动 (简单高效)
    js 自定义阻止事件冒泡函数
    js常见删除绑定的事件
    js自定义方法绑定元素事件
    js 中 attachEvent 简示例
    idea无法正常显示jsp页面
    get请求的时候特殊符号的注意事项
  • 原文地址:https://www.cnblogs.com/jennifer224/p/12354994.html
Copyright © 2020-2023  润新知