Tag: Boot error: Do not example type 3.2 pre procedure
Pickle Storing Data
Save and extract results from Python operations
First Import pickle Module
Define a dictionary:
a_dict={' da ': 111,2:[23,1,4], '% ': {1:2, ' d ': ' Sad '}}
First open a file, the suffix name is replaced by Pickle, open in binary form
Then use dump, put the a_dict into file, and close the file
Pickle.dump (A_dict,file) file.close ()
Then read the files we stored
First open the file, open the way for ' RB ', use pickle load to download the content, and finally close the file
File=open (' Pickle_example.pickle ', ' RB ') a_dict1=pickle.load (file) file.close () print (A_DICT1)
The results of the operation are as follows:
The process can be simplified by using the WITH statement without considering closing the file and automatically closing the file as soon as it finishes running
With open (' Pickle_example.pickle ', ' RB ') as file: a_dict1=pickle.load (file) print (A_DICT1)
Writes can also be simplified with the WITH statement
set to find different
Use set to remove duplicate elements from an object
Char_list=[' A ', ' B ', ' C ', ' C ', ' d ', ' d ', ' d ']print (set (char_list))
The results of the operation are as follows:
A dictionary-like content is returned, but not a dictionary, the dictionary has a key and value, but the content is only value
Use type to output types:
Print (Type (set (char_list))) print (Type ({1:2}))
The results are as follows:
Define a sentence, set can also remove duplicate content:
Sentence= ' Welcome back to this Tutorial ' print (set (sentence))
The results of the operation are as follows:
Set case-sensitive, space
Can you directly compare the differences between char_list and sentence?
Print (Set ([Sentence,char_list]))
Error: Typeerror:unhashable type: ' list ' is not passed to the list in set
You can add content through add and not repeat if the content already exists in set
Unique_char=set (char_list) unique_char.add (' x ') print (Unique_char)
The results of the operation are as follows, adding ' X '
The same add cannot pass in the list and requires a single add
You can also clear the content with clear
Unique_char.clear ()
The result of the operation is:
You can remove a content with remove and return a value of None
Print (Unique_char.remove (' x ')) print (Unique_char)
The result of the operation is:
If the content of remove is not in the object, then the operation will be error, in order to avoid this situation, you can use discard, run will not error, return none
Using difference and intersection to find the different and identical contents of two sequences, the code looks like this:
Set1=unique_charset2={' A ', ' e ', ' I '}print (set1) print (Set2) print (Set1.difference (Set2)) Print (Set1.intersection ( Set2))
The results of the operation are as follows:
Python Beginner--pickle & Set