python1-集合函数-(全局变量与局部变量)
Posted mqhpy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python1-集合函数-(全局变量与局部变量)相关的知识,希望对你有一定的参考价值。
集合
# s=set(‘hello‘)
# print(s)
#
# s=set([‘alex‘,‘alex‘,‘sb‘])
# print(s)
# s={1,2,3,4,5,6}
#添加
# s.add(‘s‘)
# s.add(‘3‘)
# s.add(3)
# print(s)
# s.clear()==>清空
# print(s)
# s1=s.copy()==>拷贝
s={‘sb‘,1,2,3,4,5,6}
#随机删
# s.pop()
#指定删除(remove())
# s.remove(‘sb‘)
# s.remove(‘hellol‘) #删除元素不存在会报错
# s.discard(‘sbbbb‘)#删除元素不存在不会报错discard()
# print(s)
# python_l=[‘lcg‘,‘szw‘,‘zjw‘,‘lcg‘]
# linux_l=[‘lcg‘,‘szw‘,‘sb‘]
# p_s=set(python_l)
# l_s=set(linux_l)
# #求交集intersection()
# print(p_s,l_s)
# print(p_s.intersection(l_s))
# print(p_s&l_s)
# #求并集union()
# print(p_s.union(l_s))
# print(p_s|l_s)
# #差集difference()
# print(‘差集‘,p_s-l_s)
# print(p_s.difference(l_s))
# print(‘差集‘,l_s-p_s)
# print(l_s.difference(p_s))
#交叉补集symmetric_difference()
# print(‘交叉补集‘,p_s.symmetric_difference(l_s))
# print(‘交叉补集‘,p_s^l_s)
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)
# print(‘差集‘,p_s-l_s)
# p_s=p_s-l_s
p_s.difference_update(l_s)
print(p_s)
# s1={1,2}
# s2={2,3,5}
# print(s1.isdisjoint(s2))
s1={1,2}
s2={1,2,3}
print(s1.issubset(s2))#s1 是s2 的子集
print(s2.issubset(s1))#False
print(s2.issuperset(s1))#s1 是s2 的父集
s1={1,2}
s2={1,2,3}
# s1.update(s2) #更新多个值
# s1.add(1,2,3,4) #更新一个值
# s1.union(s2) #不更新
print(s1)
s=frozenset(‘hello‘)
print(s)
names=[‘alex‘,‘alex‘,‘wupeiqi‘]
names=list(set(names))
print(names)
msg=‘i am %s my hobby is %s‘ % (‘lhf‘,‘alex‘) print(msg)
msg=‘i am %s my hobby is %s‘ % (‘lhf‘,1) msg=‘i am %s my hobby is %s‘ % (‘lhf‘,[1,2]) print(msg)
name=‘lhf‘ age=19 msg=‘i am %s my hobby is %s‘ % (name,age) print(msg)
打印浮点数
tpl = "percent %.2f" % 99.976234444444444444 print(tpl)
打印百分比
tpl = ‘percent %.2f %%‘ % 99.976234444444444444 # print(tpl)
实例
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} print(tpl)
msg=‘i am %(name)+60s my hobby is alex‘ %{‘name‘:‘lhf‘} print(msg)
插入时间
msg=‘i am 33[43;1m%(name)+60s 33[0m my hobby is alex‘ %{‘name‘:‘lhf‘} print(msg)
print(‘root‘,‘x‘,‘0‘,‘0‘,sep=‘:‘)
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {:s}, age {:d}".format("seven", 18)["seven", 18] l=["seven", 18] tpl = "i am {:s}, age {:d}".format(‘seven‘,18) print(tpl)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) print(tpl)
python_l=[‘lcg‘,‘szw‘,‘zjw‘] linux_l=[‘lcg‘,‘szw‘] python_and_linux_l=[] for p_name in python_l: if p_name in linux_l: python_and_linux_l.append(p_name) print(python_and_linux_l)
以上是关于python1-集合函数-(全局变量与局部变量)的主要内容,如果未能解决你的问题,请参考以下文章
学习笔记1(三元运算深浅拷贝动态参数全局变量与局部变量set数据类型 )