python之set集合

#集合(1.重复的值,循环时自动去重2.打印是无序的3.set元素区分大小写)
jihe=set([1,5,3,9,0,1,67])
for i in jihe:
print(i)
输出:0,1,3,6,7,59

names=['Ellena','Boony','Alisa','boony','Candy']

for i in set(names):
if i.lower() == 'boony':
continue
print(i)
输出:

Ellena
Candy
Alisa


new_names=['a','b','c']
a=set(names)
b=set(new_names)
a.update(b)#集合批量添加
print(a)
for i in a :
print(i)
输出:

Alisa
b
a
c
Ellena
boony
Boony
Candy



#集合,可以用{}创建,但是空集合不能用{}
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.update(y)
print(x)
输出:
{'runoob', 'cherry', 'banana', 'google', 'apple'}
#
x = {"apple", "banana", "cherry"}
y=('4','5','6')


输出:

{'4', 'cherry', '5', 'apple', '6', 'banana'}

#

x = {"apple", "banana", "cherry"}
y=['xx','bb','yy']
输出:
{'cherry', 'xx', 'apple', 'bb', 'banana', 'yy'}
posted @ 2021-12-19 18:00  -夏至未至-  阅读(55)  评论(0编辑  收藏  举报