• Python:集合与字符串格式化


    1. 集合

      定义:集合是由不同元素组合而成,集合中是一组无序排列的可hash值,可做为字典的key。

      特征:集合的目的是将不同的值放在一起,不同的集合可以用来做关系运算。集合中的元素必须是不可变类型。可以帮助简单的去重。

    1.1集合的创建

    >>> {1,2,3,1}
    {1, 2, 3}

    或者

    >>> set_test = set("hello")
    >>> set_test
    {'o', 'h', 'l', 'e'}

    集合中的元素是不会重复的

    frozenset()函数将集合改变为不可变集合

    >>> f_set_test=frozenset(set_test)
    >>> f_set_test
    frozenset({'o', 'h', 'l', 'e'})

    1.2集合的函数

      1 class set(object):
      2     """
      3     set() -> new empty set object
      4     set(iterable) -> new set object
      5 
      6     Build an unordered collection of unique elements.
      7     """
      8     def add(self, *args, **kwargs): # real signature unknown
      9         """增加元素,传一个参数"""
     10         """
     11         Add an element to a set.
     12 
     13         This has no effect if the element is already present.
     14         """
     15         pass
     16 
     17     def clear(self, *args, **kwargs): # real signature unknown
     18         """清除集合内的所有元素"""
     19         """ Remove all elements from this set. """
     20         pass
     21 
     22     def copy(self, *args, **kwargs): # real signature unknown
     23         """浅拷贝集合内的元素"""
     24         """ Return a shallow copy of a set. """
     25         pass
     26 
     27     def difference(self, *args, **kwargs): # real signature unknown
     28         """求差集 相当于集合return a-b"""
     29         """
     30         Return the difference of two or more sets as a new set.
     31 
     32         (i.e. all elements that are in this set but not the others.)
     33         """
     34         pass
     35 
     36     def difference_update(self, *args, **kwargs): # real signature unknown
     37         """a.difference_update(b) 相当于 a = a - b"""
     38         """ Remove all elements of another set from this set. """
     39         pass
     40 
     41     def discard(self, *args, **kwargs): # real signature unknown
     42         """删除一个元素,元素不存在时不会报错"""
     43         """
     44         Remove an element from a set if it is a member.
     45 
     46         If the element is not a member, do nothing.
     47         """
     48         pass
     49 
     50     def intersection(self, *args, **kwargs): # real signature unknown
     51         """求两个集合的交集;相当于 return a & b"""
     52         """
     53         Return the intersection of two sets as a new set.
     54 
     55         (i.e. all elements that are in both sets.)
     56         """
     57         pass
     58 
     59     def intersection_update(self, *args, **kwargs): # real signature unknown
     60         """求两个集合的交集 更新;相当于 a = a & b"""
     61         """ Update a set with the intersection of itself and another. """
     62         pass
     63 
     64     def isdisjoint(self, *args, **kwargs): # real signature unknown
     65         """如果两个集合的交集为空,返回true"""
     66         """ Return True if two sets have a null intersection. """
     67         pass
     68 
     69     def issubset(self, *args, **kwargs): # real signature unknown
     70         """a.issubset(b) a是否是b的子集 相当于 a <= b"""
     71         """ Report whether another set contains this set. """
     72         pass
     73 
     74     def issuperset(self, *args, **kwargs): # real signature unknown
     75         """a.issubset(b) a是否是b的父集 相当于 a > b"""
     76         """ Report whether this set contains another set. """
     77         pass
     78 
     79     def pop(self, *args, **kwargs): # real signature unknown
     80         """随机删除一个元素"""
     81         """
     82         Remove and return an arbitrary set element.
     83         Raises KeyError if the set is empty.
     84         """
     85         pass
     86 
     87     def remove(self, *args, **kwargs): # real signature unknown
     88         """指定一个元素删除,只能有一个元素;删除元素不存在会报错"""
     89         """
     90         Remove an element from a set; it must be a member.
     91 
     92         If the element is not a member, raise a KeyError.
     93         """
     94         pass
     95 
     96     def symmetric_difference(self, *args, **kwargs): # real signature unknown
     97         """交叉差集; 两个集合;相当于先并集再去除他们相同的元素; a ^ b"""
     98         """
     99         Return the symmetric difference of two sets as a new set.
    100 
    101         (i.e. all elements that are in exactly one of the sets.)
    102         """
    103         pass
    104 
    105     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
    106         """交叉差集更新; 两个集合;相当于先并集再去除他们相同的元素;a = a ^ b"""
    107         """ Update a set with the symmetric difference of itself and another. """
    108         pass
    109 
    110     def union(self, *args, **kwargs): # real signature unknown
    111         """求并集 相当于 return a | b"""
    112         """
    113         Return the union of sets as a new set.
    114 
    115         (i.e. all elements that are in either set.)
    116         """
    117         pass
    118 
    119     def update(self, *args, **kwargs): # real signature unknown
    120         """更新 元素,可以更新多个值,但是'int' object is not iterable;相当于 a = a | b"""
    121         """ Update a set with the union of itself and others. """
    122         pass
    set

    1.3集合的关系运算

    in , not in

    ==, != , <=, <, >=, >

    |, |= : 并集

    &, &= : 交集

    -, -= : 差集

    ^, ^= : 交叉差集

    2.字符串格式化

    Python中字符串格式有两种:百分号方式,format方式。

    2.2百分号字符串的拼接

    %[(name)][flags][width].[precision]typecode

    • name    可选,用于指定的key
    • flags     可选,
      •     +(右对齐前面加符号)
      •      -(左对齐,正数无符号,负数有)
      •      空格(右对齐,正数前加空格,负数前负号)
      •      0(左对齐,正数无符号,负数有;用0填充空白处)
    • width    可选,占有的宽度
    • precision         小数点后保留的位数
    • typecode         必选,格式类型

    常用格式

     1 tp = "i am a %s"%"boy"
     2 
     3 tp = "i am a %s,age %d"%("boy",18)
     4 
     5 tp = "i am a %(sex)s,age %(age)d"%{"sex":"boy","age":18}
     6 
     7 tp = "percent %.2f"%99.888
     8 
     9 tp = "percent %(p).2f"%{"p":123.456}
    10 
    11 tp = "percent %(p).2f%%"%{"p":123.456} #在字符串中连续两个%是表示百分号

    2.3Format方式

    [[fill]align][sign][#][0][width][,][.precision][type]

    • fill                        可选,空白处填充的字符
    • aligh                    可选,对齐方式,需要与width配合使用
      • <               内容左对齐
      • >               内容右对齐(默认)
      • =               内容左对齐,将符号放在填充字符的左侧
      • ^                内容居中
    • sign                     可选 ,有无符号数字
      • +               正数加+,负数加-
      • -                正数不变,负数加-
      • 空格          正数加空格,负数加-
    • #                          可选,对于二,八,十六进制,如果加上#,会显示0b/0o/0x/否则不显示
    • ,                           可选,数字添加分隔符  如 100,000
    • width                   可选,位宽
    • .precision            可选,保留小数点后位数
    • type                     可选,格式化类型

    常用格式:

     1 tp = "i am a {}, age {}, {}".format("boy",18,"abc")
     2 
     3 tp = "i am a {0}, age {0}, {2}".format("boy",18,"abc")
     4 
     5 tp = "i am a {1}, age {0}, {2}".format("boy",18,"abc")
     6 
     7 tp = "i am a {sex}, age {age}, {abc}".format(sex="boy",age=18,abc="abc")
     8 
     9 tp = "i am a {sex}, age {age}, {abc}".format(**{"sex":"boy","age":18,"abc":"abc"})
    10 
    11 tp = "i am a {0}, age {0[0]}, {0[0]}".format([1,2,3],[4,5,6])
    12 
    13 tp = "i am a {:s}, age {:d}, {:f}".format("boy",18,18.123)
    14 
    15 tp = "i am a {:s}, age {:d}, {:f}".format(*["boy",18,18.123])
    16 
    17 tp = "i am a {sex:s}, age {age:d}, {abc:f}".format(**{"sex":"boy","age":18,"abc":18.123})
  • 相关阅读:
    HDU 4452 模拟
    CSUFT2016ACM训练赛4
    HDU 4445 纯物理题+枚举
    HDU 4442 排队贪心
    2016中国大学生程序设计竞赛
    POJ 2239 化二分图右集合二维为一位的最大匹配
    学习数据库必须掌握的54条SQL查询语句
    jsp+连接MYSQL5.1
    查看MYSQL 端口
    J2EE+struts2+helloworld调试问题解决方法
  • 原文地址:https://www.cnblogs.com/kaixindexiaocao/p/9693874.html
Copyright © 2020-2023  润新知