A collection of Python learning Notes (vi) 1
The various types and methods in Python can be found using the following method:
(1) using Dir () or help () in interactive mode
(2) Google
Collection
Features: English set, some variable, some immutable; the elements are not sequential and cannot be duplicated.
The collection is not indexed (it can be viewed using Dir (set)), nor is it in sequence.
The elements in the collection are hashable (immutable) Types!
Create Collection--string
Example 1:
>> S1 = set ("WTF")
>> S1
Set ([' t ', ' W ', ' F '])
>> type (S1)
<type ' Set ' >
The character of a string is disassembled to form a set.
Example 2:
>> s2 = set ("Wttf")
>> S2
Set ([' t ', ' W ', ' F '])
>> type (s2)
<type ' Set ' >
Note: There are two "T" in "Wttf", but there is only one "T" in the collection, which also shows that the elements in the collection cannot be duplicated.
Create Collection--list
Example 3:
>> s3 = Set ([123, "WTF", "book", "WTF"])
>> S3
Set ([123, ' book ', ' WTF ')
Description: When creating a collection, if there are duplicate elements in the list, it will be filtered out, leaving no duplicates.
Add
>> Help (Set.add)
Add (...)
Add an element to a set.
This have no effect if the element is already present.
Example 4:
>> A_set = {}
>> a_set.add ("WTF")
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Dict ' object has no attribute ' add '
>> type (a_set)
<type ' Dict ' >
Description: {} This thing is used in dictionaries and collections. However, as the above method establishes a dictionary, it is not a collection. This is the Python rule.
Not with (), as follows:
Example 5:
>> A_set = ()
>> a_set.add ("WTF")
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Tuple ' object has no attribute ' add '
>> type (a_set)
<type ' tuple ' >
Description: The computer considers the tuple to be established.
To create an empty collection, you have to use Set ()
Example 6:
>> s = set ()
>> type (s)
<type ' Set ' >
To create a non-empty collection, the following:
Example 7:
>> A_set = {"A", "I"}
>> type (a_set)
<type ' Set ' >
>> Print A_set
Set ([' I ', ' a '])
Or:
>> A_set = Set (["A", "I"])
>> type (a_set)
<type ' Set ' >
>> Print A_set
Set ([' A ', ' I '])
add element:
Example 8:
>> a_set.add ("WTF")
>> A_set
Set ([' I ', ' a ', ' WTF '])
Update
Feature: Merge elements from another collection.
>> Help (Set.update)
Update (...)
Update a set with the Union of itself and others.
Example 9:
>> S1
Set ([' t ', ' W ', ' F '])
>> s2 = set (["Python", "Fei"])
>> S2
Set ([' Python ', ' Fei '])
>> s1.update (S2)
>> S1
Set ([' Python ', ' Fei ', ' t ', ' W ', ' F '])
>> S2
Set ([' Python ', ' Fei '])
Pop
>> Help (Set.pop)
Pop (...)
Remove and return an arbitrary set element.
Raises keyerror if the set is empty.
Example 10:
>> B_set = {"[X-y]", "H", "O", "n", "P", "T", "WTF", "Y"}
>> B_set.pop ()
' [A.] '
>> B_set.pop ()
' WTF '
>> B_set.pop ()
' H '
>> B_set
Set ([' O ', ' n ', ' P ', ' t ', ' wtf ', ' y '])
Description: Removes any one of the selected from set and returns the value.
Set is not able to specify the deletion of an element:
Example 11:
>> B_set.pop ("n")
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:pop () takes no arguments (1 given)
Remove
>> Help (Set.remove)
Remove (...)
Remove an element from a set; It must be a member.
If the element is not a member, raise a KeyError.
Note: obj in Set.remove (obj) must be an element in set, otherwise it will be an error, experiment as follows:
Example 12:
>> B_set
Set ([' O ', ' n ', ' P ', ' t ', ' wtf ', ' y '])
>> b_set.remove ("P")
>> B_set
Set ([' O ', ' n ', ' t ', ' wtf ', ' y '])
>> b_set.remove ("Didi")
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' Didi '
Description: Explicitly tells the collection that there is no "WTF".
Discard (obj)
>> Help (Set.discard)
Discard (...)
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
Description: Discard is similar to remove, but is different, experiment as follows:
Example 13:
>> B_set
Set ([' O ', ' n ', ' t ', ' Y '])
>> B_set.discard ("n")
>> B_set
Set ([' O ', ' t ', ' Y '])
>> b_set.discard ("WTF")
>> B_set
Set ([' O ', ' t ', ' Y '])
Note: obj in discard (obj), if it is an element in the collection, is deleted;
Make a comparison between the two:
Example 14:
>> B_set
Set ([' O ', ' t ', ' Y '])
>> B_set.discard ("W")
>> B_set
Set ([' O ', ' t ', ' Y '])
>> B_set.remove ("W")
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' W '
Clear
>> Help (Set.clear)
Clear (...)
Remove all elements from the this set.
Example 15:
>> B_set
Set ([' O ', ' t ', ' Y '])
>> B_set.clear ()
>> B_set
Set ([])
>> BOOL (B_set)
False
A collection of Python learning Notes (vi) 1