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 ...
随机推荐
- BZOJ3262陌上花开
三维偏序的模板. 当然各种树套树都可以搞,这里用CDQ分治弄一下. 首先利用排序使第一维有序,然后利用cdq函数开始执行类似归并排序的操作,由于左区间的第一维一定小于右区间的第一维,所以我们在归并过程 ...
- JS中的常用的代码操作
本文件介绍常用的js代码的DOM操作.CSS操作.对象(Object对象.Array对象.Number对象.String对象.Math对象.JSON对象和Console对象)操作说明. 一.DOM树的 ...
- 20175234 2018-2019-2 《Java程序设计》第十周学习总结
目录 20175234 2018-2019-2 <Java程序设计>第十周学习总结 教材学习内容总结 12.1进程与线程 12.2 Java中的线程 12.3 Thread类与线程的创建 ...
- 20165223《网络对抗技术》Exp 9 Web安全基础
目录 -- Web安全基础 ★ 实验说明 实验目标 基础问答 实验准备 ★ 实验内容 SQL注入攻击 1. 命令注入(Command Injection) 2. 数字型注入(Numeric SQL I ...
- python3 与linux间的小知识
1 1秒启动一个下载器 python -m http.server laso@laso-beta03 ms_product]$ python3 -m http.server Serving HTTP ...
- php - thinkphp3.2-phpQrcode生成二维码
import('/Doctor.Logic.phpqrcode',APP_PATH,'.php');// import('@.Doctor.Logic');$value = 'http://www.c ...
- VS2017调试程序时 无法启动web iis或者提示ID为***的进程未启动
打开项目的.csproj文件,定位到<WebProjectProperties>,把关于IIS的配置<DevelopmentServerPort>.<Developmen ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 网络爬虫urllib:request之urlopen
网络爬虫urllib:request之urlopen 网络爬虫简介 定义:按照一定规则,自动抓取万维网信息的程序或脚本. 两大特征: 能按程序员要求下载数据或者内容 能自动在网络上流窜(从一个网页跳转 ...
- IDEA 不编译java以外的文件
解决办法:修改pom 文件 <build> <resources> <resource> <directory>src/main/java</di ...