[Python]有关Set常用命令
my_set = set() #create a new set, 虽然set的显示结果为{item, item1...},但是不能用{}来创建集合,应该{}默认是创建dict print(help(my_set)) #查看帮助 my_set.add(‘jerry‘) #添加元素用add,而非list中的append my_set.add(‘elaine‘) my_set.add(‘george‘) my_set.add(‘kramer‘) my_set.add(‘hawkeye‘) my_set.add(‘henry‘) my_set.add(‘radar‘) my_set.add(‘frank‘) print(my_set) for name in sorted(my_set, reverse=False): print(f‘\t{name}‘) print(len(my_set)) #计算元素个数 popped_item = my_set.pop() #pop规则是什么?哪个元素被删除了, 随机剔除 print(popped_item) print(my_set) my_set.remove(‘hawkeye‘) #删除指定元素 #my_set.insert(0, ‘hot_lips‘) 这是不行的,set没有inset method print(my_set) my_set.clear() #清空全部元素 print(my_set) del my_set #删除my_set my_list = [‘spurn‘, ‘charade‘, ‘spurn‘, ‘rechazar‘, ‘cáscara‘, ‘corteza‘, ‘corteza del limón‘, ‘aseo de casa‘, ‘aseo personal‘] your_set = set(my_list) #用set(list)可以把list转换成集合,好处在于剔除重复元素 remove duplicates print(len(my_list)) print(len(your_set)) print(‘words in my_list include: ‘) for index, item in enumerate(my_list): print(f‘\t{index+1}.{item}‘) #另外还有集合的交集,并集,补集
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16