コレクション(セット)
#2順不同
#セット内の3つの要素は不変型でなければならない
#は集合定義
S = {1,2,3,4,5 }
プリント(S)#1 {1、2、3、4、5}の出力を
#異なる要素の1セット
S1 = {1,2,2,3,4,5,5,5,5,5 }
プリント(S1)#1 出力1 {1,2 ,. 3 ,. 4 ,. 5}
S2 =セット(" こんにちは")#は収集方法2定義
印刷(S2) #の出力{ 'O'、 'E' 、 'H'、 'L'}
いくつかの一般的な方法のセット(追加、クリア、コピー、ポップ、削除、更新)
S = {1、2、3、4、5 }
s.add( 7) #要素が追加
印刷(S) #の出力を1 {1,2 ,. 3 ,. 4 ,. 5 ,. 7}
S1 = {1,2 ,. 3 ,. 4 ,. 5 }
s1.clear() #をクリア
プリント(S1) #の出力設定()
S2 = s.copy() #のコピー
プリント(S2) #の出力。1 {1,2 ,. 3 ,. 4 ,. 5 ,. 7}
S3 = {1、2、3、4、5 }
s3.pop() #ランダムに削除された
プリント(S3) #1 出力2 {3 ,. 4 ,. 5}
S4 = {1,2 ,. 3 ,. 4 ,. 5 }
s4.remove( 3) #指定削除
プリント(S4) #1 の出力{1、2、4、5}
S1 = {1,2,3 }
S2 = {2,3,4 }
s1.add( 4) #は、要素の追加
印刷を(S1)
s1.update(S2) #は、複数の要素を追加することができ
s1.update((7,8)) #タプルが通過することができる
プリント(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%