鍙橀噺绫诲瀷-Set
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍙橀噺绫诲瀷-Set相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%93%88%e5%b8%8c' title='鍝堝笇'>鍝堝笇
int indexing 鍒ゆ柇 lov values 鐗圭偣 typeerror 搴忓垪鏁欑▼:
闆嗗悎鏄竴涓棤搴忎笉閲嶅鍏冪礌鐨勫簭鍒楋紝瀹冪殑鐗圭偣鏄細鏈夌殑鍙彉锛屾湁鐨勪笉鍙彉锛屽厓绱犳棤搴忎笉鍙噸澶嶏紒
涓€锛氶泦鍚堢殑鍒涘缓
锛?锛変娇鐢ㄨ姳鎷彿
set={value1, values2}
娉ㄦ剰鍒涘缓涓€涓┖闆嗗悎鐨勬椂鍊欎笉鑳界敤{}锛屾鏃跺垱寤虹殑鏄竴涓瓧鍏?br>
锛?锛変娇鐢ㄥ唴鑱斿嚱鏁皊et锛堬級------>鍙互鍘熷湴淇敼鐨勯泦鍚?br>
s = set()
娉ㄦ剰锛?nbsp; 闆嗗悎鏄棤搴忕殑锛堥泦鍚堜笉鏄簭鍒楃被鍨嬶紝鍒欓泦鍚堜篃涓嶆敮鎸佺储寮曞拰鍒囩墖锛夛紝
鎵撳嵃缁撴灉鍙栧喅浜庡唴閮ㄥ瓨鍌ㄧ粨鏋勫拰杈撳嚭鏂瑰紡
锛?锛変娇鐢ㄥ唴鑱斿嚱鏁癴rozenset()---->hashable 鍙搱甯屽崱绫诲瀷
浜岋細闆嗗悎鐨勭储寮曪紙涓嶅厑璁革級
File "<ipython-input-5-9b6a789630ee>", line 1, in <module>
set_1[1]
TypeError: 鈥榮et鈥?object does not support indexing
涓夛細闆嗗悎鐨勮繍绠?br>
锛?锛夊厓绱犱笌闆嗗悎涔嬮棿鐨勫叧绯?br>
鈥榠鈥?in set(鈥榠love鈥?
锛?锛夐泦鍚坅涓庨泦鍚坆涔嬮棿鐨勫叧绯?br>
a == b 鍒ゆ柇鏄惁鐩哥瓑
a < b 鎴栬€?a.issubset(b) 鍒ゆ柇鏄惁涓哄瓙闆?br> a 骞?b ---> a|b 鎴栬€?a.union(b)
a 浜?b ---> a&b 鎴栬€?a.intersection(b)
a 琛?b ---> a-b 鎴栬€?a.difference(b)
a 鍜?b 鐨勫绉板樊闆?nbsp; a^b 鎴栬€?a.symmetric_difference(b)
涓夛細闆嗗悎鍏冪礌鐨勬坊鍔?br>
锛?锛夋柟娉曚竴锛?nbsp; s.add(x)
锛?锛夋柟娉曚簩锛? s.update(x) 鍏朵腑x鍙互鏄垪琛紝鍏冪粍锛屽瓧鍏?br>
鍥涳細闆嗗悎鍏冪礌鐨勫垹闄?br>
锛?锛夋柟娉曚竴锛?nbsp; s.remove(x) 鍏冪礌涓嶅瓨鍦ㄥ彂鐢熼敊璇?br> 锛?锛夋柟娉曚簩锛? s.discard(x) 鍏冪礌涓嶅瓨鍦ㄤ笉浼氬彂鐢熼敊璇?br> 锛?锛夋柟娉曚笁锛? s.pop() 闅忔満鍒犻櫎涓€涓厓绱?br> 锛?锛夋柟娉曞洓锛? s.clear() 娓呯┖闆嗗悎
浜旓細闆嗗悎鍐呯疆鍑芥暟
len锛坰et锛?---set鍏冪礌鐨勬€绘暟
set.add()-----娣诲姞
set.update()--鏇存柊
杩樺寘鎷笂闈㈢殑鍥涗釜鍑芥暟
CODE:
# -----------------------------------------------------------------------------------------------------#
# 闆嗗悎鐨勫垱寤?br># -----------------------------------------------------------------------------------------------------#
set_1 = set(鈥榓bracadabra鈥? # 鍒涘缓鍙彉鐨勯泦鍚?br>print("set_1:", set_1)
set_1.add(鈥榩ython鈥? # 鍏冪礌鐨勬坊鍔?br>print("娣诲姞Python鍚巗et_1:", set_1)
set_2 = set(鈥榓lacazam鈥?
print("set_2:", set_2)
set_3 = frozenset(鈥榝aith鈥? # 鍒涘缓涓嶅彲鍙樼殑闆嗗悎楠岃瘉鍏冪礌涓嶅彲娣诲姞
print("鍒涘缓涓嶅彲鍙橀泦鍚坰et_3:", set_3)
# -----------------------------------------------------------------------------------------------------#
# 闆嗗悎鐨勮繍绠?br># -----------------------------------------------------------------------------------------------------#
print("set_1 - set_2: ", set_1 - set_2)
print("set_1 | set_2: ", set_1 | set_2)
print("set_1 & set_2: ", set_1 & set_2)
print("set_1 ^ set_2: ", set_1 ^ set_2)
# -----------------------------------------------------------------------------------------------------#
# 闆嗗悎鍏冪礌鐨勬坊鍔狅紙瀵逛簬鍙彉闆嗗悎锛?br># -----------------------------------------------------------------------------------------------------#
# 鏂规硶涓€
set_2 = set(鈥榓lacazam鈥?
set_2.add("faith")
print("娣诲姞鍏冪礌鏂规硶涓€:", set_2)
# 鏂规硶浜?br>x = {1, 2, 3, 鈥榓aa鈥榼 # 鍒楄〃
y = (1, 2, 3, 鈥榓aa鈥? # 鍏冪粍
z = {1: 鈥榳ang鈥? 2: 鈥榓aa鈥榼 # 瀛楀吀--->鍙坊鍔犻敭鍊?br>
set_2.update(z)
print("娣诲姞鍏冪礌鏂规硶浜?", set_2)
# -----------------------------------------------------------------------------------------------------#
# 闆嗗悎鍏冪礌鐨勫垹闄?br># -----------------------------------------------------------------------------------------------------#
# 鏂规硶涓€
set_2.remove(鈥榝aith鈥?
print("鍒犻櫎鍏冪礌鏂规硶涓€:", set_2)
# 鏂规硶浜?br>set_2.discard(鈥?鈥?
print("鍒犻櫎鍏冪礌鏂规硶浜?", set_2)
# 鏂规硶涓?br>set_2.pop()
print("鍒犻櫎鍏冪礌鏂规硶涓?", set_2)
# 鏂规硶涓?br>set_2.clear()
print("鍒犻櫎鍏冪礌鏂规硶鍥?", set_2)
以上是关于鍙橀噺绫诲瀷-Set的主要内容,如果未能解决你的问题,请参考以下文章
妗嗘灦鍋ュ.鎬т紭鍖栵紝鐜鍙橀噺绫伙紝姝e垯鏇挎崲excel
DOM-Text绫诲瀷銆丆omment绫诲瀷銆丆DATASection绫诲瀷銆丏ocumentType绫诲瀷銆丏ocumentFragment绫诲瀷銆丄ttr绫诲瀷