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-8animalslist = ['fox', 'tiger ', 'rabbit', 'snake '] print "I don't like these", len (animalslist ), 'animals... 'for items in animalslist: print items, print "\ n operation" # list operations, add, delete, and sort 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-8dict1 = {'zhang ': 'zhang Jiahui', 'wang ': 'wang Baoqiang', 'lil': 'Li Bingbing ', 'zhao ': 'zhao wei'} # dictionary operation, add, delete, and print dict1 ['huang '] = 'huang jiaju 'del dict1 ['zhao'] for firstname, name in dict1.items (): print firstname, name

Result:

Li Bingbing wang Baoqiang huang jiaju zhang Jiahui

The above section describes the differences between the Python group, list, and dictionary. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner, and I would like to thank you for your support for the help House website!

Related Article

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.