python第五天---集合与format格式化
"""
集合:set
1、由不同元素组成,
2、无序
3、不可变:数字、字符串、元组
不可变类型
"""
s = {1, 2, 3, 4, 1, 6, 3, 4, 5, 6}
print(s) t = {'hello', 'ssad', 'asd', 'asd', 'hello'}
print(t) s1 = set('hello')
print(s1) # s2 = set(['cui', 'hai', 'cheng', 'cui'])
# print('s2:', s2) ss = {1, 2, 3, 4, 5, 6} ss.add('3') # 添加元素,只能一个值
ss.add(32)
print(ss) # ss.clear() # 清空集合
# print(ss) s3 = ss.copy()
print(s3) ss.pop() # 随机删除
print(ss)
# ss.remove('3') # 指定元素删除,不存在会报错
print(ss) ss.discard('ewrewrewr') # 删除元素不存在,不会报错
print(ss) python_l = ['cui', 'hai', 'cheng', 'pipi', 'anan']
linux_l = ['cui', 'pipi', 'cheng', 'kele', 'honr'] p_l = set(python_l)
l_l = set(linux_l) print(p_l, l_l)
print(p_l.intersection(l_l)) # 求p_l中和l_l一样的元素
print(p_l & l_l) # 求p_l和l_l交集 print(p_l.union(l_l)) # 求p_l和l_l一共的元素
print(p_l | l_l) # 求p_l和l_l并集 print("差集:", p_l - l_l) # 求差集
print('chaji:', l_l - p_l)
print(p_l.difference(l_l)) print(p_l.symmetric_difference(l_l)) # 交叉补集
print(p_l ^ l_l) s4 = {1, 2}
s5 = {3, 4}
print(s4.isdisjoint(s5)) # 没有交集的时候返回True s6 = {1, 2, 3}
s7 = {1, 2}
print(s7.issubset(s6)) # s7是s6的子集 print(s6.issuperset(s7)) # s6是s7的父集 s7.update(s6) # 更新多个值
print(s7) # 集合变为不可变的
s9 = frozenset('hello')
print(s9) s8 = ['pipi', 'cui', 'pipi', 'cui', 'cheng']
names = list(set(s8))
print(names) """
字符串格式化
%s 字符串,万能接
%d 整形数字
%f 浮点数(保存六位) %.2f 保留两位
字典形式
"""
print('I am %s ,my hobby is %s' % ('cui', 'read book'))
print('I am is %d' % 20)
print('my percent is %.2f %%' % 98.44444)
print('I am %(name)s age %(age)d' % {'name': 'cui', 'age': 20}) """
format 格式化
""" tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
print(tpl) tp2 = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
print(tp2) tp3 = "i am {0}, age {1}, really {0}".format("seven", 18)
print(tp3) tp4 = "i am {0}, age {1}, really {0}".format(*["seven", 18])
print(tp4) tp5 = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
print(tp5) tp6 = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
print(tp6) tp7 = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
print(tp7) tp8 = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
print(tp8) tp9 = "i am {:s}, age {:d}".format(*["seven", 18])
print(tp9) tpl0 = "i am {name:s}, age {age:d}".format(name="seven", age=18)
print(tpl0) tpl1 = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
print(tpl1) tpl2 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl2) tpl3 = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl3) tpl4 = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
print(tpl4) tpl5 = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15) print(tpl5)
python第五天---集合与format格式化的更多相关文章
- Python学习(五)—— 集合和字符格式化
数据类型和变量的总结 字符串 数字 列表 元组 字典 分类 1.可变不可变: 可变(即修改变量值以后id不改变):列表.字典 不可变(即修改变量值以后id改变):字符串.数字.元组 2.访问顺序: 直 ...
- Python基础 之 set集合 与 字符串格式化
数据类型的回顾与总结 可变与不可变1.可变:列表,字典2.不可变:字符串,数字,元组 访问顺序:1.直接访问:数字2.顺序访问:字符串,列表,元祖3.映射:字典 存放元素个数:容器类型:列表,元祖,字 ...
- 14.python类型总结,集合,字符串格式化
借鉴:https://www.cnblogs.com/linhaifeng/articles/5935801.html https://www.cnblogs.com/wupeiqi/article ...
- python学习6—数据类型之集合与字符串格式化
python学习6—数据类型之集合与字符串格式化 1. 使用id()可以查看一个变量的内存地址: name = 'alex' id(name) 2. 进制转换 十进制转换为二进制等: a = 10 # ...
- 【387】Python format 格式化函数
参考:Python format 格式化函数 # 保留小数点后两位 f'{3.1415926:.2f}' # 带符号保留小数点后两位 f'{3.1415926:+.2f}' f'{-1:+.2f}' ...
- Python format 格式化函数。
Python format 格式化函数 Python 字符串 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 ...
- 【Python】Python format 格式化函数(转帖)
https://www.runoob.com/python/att-string-format.html Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符 ...
- python format格式化函数用法
python format格式化函数用法 原文 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前 ...
- Python format格式化函数
参考资料:https://www.runoob.com/python/att-string-format.html 在学习Python的时候碰到了一个很有趣的格式化输入的技巧,下面记录在此. Pyth ...
随机推荐
- 分布式锁 ----zookeeper实践 (排它锁)
排它锁概念: Exclusive Locks,被称为X锁,写锁,独占锁.如果事物T1对数据对象O1加上了排它锁,那么在整个加锁期间,只允许事务T1对O1进行读写操作,其他事务必须等到T1释放锁后才能进 ...
- 怎么样修改小程序分享的title/onShareAppMessage
onShareAppMessage: function (res) { if (res.from === 'button') { // 来自页面内转发按钮 console.log(res.target ...
- Windows使用Latex
目录 安装Texlive 安装TeXstudio 编写简单的文章 教程 安装Texlive 到清华大学开源软件镜像站下载Texlive2019.iso文件 下载之后,如果有光驱就装载,没有的话就解压. ...
- Mac -- pkg-config: exec: "pkg-config": executable file not found in $PATH
just run: brew install pkg-config
- pytorch加载数据的方法-没弄,打算弄
参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...
- 001-poi-excel-基础、单元格使用操作
一.概述 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...
- 请求头User-Agent作用?
请求头User-Agent作用 答: User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA.它是一个特殊字符串头,是一种向访问网站提供你所 ...
- bat命令编写大全
bat命令编写大全 摘自:https://blog.csdn.net/haibo19981/article/details/52161653 2016年08月09日 12:26:31 爱睡觉的猫L 阅 ...
- css简单学习属性3---css属性选择器
1:通配符 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Chrome浏览器控制网速的方法