Python集合的經常使用操做

字典經常使用的就是,他的去重。

set集合是python的一個基本數據類型.
set中的元素是不重複的.⽆無序的.⾥面的元素必須是可hash的(int, str, tuple,bool)。
咱們能夠這樣來記. set就是dict類型的數據可是不保存value, 只保存key. set也⽤{}表⽰
注意:
	set中的元素是不重複的, 且無序的.
	使⽤用這個特性.咱們能夠使⽤用set來去掉重複
	set集合中的元素必須是可hash的, 可是set自己是不可hash得. set是可變的。

set集合增刪改查

1.增長
    def add(self, *args, **kwargs): # real signature unknown
        """
        Add an element to a set.
        
        This has no effect if the element is already present.
        """
        pass

        用法:添加一個元素到集合。重複的內容不會被添加到set集合。

        例子:
        	s = {"劉大哥", '關大哥', "張大哥"}
			s.add("趙子龍")
			print(s)
			s.add("趙子龍") # 重複的內容不不會被添加到set集合中 
			print(s)


    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. """
        pass

        用法:迭代更新

        例子:
        	s = {"劉大哥", '關大哥', "張大哥"} 
        	s.update("趙子龍") # 迭代更更新 
        	print(s)
			s.update(["阿斗", "臥龍","鳳雛"]) 
			print(s)

2.刪除
    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty.
        """
        pass

    	用法:隨機刪除一個元素,若是集合爲空,會報錯。

    	例子:

	    	s = {"劉大哥", '關大哥', "王大哥","張哥哥"} 
	    	item = s.pop() # 隨機彈出⼀一個.
			print(s)
			print(item)

    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set; it must be a member.
        
        If the element is not a member, raise a KeyError.
        """
        pass

    	用法:刪除集合中指定成員,若是成員不存在會報錯。

    	例子:
    		s = {"劉大哥", '關大哥', "王大哥","張哥哥"}
    		s.remove("關大哥") # 直接刪除元素
			# s.remove("⻢馬") # 不不存在這個元素. 刪除會報錯 
			print(s)

        def discard(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set if it is a member.
        
        If the element is not a member, do nothing.
        """
        pass

    	用法:刪除集合中指定成員,若是該集合不存在所刪除成員,不作任何操做,也不會報錯。

    	例子:
    		s = {1, 3, 4, 5}
			print(s)#{1, 3, 4, 5}
			s.discard(3)
			print(s)#{1, 4, 5}
			s.discard(66)
			print(s)#{1, 4, 5}

        def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. """
        pass

    	clear()  清空set集合。
    	注意:set集合若是是空的。打印出來的是set()。須要和dict區分。
    	例子:
    		s = {1, 3, 4, 5}
			s.clear()
			print(s)。#set()

3.修改
	set 集合中的數據沒有索引,也沒有辦法定位一個元素。因此沒有辦法直接修改。
	咱們能夠採用:
		1)先刪除後添加的方式
		2)將set轉爲list刪除元素後將list轉爲set

4.查詢
	set是一個可迭代對象,因此能夠進行for循環
		for x in s:
			print(x)

5.經常使用操做

	1.求差集(-)
	    def difference(self, *args, **kwargs): # real signature unknown
        """
        Return the difference of two or more sets as a new set.
        
        (i.e. all elements that are in this set but not the others.)
        """
        pass

        例子:
        	s1 = {1, 3, 5, 7, 9}
			s2 = {1, 3, 6, 8, 10}
			print(s1-s2)#{9, 5, 7}
			print(s1.difference(s2))#{9, 5, 7}
			print(s2-s1)#{8, 10, 6}
			print(s2.difference(s1))#{8, 10, 6}


    2.求交集(&)
        def intersection(self, *args, **kwargs): # real signature unknown
        """
        Return the intersection of two sets as a new set.
        
        (i.e. all elements that are in both sets.)
        """
        pass

        例子:
        	s1 = {1, 3, 5, 7, 9}
			s2 = {1, 3, 6, 8, 10}
			print(s1&s2)#{1, 3}
			print(s1.intersection(s2))
			print(s2&s1)#{1, 3}
			print(s2.intersection(s1))


    3.求對稱差集(^)
        def symmetric_difference(self, *args, **kwargs): # real signature unknown
        """
        Return the symmetric difference of two sets as a new set.
        
        (i.e. all elements that are in exactly one of the sets.)
        """
        pass

        例子:
        	s1 = {1, 3, 5, 7, 9}
			s2 = {1, 3, 6, 8, 10}
			print(s1 ^ s2)#{5, 6, 7, 8, 9, 10}
			print(s1.symmetric_difference(s2))#{5, 6, 7, 8, 9, 10}
			print(s2 ^ s1)#{5, 6, 7, 8, 9, 10}
			print(s2.symmetric_difference(s1))#{5, 6, 7, 8, 9, 10}


    4.求並集(|)
        def union(self, *args, **kwargs): # real signature unknown
        """
        Return the union of sets as a new set.
        
        (i.e. all elements that are in either set.)
        """
        pass

        例子:
        	s1 = {1, 3, 5, 7, 9}
			s2 = {1, 3, 6, 8, 10}
			print(s1 | s2)#{1, 3, 5, 6, 7, 8, 9, 10}
			print(s1.union(s2))#{1, 3, 5, 6, 7, 8, 9, 10}
			print(s2 | s1)#{1, 3, 5, 6, 7, 8, 9, 10}
			print(s2.union(s1))#{1, 3, 5, 6, 7, 8, 9, 10}

    5.子集
         def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set. """
        pass

        例子:
        	s1 = {"1", "2"}
			s2 = {"1", "2", "3"}
			# ⼦子集
			print(s1 < s2) # set1是set2的⼦子集嗎? True 
			print(s1.issubset(s2))

    6.超集
    	def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. """
        pass

        例子:
	        s1 = {"1", "2"}
			s2 = {"1", "2", "3"}
	        print(s1 > s2) # set1是set2的超集嗎? False 
	        print(s1.issuperset(s2))
相關文章
相關標籤/搜索