One, the meta-group
Tuples (tuple) are like lists, with () wrapping elements, which are immutable types.
Tuples can be placed within the various data, can not be increased, minus, change operations, only to view and delete. Here is the immutable type, a total of three kinds: numbers, strings, tuples. Where numbers and strings are scalar types, and tuples are a container, which can be placed into many types of elements.
But tuples can be expanded, using +
Atuple = ("One", "one", "one", "three") Atuple = Atuple + ("Four",) print Atuple
If your current operation data is dynamic, in the future will be frequently modified operations, including increase, subtract, delete, change and other basic operations, you should select the list.
If you are maintaining some sensitive data and need to pass that data on to a function that you do not understand, or sometimes you are just maintaining part of the software development, and you do not know how the sensitive data will be used by others in the future, setting the data immutable will make the content much safer.
At the same time, lists and tuples can be converted to each other:
The list () function is used to generate an empty list, or to convert other data to a list, including tuples.
The tuple () function is used to generate an empty tuple, or to convert other data into tuples, including lists.
Dictionary dictionaries, often marked with dict in English (dictionary abbreviations), are sometimes referred to as maps.
It is a container for storing elements, like lists and tuples. Of course, it also has a different point. The dictionary does not use the default natural number to index values, but instead uses other variables to explicitly mark them. We can interpret it as a one-to-one mapping relationship. In the dictionary structure of Python, each map is determined by a key (key) and the value that points to it (value).
At the same time, the dictionary is also a container, which can store any number, any type of data. The dictionary uses curly braces to wrap elements, elements and elements separated by a comma ",", and each element is made up of a "key-value" pair, with a colon ":" between the key and the value to connect.
Players = { "Lionel Messi": "Barcelona", "Cristiano Ronaldo": "Real Madrid", "Kak": "AC Milan", " Beckham ": Manchester", "Mickael Jordan": "Chicago Bulls", "Kobe Bryant": "Los Angeles Lakers"}
Dictionaries are widely used as the only hash type in Python. A hash table is a data structure that is stored in each of the hash tables, such as 1XX 1234 5678, according to its key (sin). The operation of the hash table is this: according to your key, perform a operation called a hash function, calculate a result, the result is the value of the address stored. That is, the address at which a value is stored depends on its key. The results of this calculation are arbitrary, so the storage structure of the hash table is also very "random", not continuous. The use of keys to find values is very, very fast.
Basic operation of the dictionary
You can find the value by key, but the value-finding key requires a circular judgment.
Players = { "Lionel Messi": "Barcelona", "Cristiano Ronaldo": "Real Madrid", "Kak": "AC Milan", " Beckham ": Manchester", "Mickael Jordan": "Chicago Bulls", "Kobe Bryant": "Los Angeles Lakers"}adict = Dict () adict = {"Name": "Me", "Location": "earth"}names = Players.keys () teams = Players.values () print Namesprint Teamsprint players["Lionel Messi"]def Searchplayer (Team): for name in players: if players[name] = = Team: Print Namesearchplayer ("Real Madrid")
Modify
Adict = {"Name": "Me", "Location": "Earth"}adict["name"] = "Chong" Print adict["name"]print adictdel adict["name"] # Delete an element adict.clear () #删除字典内容, leaving an empty dictionary del adict #删除字典
Dict ()
This function is used to create a dictionary, and if there are no arguments in parentheses, an empty dictionary is generated. But if the parentheses have parameters, this is a very interesting situation. The Dict function takes a sequence, the parameters in the sequence must be two parts, the first part is the key in the future dictionary element, and the second part is the value in the future dictionary element. Like what:
Dict1 = Dict ([(1, 2), ("X", "Y")])
Print Dict
In fact, the content in parentheses is the sequence that can iterate, the sequence in the example above is a tuple, we can change to a list, the effect is the same.
Len ()
The Len function returns the number of "key-value" pairs in the dictionary.
Keys ()
The key function extracts all the keys into a list.
VALUES ()
The values function extracts all of the value into a list.
Update ()
The update function is used to add content from one dictionary to another, and if a key in the dictionary conflicts with a key in the newly added dictionary, the value of the new key will overwrite (replace) the old value.
SetDefault ()
The SetDefault function is a frequently used function with two parameters representing a "key-value" pair. The function completes a search first, if the key is present, returns the original value, the new value is not updated, and if the key does not exist, a new key-value pair is inserted.
Python Learning Notes (iii)