python之路(2)集合(set)和字符串格式化
目录
集合(set)
{‘a’,'b','c','d','e'}
定义:有不同元素组成的集合,集合的元素为不可变类型(数字,字符串,元组),集合是一组无序排列的可hash值,可以作为字段的key
注:进行修改值操作后不改变id值的变量类型( id(argv)不发生改变 即内存地址不发生改变 )为不可变类型
集合的创建:
定义可变量集合set >>> set_test=set('hello') 或 set_test=set(['hello','123','chen'])
定义不可变量集合set >>> set_test=frozenset('hello')
集合的关系测试
a = {'aa','bb','cc','dd'}
b = {'aa','bb','cc'}
交集:取两个集合相交的部分
a.intersection(b)
a&b
并集: 两个集合联合,去掉相同的部分
a.union(b)
a|b
差集:取一个集合在另一个集合没有的部分
a.difference(b)
a-b
交叉补集:联合两个集合,去掉共同的部分
a.symmetric_difference(b)
a^b
集合的方法:
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object Build an unordered collection of unique elements.
""" def add(self, *args, **kwargs): # real signature unknown
"""添加一个元素到集合到集合中"""
"""
Add an element to a set. This has no effect if the element is already present.
"""
pass def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass def difference(self, *args, **kwargs): # real signature unknown
"""差集 a-b"""
"""
Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.)
"""
pass def difference_update(self, *args, **kwargs): # real signature unknown
"""取差集并更新集合"""
""" Remove all elements of another set from this set. """
pass def discard(self, *args, **kwargs): # real signature unknown
"""删除元素,如果没找到,返回none"""
"""
Remove an element from a set if it is a member. If the element is not a member, do nothing.
"""
pass def intersection(self, *args, **kwargs): # real signature unknown
"""交集 a&b"""
"""
Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
"""
pass def intersection_update(self, *args, **kwargs): # real signature unknown
"""取交集并更新集合"""
""" Update a set with the intersection of itself and another. """
pass def isdisjoint(self, *args, **kwargs): # real signature unknown
"""判断两个交集是否有交集"""
""" Return True if two sets have a null intersection. """
pass def issubset(self, *args, **kwargs): # real signature unknown
"""判断一个集合是不是另一个集合的子集"""
""" Report whether another set contains this set. """
pass def issuperset(self, *args, **kwargs): # real signature unknown
"""判断一个集合是不是另一个集合的父集"""
""" Report whether this set contains another set. """
pass def pop(self, *args, **kwargs): # real signature unknown
"""随机删除一个元素"""
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass def remove(self, *args, **kwargs): # real signature unknown
"""指定元素删除,如果没找到,报异常"""
"""
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
"""
pass def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""交叉补集 a^b"""
"""
Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.)
"""
pass def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
"""取交叉补集并更新集合"""
""" Update a set with the symmetric difference of itself and another. """
pass def union(self, *args, **kwargs): # real signature unknown
"""并集 a|b"""
"""
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass def update(self, *args, **kwargs): # real signature unknown
"""添加集合,参数为可迭代对象"""
""" Update a set with the union of itself and others. """
pass
set_method
字符串的格式化
1. 百分号方式
%[(name)[flags][width].[precision]tyecode]
- (name) 【可选】用于指定key
- flags 【可选】
- + 右对齐:正数前加负号
- - 左对齐:负数前加正号
- 空格 右对齐
- 0 左对齐
- width 【可选】指定宽度
- .precision 【可选】小数点后保留位数
- typecode 【必选】
- s 获取对象为str类型
- r 获取对象是可打印类型
- c 将数字转换成unicode对应的值
- x 将整数转换成八进制表示
- d 将整数转换成十进制表示
- e 将整数,浮点数转换成科学表示法
test = 'i am %s' %'chen' test = 'i am %s,age %d' %('hello',21) test = 'i am %(name)s,age %(age)d' %{"name":'hello',"age":21} test = 'i am %.2f' %27.22454 test = 'i am %(pp).2f' %{"pp":27.22454} test = 'i am %(pp).2f %%' %{"pp":27.22454} #打印百分号
2.format方式
[[file]align][sign][#][0][width][,][.precision][type]
- file 【可选】空白处填充字符
- align 【可选】对齐方式
- < 内容左对齐
- > 内容右对齐
- ^ 内容居中
- sign 【可选】有无符号数字
- + 正号加负,负号加正
- - 正号不变,负号加正
- 空格 正号空格,符号加负
- # 【可选】对于二进制,八进制,十六进制,如果加上#,会显示0b/0o/0x,否则不显示
- , 【可选】为数字添加分割符
- width 【可选】格式化位所占宽度
- .precision 【可选】小数位保留精度
- type 【可选】格式化类型
- s 格式化字符串类型
- 空白 同s
- b 将十进制自动转化成二进制然后格式化
- c 将十进制自动转化成对应的unicode字符
- d 十进制整数
- x 将十进制自动转换成十六进制然后格式化(小写x)
- X 将十进制自动转换成十六进制然后格式化(大写X)
- e 将数字转换成科学计数法(小写e)
- E 将数字转换成科学计数法(大写E)
- f 转换成浮点型(默认小数后6位)
- F 转换成浮点型(默认小数后6位)
- g 自动在e和f中切换
- G 自动在E和F中切换
- % 显示百分比(默认显示6位)
test = 'i am {}'.format("chen") test = 'i am {},age {}'.format(*["chen",21])#一个*表示传列表 test = 'i am {1},age {0}'.format(21,"chen") test = 'i am {name}'.format(name="chen") test = 'i am {name}'.format(**{"name":"chen"})#两个*表示传字典 test = 'i am {0[0]},,age {0[1]}'.format(["chen",21]) test = 'i am {:s},,age {:d}'.format("chen",21) test = 'i am {:s},,age {:d}'.format(*["chen",21]) test = 'i am {name:s},age {age:d}'.format(name="chen",age=21) test = 'i am {name:s},age {age:d}'.format(**{"name":"chen","age":21}) test = 'numbers : {:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,0.15) test = 'numbers : {0:b},{0:o},{0:d},{0:x},{0:X},{0:%}'.format(15) test = 'numbers : {num:b},{num:o},{num:d},{num:x},{num:X},{num:%}'.format(num=15)
python之路(2)集合(set)和字符串格式化的更多相关文章
- 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别
<Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...
- Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?
Python中使用百分号占位符的字符串格式化方法中%s和%r表示需要显示的数据对应变量x会以str(x)还是repr(x)输出内容展示. 关于str和repr的关系请见: <Python中rep ...
- Python学习之路——基础篇(1)字符串格式化
字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 百分号方式 ...
- Python小白学习之路(九)—【字符串格式化】【百分号方式】【format方式】
写在前面: 最近的事情好像有很多.李咏的离去,让我觉得很突然,仿佛印象中就是主持节目的他,看着他和哈文的爱情,很是感动.离去,没有什么抱怨,只是遗憾. 总会感慨,时光的流逝. 好像真的很快,转眼间,我 ...
- python之路-基本数据类型之str字符串
1.概念 python中用',",''',"""引起来的内容称为字符串,可以保存少量数据并进行相应的操作 #先来看看str的源码写了什么,方法:按ctrl+鼠标 ...
- 复习python(条件判断、循环、字符串格式化)
1.条件判断: 只有一种 if: *** elif:#多个条件加elif,想加几个加几个 **** else: **** python里靠缩进来表示表示语句块,见到冒号,下行就要缩进 2.循环 两种, ...
- python之条件判断、循环和字符串格式化
1. python的条件判断:if和else 在条件判断中可以使用算数运算符 等于:== 不等于:!= 大于:> 小于:< 大于等于:>= 小于等于:<= 示例1: usern ...
- Python之循环条件、变量、字符串格式化
一.认识python python语言的优缺点,自行百度,这里不概述,简单说下,python是一门面向对象,解释型计算机语言.那么问题来了,解释型和编译型语言有什么区别? 1.解释型和编译型语言区别 ...
- Python之路 day2 集合的基本操作
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author:ersa ''' #集合是无序的 集合的关系测试, 增加,删除,查找等操作 ''' #列表去重 ...
随机推荐
- 把exe注册为windows服务
1.需要工具 Instsrv.exe(可以给系统安装和删除服务) Srvany.exe(可以让程序以服务的方式运行) 2.运行cmd,输入注册服务命令 "instsrv.exe完整路径&qu ...
- Java多线程基础(二)
1.多线程数据安全 线程同步:多个线程需要访问同一资源时,需要以某种顺序来确定该资源某一时刻只能被一个线程使用.从而,解决并发操作可能带来的异常. 2.同步代码块实现同步(部分代码的访问,我们希望它同 ...
- LeetCode算法题-Range Addition II(Java实现)
这是悦乐书的第271次更新,第285篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第138题(顺位题号是598).给定一个m行n列的新二维数组M,其初始值为0.提供一个二 ...
- day4-python基础-数据类型
今日份小技巧 a =3 b=4, 最快将a和b值替换的方法为 a,b =b,a 今日内容 1. 字典 2. 集合 3.hash 4.基本数据类型总结 5.循环之for循环 6.range的使用 7.深 ...
- Mango 基础知识
1 mongdb和python交互的模块 pymongo 提供了mongdb和python交互的所有方法 安装方式: pip install pymongo 2 使用pymongo 1. 导入pymo ...
- VirtualBox修改UUID实现虚拟硬盘的重复利用
其实,记录这个是为了留给自己看.每次用每次查,已经老到什么东西都记不住了.本次查询是从这里(VirtualBox 修改UUID实现虚拟硬盘复制)获得帮助的,感谢. 在VirtualBox把一个已经使用 ...
- odoo中def init(self):
# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. f ...
- 如何用ABP框架快速完成项目(6) - 用ABP一个人快速完成项目(2) - 使用多个成熟控件框架
正如我在<office365的开发者训练营,免费,在微软广州举办>课程里面所讲的, 站在巨人的肩膀上的其中一项就是, 尽量使用别人成熟的框架. 其中也包括了控件框架 abp和52abp ...
- Mac之日常操作
1.创建root用户使用最高权限 sudo passwd root 一般情况下,使用临时获取最高权限 sudo vim /etc/shells 2. apache操作 #启动Apache sudo a ...
- IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别参考
参考https://www.cnblogs.com/aspirant/p/6877350.html?utm_source=itdadao&utm_medium=referral IO复用,AI ...