027集合:在我的世界里,你就是唯一

>>> num = {1,2,3}
>>> type(num)
<class 'set'>
>>> num2 = {1,2,3,4,5,5,4,2,1}
>>> num2
{1, 2, 3, 4, 5}

set()

>>> set1 = set([1,2,3])
>>> set1
{1, 2, 3}
>>> num1 = [1,2,3,4,5,5,3,1,0]#去掉重复值
>>> temp = []
>>> for each in num1:
	if each not in temp:
		temp.append(each)

		
>>> temp
[1, 2, 3, 4, 5, 0]#这个是有序的
>>> num1 = list(set(num1))
>>> num1
[0, 1, 2, 3, 4, 5]#使用set是无序的
>>> num2
{1, 2, 3, 4, 5}
>>> 1 in num2
True
>>> "1" in num2
False
>>> num2.add(6)
>>> num2
{1, 2, 3, 4, 5, 6}
>>> num2.remove(5)
>>> num2
{1, 2, 3, 4, 6}

frozen.set()

>>> num3 = frozenset([1,2,3,4,5])
>>> num3.add(6)
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    num3.add(6)
AttributeError: 'frozenset' object has no attribute 'add'
发布了42 篇原创文章 · 获赞 0 · 访问量 284

猜你喜欢

转载自blog.csdn.net/qq_43169516/article/details/103805030