Python3之set, frozenset记录
生活随笔
收集整理的這篇文章主要介紹了
Python3之set, frozenset记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
set1 = set([1, 2, 3, 4])
set2 = frozenset([1, 2, 3, 4])
print(set1, set2, sep='|||')
set1.add("five")
set1.update("six")
set1.update({"seven"})
set1.update(["eight", "nine"])
print(set1, set2, sep='|||')
運行結果如下:
{1, 2, 3, 4}|||frozenset({1, 2, 3, 4}) {1, 2, 3, 4, 'x', 'eight', 's', 'i', 'seven', 'nine', 'five'}|||frozenset({1, 2, 3, 4})?
說明:
1、frozenset與普通set的區別在于其內容不可更改(如add, update, remove, pop等)。
在不改變內容的時,frozenset可以與普通set做比較、子集判斷等操作。
?
2、set.add每次只能添加一個元素。
add(elem)
Add element elem to the set.
?
3、set.update每次可以添加多個元素。
注意:如果追加對象是字符串,會將字符串的每個元素分別添加到set中;需要以set或者list形式追加對象。
update(*others)set |= other | ...
Update the set, adding elements from all others.
?
參考:
1、(Python3文檔)https://docs.python.org/3/library/stdtypes.html#set
轉載于:https://www.cnblogs.com/zhangwei22/p/9839425.html
總結
以上是生活随笔為你收集整理的Python3之set, frozenset记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 普及模拟 day4
- 下一篇: 洛谷P2671 求和 [数论]