Python學習第七課——集合(set) 和 字符串拼接


集合(set)

# 2 無序 # 3 集合中元素必須是不可變類型 # 定義集合
s = {1,2,3,4,5} print(s) # 輸出結果 {1, 2, 3, 4, 5}

# 1 集合由不同元素組成
s1 = {1,2,2,3,4,5,5,5,5,5} print(s1) # 輸出結果 {1, 2, 3, 4, 5}
 s2=set("hello") # 定義集合方法2
print(s2) # 輸出結果 {'o', 'e', 'h', 'l'}

set中的一些常用方法(add,clear,copy,pop,remove,update)

s = {1, 2, 3, 4, 5} s.add(7)  # 增加元素
print(s)  # 輸出結果 {1, 2, 3, 4, 5, 7}
 s1 = {1, 2, 3, 4, 5} s1.clear() # 清空
print(s1)  # 輸出結果 set()
 s2 = s.copy()  # 復制一份
print(s2)  # 輸出結果 {1, 2, 3, 4, 5, 7}
 s3 = {1, 2, 3, 4, 5} s3.pop() # 隨機刪除
print(s3)  # 輸出結果 {2, 3, 4, 5}
 s4 = {1, 2, 3, 4, 5} s4.remove(3)  # 指定刪除 
print(s4)  # 輸出結果 {1, 2, 4, 5}

 

s1={1,2,3} s2={2,3,4} s1.add(4) # 添加 一個元素
print(s1) s1.update(s2) # 可以添加多個元素
s1.update((7,8)) # 也可以傳元組
print(s1) # 輸入結果 {1, 2, 3, 4, 7, 8}

 

集合(交,差,並)

python_1 = ['hanhan', 'meimei', 'haohao'] linux_1 = ['hanhan', 'meimei'] # 求這兩個列表的交集
p_s = set(python_1)  # 創建集合
l_s = set(linux_1)  # 創建集合
print(p_s.intersection(l_s))  # 輸出結果 {'hanhan', 'meimei'}
print(p_s & l_s)  # 輸出結果 {'hanhan', 'meimei'}

# 求兩個列表的並集

print(p_s.union(l_s))  # 輸出結果 {'meimei', 'hanhan', 'haohao'}
print(p_s | l_s)  # 輸出結果 {'meimei', 'hanhan', 'haohao'}

# 求差集 差集:兩個集合相差的元素
print(p_s.difference(l_s))  # 輸出結果 {'haohao'}
print(p_s - l_s)  # 輸出結果 {'haohao'}
print(l_s.difference(p_s))  # 輸出結果 set()
print(l_s - p_s)  # 輸出結果 set()

# 交叉補集
print(p_s.symmetric_difference(l_s)) # 輸出結果 {'haohao'}
print(p_s^l_s) # 輸出結果 {'haohao'}

 字符串格式化

# 字符串格式化
msg='i am hanhan'+' my hobby is coding' # 此方法容易占空間 不太好
print(msg) # 輸出結果 i am hanhan my hobby is coding
 msg1='i am %s my hobby is coding' % ('hanhan') # % 更實用
print(msg1) # 輸出結果 i am hanhan my hobby is coding
 msg2='i am %s my hobby is %s and i am %d yeas old' % ('hanhan','coding',23)  # % 更實用
print(msg2) # 輸出結果 i am hanhan my hobby is coding and i am 23 yeas old
 msg4='percent %.2s' % 99.987456456456 # %.2s 就是后面字符串的簡單截取
print(msg4) # 輸出結果 99
 msg5='percent %.3s' % 99.987456456456 # %.3s 就是后面字符串的簡單截取
print(msg5) # 輸出結果 99.
 msg6='percent %.4s' % 99.987456456456 # %.4s 就是后面字符串的簡單截取
print(msg6) # 輸出結果 99.9
 msg3='percent %.2f' % 99.987456456456 # .2就是小數點后面保留兩位有效數字
print(msg3) # 輸出結果 percent 99.99

# 打印百分比
msg7='percent: %.2f %%' % 99.987456456456
print(msg7) # percent: 99.99 %

# 以字典的形式來完成輸入
msg8='i am %(name)s and i am %(age)d yeas old' %{'name':'hanhan','age':23} print(msg8) # 輸出結果 i am hanhan and i am 23 yeas old

# 也可以加上顏色和距離
msg9='i am %(name)+30s and i am %(age)d yeas old' %{'name':'hanhan','age':23} print(msg9) # 輸出結果 i am hanhan and i am 23 yeas old

print('ip','107','111','23','51',sep=':') # 輸出結果 ip:107:111:23:51

format用法

msg = 'i am {} i am {} yeas old my hobby is {}'.format('hanhan', 23, 'coding') # 如果{} 中沒有索引,則將一一對應后面的值,如果對應不了則報錯
print(msg)  # 輸出結果 i am hanhan i am 23 yeas old my hobby is coding
 msg1 = 'i am {0} i am {1} yeas old my hobby is {2}'.format('hanhan', 23, 'coding') # format 將會按照前面{}中的索引來傳值
print(msg1)  # 輸出結果 i am hanhan i am 23 yeas old my hobby is coding
 msg2 = 'i am {0} i am {0} yeas old my hobby is {0}'.format('hanhan', 23, 'coding') print(msg2)  # 輸出結果 i am hanhan i am hanhan yeas old my hobby is hanhan # 也可以按照對象名字來傳值
msg3 = 'i am {name} i am {age} yeas old my hobby is {hobby}'.format(name='hanhan', age=23, hobby='coding') print(msg3)  # 輸出結果 i am hanhan i am 23 yeas old my hobby is coding # 也可以按照列表來傳值
msg4 = 'i am {0[0]} i am {1[1]} yeas old my hobby is {0[2]}'.format(['hanhan', 80, 'coding'], [11, 23, 25]) print(msg4)  # 輸出結果 i am hanhan i am 23 yeas old my hobby is coding
 l=['hanhan',18,'coding'] # 如果需要動態傳入列表 必須加上*
msg5 = 'i am {:s} i am {:d} yeas old my hobby is {:s}'.format(*l) print(msg5) # 輸出結果 i am hanhan i am 18 yeas old my hobby is coding
 msg6 = 'number:{:b},{:o},{:d},{:x},{:X},{:%}'.format(15,15,15,15,15,15) print(msg6) # 輸出結果 number:1111,17,15,f,F,1500.000000%

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM