I. Python collection set () add Delete, intersect, set, set operation details
In Python set is a collection type of the base data type, which has both a mutable set (set ()) and an immutable set (Frozenset). Creating Set Sets, collection set additions , collection deletions , intersections , sets , and difference sets is a very useful approach.
The 1.python set class is in the Python sets module, and you are now using the python2.7.x that you do not need to import sets modules to create the collection directly.
>>>set (' Boy ')
Set ([' Y ', ' b ', ' O '])
The value of set () cannot be duplicated
2. Collection Add, delete
There are two common methods for adding Python collections, namely add and update.
Collection Add method: The element to be passed in as an entire add to the collection, for example:
>>> a = set (' Boy ')
>>> a.add (' python ')
>>> A
Set ([' Y ', ' python ', ' B ', ' O '])
3. Set Update method: Splits the element to be passed in as an individual into the collection, for example:
>>> a = set (' Boy ')
>>> a.update (' python ')
>>> A
Set ([' B ', ' H ', ' o ', ' n ', ' P ', ' t ', ' Y '])
4. Collection Delete Action method: remove
Set ([' Y ', ' python ', ' B ', ' O '])
>>> a.remove (' python ')
>>> A
Set ([' Y ', ' b ', ' O '])
5.python set () Set operation symbols, mathematical symbols
The intersection of the collection, the collection (and set), and the difference set, to understand the Python collection set and list of these very useful features, you should first understand some of the collection operation symbols
The variable collection python set is the www.iplaypy.com Python learning communication platform, which is more accessible in early Python learning. An iterative pair of images such as lists, dictionaries, and strings can be used as parameters to a collection. Set sets are unordered and cannot be manipulated by indexes and slices.
How does the set collection in Python decide whether to repeat?
Look at the following code, two values of the same item object, added to set is considered to be two objects.
- Class Item (object):
- def __init__ (self, foo, bar):
- Self.foo = foo
- Self.bar = Bar
- def __repr__ (self):
- return "Item (%s,%s)"% (Self.foo, self.bar)
- Print set ([Item ('1 ', ' 2 '), Item (' 1 ', ' 2 ')])
- # Output: Set ([Item (1, 2), item (1, 2)])
The design idea of hash table in data structure is:
- The inserted element is inserted into the bucket (usually an array) according to the hash value.
- The new insert element compares the inside of the bucket element according to the Equals method to determine if there is already
In Python if we customize class, we need to add __hash__ and __eq__ two methods that determine which bucket the current element is in, which determines whether the current element already exists in the bucket.
The modified code is as follows:
- Class Item (object):
- def __init__ (self, foo, bar):
- Self.foo = foo
- Self.bar = Bar
- def __repr__ (self):
- return "Item (%s,%s)"% (Self.foo, self.bar)
- def __eq__ (self, other):
- if Isinstance (Other, Item):
- return ((Self.foo = = Other.foo) and (Self.bar = = Other.bar))
- Else:
- return False
- def __hash__ (self):
- return Hash (Self.foo + "" + Self.bar)
- Print set ([Item ('1 ', ' 2 '), Item (' 1 ', ' 2 ')])
- # Output: Set ([Item (1, 2)])
Python data structure