Five python in the meta-ancestor, list, dictionary differences

Source: Internet
Author: User

There are 3 built-in data structures in Python: Lists, Ganso, and dictionaries:

1. List

A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list.

The items in the list should be included in square brackets so python knows you are specifying a list. Once you have created a list, you can add, delete, or search for items in the list. Since you can add or delete items, we say that the list is a mutable data type, that is, the type can be changed, and the list can be nested.

Instance:

#coding =utf-8

#author: RXS002

Animalslist = [' Fox ', ' Tiger ', ' rabbit ', ' snake ']

Print (' I don't like these ', Len (animalslist), ' animals ... ')

For item in Animalislist:

Print (item)

Print (' \ n after operation ')

#对列表的操作, add, remove, sort

Animalslist.append (' pig ')

Del Animalslist[0]

Animalslist.sort () #sort是排序

For I in range (0,len (animalslist)):

Print (Animallist[i])

Execution Result:

I don't like these 4 animals ...

Fox Tiger Rabbit Snake

After operation

Pig Rabbit Snake Tiger

2. Yuan zu

Ganso and lists are very similar, but Ganso is immutable. That is, you cannot modify the ancestor.

Ganso items are defined by commas separated by parentheses. Ganso usually makes it possible for a statement or user-defined function to safely take a set of worthwhile times, that is, the value of the Ganso being used does not change. Ganso can be nested.

>>>zoo = (' Wolf ', ' Elephant ', ' penguin ')

>>>zoo.count (' Penguin ')

1

>>>zoo.index (' Penguin ')

2

>>>zoo.append (' pig ')

Execution error: Because the meta-ancestor cannot be modified

3. Dictionaries

The dictionary is similar to the Address book where you find the address and contact details by contact name, i.e. we associate the key (first name) with the value (details) . Note that the key must be unique, as if two people happen to have the same name, you cannot find the correct information.

Key-value pairs are tagged in the dictionary in such a way: d={key:value,key2:value2}. Note that their key/value pairs are separated by colons, and each team is delimited by commas, all of which are included in curly braces. Also, remember that the keys/values in the dictionary are not sequential. If you want a particular order, then you should sort them before using them.

Instance:

#coding = UTF-8

#author: rxs002

Dict1 = {' Zhang ': ' hellos ', ' Wang ': ' Wangbaoqiang ', ' li ': ' Bingbing ', ' Zhao ': ' Zhao Wei '}

#字典的操作, add, delete, print

Dict1[' huang '] = ' Huang Jiaju '

Del dict1[' Zhao ']

For Firstname,name in Dict1.item ():

Print Firstname,name

Execution Result:

Li Bingbing

Wang Wangbaoqiang

Huang Huang Jiaju

Zhang Hellos

Five python in the meta-ancestor, list, dictionary differences

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.