In Python, metadata groups, lists, Dictionary differences, and python dictionaries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.