Python字符串格式化和集合

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#format字符串格式化
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
print(tpl)
# 运行结果:i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
print(tpl)
# 运行结果:i am seven, age 18, really seven
tpl = "i am {:s}, age {:d}".format('seven',18)
print(tpl)
# 运行结果:i am seven, age 18
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
print(tpl)
# 运行结果:i am seven, age 18
tpl = "i am {:s}, age {:d}".format("seven", 18)
print(tpl)
# 运行结果:i am seven, age 18
l=["seven", 18]
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
# 运行结果:1111,17,15,f,F, 1587.623000%,2
msg='i am %s my hobby is %s' % ('lhf','alex')
print(msg)
# 运行结果:i am lhf my hobby is alex
msg='i am %s my hobby is %s' % ('lhf',1)
print(msg)
# 运行结果:i am lhf my hobby is 1
msg='i am %s my hobby is %s' % ('lhf',[1,2])
print(msg)
# 运行结果:i am lhf my hobby is [1, 2]
name='lhf'
age=19
msg='i am %s my hobby is %s' % (name,age)
print(msg)
# 运行结果:i am lhf my hobby is 19

#打印浮点数
tpl = "percent %.2f" % 99.976234444444444444
print(tpl)
# 运行结果:percent 99.98
#打印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444
print(tpl)
# 运行结果:percent 99.98 %
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
print(tpl)
# 运行结果:i am alex age 18
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg)
# 运行结果:i am                                                          lhf my hobby is alex
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg)
# 运行结果:i am                                                          lhf my hobby is alex

#运行效率比较,第一个效率高,推荐使用
print('root','x','0','0',sep=':')
print('root'+':'+'x'+':'+'0','0')
print("==============================================")
#集合
s=set('hello')
print(s)
# 运行结果:{'o', 'e', 'l', 'h'}
s=set(['alex','alex','sb'])
print(s)
# 运行结果:{'sb', 'alex'}
#添加
s={1,2,3,4,5,6}
s.add('s')
s.add('3')
s.add(3)
print(s)
# 运行结果:{1, 2, 3, 4, 5, 6, 's', '3'}
s.clear()
print(s)
# 运行结果:set()
s={'sb',1,2,3,4,5,6}
#随机删
# s.pop()
#指定删除
# s.remove('sb')
# s.remove('hellol') #删除元素不存在会报错
# s.discard('sbbbb')#删除元素不存在不会报错
# print(s)
s=frozenset('hello')
print(s)
#运行结果:frozenset({'o', 'e', 'l', 'h'})
names=['alex','alex','wupeiqi']
names=list(set(names))
print(names)
#运行结果:['wupeiqi', 'alex']
print("==============================================")
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
#求交集
print(p_s,l_s) #{'lcg', 'zjw', 'szw'} {'lcg', 'sb', 'szw'}
print(p_s.intersection(l_s))
#运行结果:{'lcg', 'szw'}
print(p_s&l_s)
#运行结果:{'lcg', 'szw'}
#求并集
print(p_s.union(l_s))
#运行结果:{'lcg', 'sb', 'szw', 'zjw'}
print(p_s|l_s)
#运行结果:{'lcg', 'sb', 'szw', 'zjw'}
#差集
print('差集',p_s-l_s)
#运行结果:差集 {'zjw'}
print(p_s.difference(l_s))
#运行结果:{'zjw'}
print('差集',l_s-p_s)
#运行结果:差集 {'sb'}
print(l_s.difference(p_s))
#运行结果:{'sb'}
#交叉补集
print('交叉补集',p_s.symmetric_difference(l_s))
#运行结果:交叉补集 {'sb', 'zjw'}
print('交叉补集',p_s^l_s)
#运行结果:交叉补集 {'sb', 'zjw'}
print("==============================================")
python_l=['lcg','szw','zjw','lcg']
linux_l=['lcg','szw','sb']
p_s=set(python_l)
l_s=set(linux_l)
print(p_s,l_s)
#运行结果:{'lcg', 'zjw', 'szw'} {'lcg', 'sb', 'szw'}
print('差集',p_s-l_s)
#运行结果:差集 {'zjw'}
p_s=p_s-l_s
p_s.difference_update(l_s)
print(p_s)
#运行结果:{'zjw'}
print("=============================================")
s1={1,2}
s2={2,3,5}
print(s1.isdisjoint(s2))
#运行结果:False
s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))#s1 是s2 的子集
#运行结果:True
print(s2.issubset(s1))#False
#运行结果:False
print(s2.issuperset(s1))#s1 是s2 的父集
#运行结果:True
s1={1,2}
s2={1,2,3}
s1.update(s2) #更新多个值
print(s1)
#运行结果:{1, 2, 3}
s3={1,2}
s4={1,2,3}
s3.add(4) #更新一个值
print(s3)
#运行结果:{1, 2, 4}
s5={1,2}
s6={1,2,3}
s5.union(s6) #不更新
print(s5)
#运行结果:{1, 2}

Leave a Reply