In Python, metadata groups, lists, Dictionary differences, and python dictionaries
Python has three built-in data structures: List, tuples, and dictionary.
1. List
List is the data structure that processes a group of ordered projects. You can store a series project in a list. Project in the 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 projects, 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
Animalslist = ['fox', 'tiger ', 'rabbit', 'snake ']
Print "I don't like these", len (animalslist), 'animals ...'
For items in animalslist:
Print items,
Print "\ n operation"
# List operations, adding, deleting, and sorting
Animalslist. append ('pig ')
Del animalslist [0]
Animalslist. sort ()
For I in range (0, len (animalslist )):
Print animalslist [I],
Result:
I don't like these 4 animals...
Fox tiger rabbit snake
After the operation
Pig rabbit snake tiger
2. tuples
The ancestor is very similar to the list, but the tuples are immutable. That is, you cannot modify the tuples. The tuples are defined by commas (,) in parentheses. Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change. Tuples can be nested.
>>> Zoo = ('Wolf ', 'elephant', 'penguin ')
>>> Zoo. count ('penguin ')
1
>>> Zoo. index ('penguin ')
2
>>> Zoo. append ('pig ')
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>> Del zoo [0]
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object doesn' t support item deletion
3. Dictionary
The dictionary is similar to the address book in which you use the contact name to find the address and contact details. That is, we associate the 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 in this way: d = {key1: value1, key2: value2 }. Note that their key/value pairs are separated by colons, and each pair is separated by commas, all of which are included in curly brackets. Remember that the key/value pairs in the dictionary have no order. If you want a specific sequence, you should sort them before use.
Instance:
# Coding = UTF-8
Dict1 = {'zhang ': 'zhang Jiahui', 'wang ': 'wang baoqi', 'lil': 'Li Bingbing', 'zhao': 'zhao wei '}
# Dictionary operations, add, delete, and print
Dict1 ['huang '] = 'huang jiaju'
Del dict1 ['zhao']
For firstname, name in dict1.items ():
Print firstname, name
Result:
Li Bingbing
Wang wangbaoqiang
Huang jiaju
Zhang Jiahui