定義方式:大括號&逗號 {1,2,3,4}python
s = set(可迭代類型)linux
s = set('hello')
print(s)
s = set(['cyx', 'cyx', 'gb'])
print(s)
補:type()查看類型app
可變集合(set):可添加和刪除元素,非可哈希的,不能用做字典的鍵,也不能作其它集合的元素orm
不可變集合(frozenset):不 可添加和刪除元素索引
print(set('cyx') == set('ypp'))
print(set('cyx') != set('ypp'))
s = {1, 2, 3, 4}
s.add('s')
print(s)
s = {1, 2, 3, 4}
s.clear()
print(s)
s = {1, 2, 3}
p = s.pop()
print(s)
print(p)
s = {1, 2, 3}
del s
print(s)#會報錯
s = {'s', 1, 2, 3, 4}
s.pop()
print(s)
s = {'l', 1, 2, 3, 4}
s.remove('l')
print(s)
s = {'l', 1, 2, 3, 4}
s.discard('l0')
print(s)
用for循環求交集rem
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw']
python_linux_l = []
for i in python_l:
if i in linux_l:
python_linux_l.append(i)
print(python_linux_l)
intersection求交集字符串
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.intersection(l_s))
a & b求交集io
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s & l_s)
union求並集for循環
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.union(l_s))
|求並集form
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s | l_s)
difference求差集
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.difference(l_s))#in p_s but not in l_s
-求差集
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s - l_s)
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.symmetric_difference(l_s))
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s ^ l_s)
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
p_s.difference_update(l_s)
print(p_s)
python_l = ['lcg', 'szw', 'zjw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.isdisjoint(l_s))
python_l = ['lcg', 'szw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.issubset(l_s))
print(set('cyx') < set('cyx'))
print(set('cyx') < set('cyxypp'))
python_l = ['lcg', 'szw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
print(p_s.issuperset(l_s))
也能夠用>判斷
s = {1, 2, 3}
s.update('cyx')#cyx會被當成序列一個一個添加而不是總體添加
print(s)
s = {1, 2, 3}
s.update([12, 'ypp'])
print(s)
python_l = ['lcg', 'szw']
linux_l = ['lcg', 'szw', 'cyx', 'ypp']
p_s = set(python_l)
l_s = set(linux_l)
p_s.update(l_s)
print(p_s)
s = frozenset('hello')
print(s)
names = ['cyx', 'ypp', 'cyx']
s = set(names)
names = list(s)
print(names)
msg = 'i am %s my hobby is reading' % 'cyx'
print(msg)
msg = 'i am %s my hobby is %s' % ('cyx', 'reading')
print(msg)
%s是萬能的,能夠接收任何值
%d表明整形數字,只能接收數字
msg = 'i am %(name)s age %(age)d' % {'name': 'cyx', 'age': 18}
print(msg)
msg = 'i am %(name)-60s age 18' % {'name': 'cyx'}
print(msg)
msg = 'i am %(name)+60s age 18' % {'name': 'cyx'}
print(msg)
msg = 'i am \033[45;1m%(name)+60s\033[0m age 18' % {'name': 'cyx'}
print(msg)
msg = 'percent %f' % 99.976234
print(msg)
msg = 'percent %.2f' % 99.976234
print(msg)
msg = 'percent %.2f %%' % 99.976234
print(msg)
msg = 'i am %(pp).2f' % {'pp': 123.45687}
print(msg)
print('root', 'x', '0', '1', sep=':')
a = 'i am {}, age {}, {}'.format('cyx', 18, 'ypp')
print(a)
不一一對應會報錯
a = 'i am {}, age {}, {}'.format('cyx', 18)
print(a)
按索引取值
a = 'i am {2}, age {1}, {0}'.format('cyx', 18, 'ypp')
print(a)
a = 'i am {1}, age {1}'.format('cyx', 18, 'ypp')
print(a)
字典方式取值
a = 'i am {name}, age {age}, really {name}'.format(name='seven', age=18)
print(a)
a = 'i am {name}, age {age}, really {name}'.format(**{'name': 'seven', 'age': 18})
print(a)
a = 'i am {0[0]}, age {1[2]}, really {1[1]}'.format([1, 2, 3], [5, 6, 8])
print(a)
a = 'i am {:s}, age {:d}, really {:f}'.format('cyx', 12, 78.906)
print(a)
a = 'i am {:s}, age {:d}'.format(*['cyx', 12])
print(a)
a = 'i am {name:s}, age {age:d}'.format(name='seven', age=18)
print(a)
a = 'i am {name:s}, age {age:d}'.format(**{'name': 'seven', 'age': 18})
print(a)
b二進制,o八進制,d整形,x十六進制,X十六進制(傳大寫的ABCD),%顯示百分比(默認顯示小數點後六位)
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)print(tpl)