The Python dictionary has a set method. The following is the relevant introduction to the set method:
The difference between dict and set:
##dict
1. dict is actually a map in Java. dict is a set of key-value pairs (key -value), it can uniquely determine the memory address of the corresponding value based on the key, has extremely fast query and insertion speed, and will not slow down as the data increases 2. The order of key storage is the same as The order of internal storage does not matter 3. It takes up a lot of memory, causing a waste of resources 4. The key value of dict is immutable Variable data Type: list and dictionary dict; Immutable data types: int, floating point float, string string and tuple, an error will be reported when using variable data types as key values. 5. The key value cannot be repeated 6. The order of storage has nothing to do with the order of internal storageRelated recommendations: "python video tutorial"
set
#1. Like dict, it is a collection of keys and cannot be repeated 2. To create a set collection, you need to provide a list as the input collection ,eg.s=set([1,2,3,4]), if there are duplicate values in the list, set will automatically remove the duplicates 3. Variable data types cannot be stored as key values, internal The storage principle is the same as dict, except that there is no value 4. Basic usageInitialization: dict={1:'Bob','name':'Xiao Li'},s=set ([2,4,23,5,32,562,2]) Change the value of dict: dict[1]=100 Add a dict element: dict[test]=None Add a set element: s=set([1,2,3,4]) s.add(None) s.add('test') Remove the value of a dict :dict.pop(key) Remove a set value: s.remove(2)#Remove the specified value. If there is no such value, an error will be reported. You must first use in to determine s.pop()# Remove the first valuemethod
The above is the detailed content of Does Python dictionary have a set method?. For more information, please follow other related articles on the PHP Chinese website!