集合: 1,将元素用花括号括起来,没有体现出映射关系 2,无序 3,元素唯一性 4,不支持索引,num1[2],将报错 5,创建集合,set1 = set([1,1,1,2,2,3,43,4]) 6,添加元素,set1.add(6) 7, 删除元素,set1.remove(6) 8,不可变集合,num3 = frozen([1,2,3,4,5]) //num3.add(3)报错 >>> num1 = {1,2,3,4,5,6} >>> type(num1) <class 'set'> >>> 唯一性: >>> num1 = {1,1,1,2,2,2,3,3,3,4,4,4} >>> num1 {1, 2, 3, 4} >>> 将列表照片中的重复值去掉 >>> set1 = [1,1,1,2,2,2,3,3,4,4,5,5,6,6,7] >>> temp = [] >>> for each in set1: if each not in temp: temp.append(each) >>> temp [1, 2, 3, 4, 5, 6, 7] >>> 用集合的方法为: set1 = list(set(set1))//缺点,得到的列表序列改变
转载请注明原文地址: https://www.6miu.com/read-49772.html