1, when two lists or dictionaries have cross and repeat, need to count the number of people,
The collection will be a great help to the heavy
The collection can also split the list or collection.
2, gather
The more important role of 3.set is "relationship testing"
Set two major features:
Go weight: Turn a list into a set and automatically go heavy
Relationship testing: Test the intersection, difference set, and set of two sets of data
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author:summer_han
"' list_1 = [1,4,3,5,6,7,9,7,4,10]
S1 = set (List_1) #set turns the list into a collection and sorts the weight away.
List_2 = [2,15,8,7,22,4,70,5,10]
S2 = Set (list_2)
List_3 = [3,5,10]
S3 = Set (List_3)
#print (S1,S2)
#交集
S_inter = s1.intersection (s2) #求 s1,s2 Intersection
Print (S_inter)
#并集
s_union = s1.union (s2) #合并去重
Print (s_union)
#差集 S1, there's nothing inside S2.
S_diff = s1.difference (s2)
#差集 S2, there's nothing inside S1.
S_DIFF2 = s2.difference (S1)
Print (S_DIFF2)
#子集 who contains who
Print (S1.issubset (s2)) #不被包含为 Description 1 is not a subset of 2, False
Print (S3.issubset (S1)) # S2 is S1 included, 3 is a subset of 1, true
Print (S3.issubset (S2)) #s3 is not a subset of S2, want to see the reason below, see the difference set found in S3 there is a 3 in the S2 does not exist
Print (s3.difference (S2))
#父集 who is included
Print (S1.issuperset (S3)) #s1是s3的父集 True
Print (S2.issuperset (S3)) #s2是s3的父集 False
#对称差集
Print (s1.symmetric_difference (S2)) #s1, S2 does not exist inside.
#
List_3 = [3,5,10]
S3 = Set (List_3)
List_4 = [4,6,9]
S4 = Set (List_4)
#无交集
Print (S3.isdisjoint (S4)) # Judgment no intersection
#交集
Print (List_1 & list_2)
#并集
Print (list_1 | list_2)
#差集
List_1 = [1,4,3,5,6,7,9,7,4,10]
S1 = set (List_1) #set turns the list into a collection and sorts the weight away.
List_2 = [2,15,8,7,22,4,70,5,10]
S2 = Set (list_2)
Print (S1)
Print (s2)
Print (S1-S2) # in List 1 and not in List 2
#对称差集
Print (s1 ^ s2)
‘‘‘
#对集合增删改查
List_1 = [1,4,3,5,6,7,9,7,4,10]
List_1 = Set (List_1) #set turns the list into a collection and sorts the weight away.
List_2 = [2,15,8,7,22,4,70,5,10]
List_2 = Set (list_2)
List_1.add (#添加一项)
Print (list_1)
List_1.update ([1,100,150, ' X ']) #注意用 []
Print (list_1)
#remove Delete an item you can delete only one item
List_1.remove (' x ')
Print (list_1)
#列表 Dictionary collection characters are determined using the following methods
#x in a # Tests if X is a member of a
#x not in a # to determine if X is not a member of a
#len (judging length)
#copy复制
List_2 = List_1.copy ()
Print (list_2)
#print (S2)
#pop Delete
#print (List_1.pop ()) #随机删除一个 and returns the Delete element
Print (List_1.discard (' ddd ')) # Delete a member If there is a set, if there is no do nothing # # # Remove does not exist will error, discard will not error
Python collection gather