码迷,mamicode.com
首页 > 编程语言 > 详细

Python3之set, frozenset记录

时间:2018-10-23 23:09:12      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:rom   list   一个   https   seve   结果   move   pytho   highlight   

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

Python3之set, frozenset记录

标签:rom   list   一个   https   seve   结果   move   pytho   highlight   

原文地址:https://www.cnblogs.com/zhangwei22/p/9839425.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!