集合、字符串格式化

1.集合定義:不一樣元素組成,元素都是無序的,元素是不可變類型/可哈希類型(可做爲字典的鍵)(數字、字符串、元組),去重特性

定義方式:大括號&逗號 {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'))

2.集合的內置方法

a.add 添加

s = {1, 2, 3, 4}
s.add('s')
print(s)

b.clear 清空

s = {1, 2, 3, 4}
s.clear()
print(s)

c.copy 拷貝

d.pop隨機刪

s = {1, 2, 3}
p = s.pop()
print(s)
print(p)

remove指定刪,刪除元素不存在會報錯

del能夠刪除任何東西

s = {1, 2, 3}
del s
print(s)#會報錯

discard指定刪,刪除元素不存在時,不會報錯

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)

e.求交集

用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)

f.求並集

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)

g.求差集

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)

h.交叉補集/對稱差集/反向交集 :先合到一塊兒,在減去共有的

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)

i.difference_update求完差集後更新

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)

j.isdisjoint 判斷是否不相交

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))

k.判斷是不是子集

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'))

l.判斷是不是父集

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))

也能夠用>判斷

m.update更新多個值,add只能更新一個值

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)

n.集合是可變的,能夠用frozenset定義不可變集合

s = frozenset('hello')
print(s)

o.set()能夠簡單去重,可是元素順序會變

names = ['cyx', 'ypp', 'cyx']
s = set(names)
names = list(s)
print(names)

3.字符串格式化

a.百分號字符串拼接

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)

b.打印浮點數

msg = 'percent %f' % 99.976234
print(msg)
msg = 'percent %.2f' % 99.976234
print(msg)

 c.打印百分比

msg = 'percent %.2f %%' % 99.976234
print(msg)
msg = 'i am %(pp).2f' % {'pp': 123.45687}
print(msg)
print('root', 'x', '0', '1', sep=':')

 d.format字符串格式化

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)
相關文章
相關標籤/搜索