Home > Article > Backend Development > Introduction to dictionary operation methods in python (code example)
This article brings you an introduction to dictionary operation methods in Python (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Dictionary, the name is called dictionary, translated as dictionary, similar to the previous int/str/list, the name of this type of data is: dict
Experiment:
>>>help(dict)
Using dir, you can get the same result.
>>> dir(dict) ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
valuesHow to use this built-in function
>>> help(dict.values)
Help on method_descriptor: values(...) D.values() -> list of D's values (END)
q key returns.
dict in python has the following characteristics:
dict is variable
dict can store any number of Python objects
dict can store any python data type
dict stores data in the form of: key:value, that is, "key:value" pairs, and each key is unique.
dict is also called an associative array or hash table.
If you don’t understand the above items very well, it doesn’t matter. You can understand them through the following studies, especially through various experiments.
Method 1:
Create an empty dict. This empty dict can be added to it later. Add something to use.
>>> mydict = {} >>> mydict {}
Create a dict with content.
>>> person = {"name":"hiekay","site":"hiekay.github.io","language":"python"} >>> person {'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'}
"name": "hiekay" is a key-value pair. The first name is called the key (key), and the following hiekay is the value (value) corresponding to the previous key. In a dict, keys are unique and cannot be repeated; values correspond to keys and values can be repeated. Use an English semicolon (:) between key values, and use an English comma (,) to separate each pair of key values.
>>> person['name2']="hiekay" #这是一种向dict中增加键值对的方法 >>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'}
The following demonstrates the process of adding content starting from an empty dict:
>>> mydict = {} >>> mydict {} >>> mydict["site"] = "hiekay.github.io" >>> mydict[1] = 80 >>> mydict[2] = "python" >>> mydict["name"] = ["zhangsan","lisi","wangwu"] >>> mydict {1: 80, 2: 'python', 'site': 'hiekay.github.io', 'name': ['zhangsan', 'lisi', 'wangwu']} >>> mydict[1] = 90 #如果这样,则是修改这个键的值 >>> mydict {1: 90, 2: 'python', 'site': 'hiekay.github.io', 'name': ['zhangsan', 'lisi', 'wangwu']}
Method 2: Tuple
>>> name = (["first","Google"],["second","Yahoo"]) #这是另外一种数据类型,称之为元组,后面会讲到 >>> website = dict(name) >>> website {'second': 'Yahoo', 'first': 'Google'}
Method 3: The Python dictionary fromkeys() function is used to create a new dictionary, using the elements in the sequence seq as the keys of the dictionary, and value is the initial value corresponding to all keys in the dictionary.
>>> website = {}.fromkeys(("third","forth"),"facebook") >>> website {'forth': 'facebook', 'third': 'facebook'}
It should be reminded that this method is to re-create a dict.
Because dict stores data in the form of key-value pairs, as long as you know the key, you can get the value. This is essentially a mapping relationship.
>>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'} >>> person['name'] 'hiekay' >>> person['language'] 'python' >>> site = person['site'] >>> print site hiekay.github.io
You can use the for statement
>>> person {'name2': 'hiekay', 'name': 'hiekay', 'language': 'python', 'site': 'hiekay.github.io'} >>> for key in person: ... print person[key] ... hiekay hiekay python hiekay.github.io
The above is the detailed content of Introduction to dictionary operation methods in python (code example). For more information, please follow other related articles on the PHP Chinese website!