Python has three built-in data structures: list, ancestor, and Dictionary. This article introduces these three data structures and provides examples, let's take a closer look at the differences between the three.
1. list)
List is the data structure that processes a group of ordered projects. you can store a series project in a list.
The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete a project, we say that the list is a variable data type, that is, this type can be changed and the list can be nested.
Instance:
# Coding = UTF-8 # author: RXS002animalslist = ['fox', 'Tiger ', 'rabbit', 'Snake '] print (' I do not like these ', len (animalslist), 'Animals... ') for item in animalislist: print (item) print (' \ n ') # List operations, add, delete, and sort animalslist. append ('pig') del animalslist [0] animalslist. sort () # sort is sorting for I in range (0, len (animalslist): print (animallist [I])
Execution result:
I do not like these 4 animals... after the fox tiger rabbit snake operation, pig rabbit snake tiger
2. Yuanzu (tuple)
The ancestor is very similar to the list, but the ancestor is immutable. That is, you cannot modify the ancestor.
Project definitions separated by commas (,) in parentheses. In general, when the statements or user-defined functions can safely adopt a set of values, the value of the used ancestor will not change. The ancestor can be nested.
>>>zoo = ('wolf','elephant','penguin')>>>zoo.count('penguin')
1
>>>zoo.index('penguin')
2
>>>zoo.append('pig')
Execution error:Because the ancestor cannot be modified
3. Dictionary)
The dictionary is similar to the address book in which you use the contact name to find the address and contact details.Key (name) and value (details)Together. Note that keys must be unique, as if two people happen to have the same name, you cannot find the correct information.
Key-value pairs are marked in the dictionary as follows:d={key:value,key2:value2}.Note that their key/value pairs are separated by colons, and each team is separated by commas, all of which are included in curly brackets. In addition,Remember that keys/values in the dictionary are unordered. If you want a specific sequence, you should sort them before use.
Instance:
# Coding = UTF-8 # author: rxs002dict1 = {'Zhang ': 'Zhang Jiahui', 'Wang ': 'Wang baoqi', 'Li': 'Li Bingbing ', 'zhao ': 'Wei zhao '} # Dictionary operation, add, delete, print dict1 ['Huang'] = 'Huang Jiaju 'Del dict1 ['zhao'] for firstname, name in dict1.item (): print firstname, name
Execution result:
Li Bingbing wang Baoqiang huang Jiaju zhang Jiahui
Summary
The above is the introduction and difference of the ancestor, list, and dictionary in Python. I hope it will be helpful for you to learn how to use Python.
For more articles about the differences between the ancestor, list, and dictionary in Python, refer to the PHP Chinese website!