Usage of the Python list tuple dictionary

Source: Internet
Author: User

First, List

The form of a list is denoted by [], [] There are ', ', ' separate list contents. Lists can also be nested.

Syntax: eg.name1 = [' 1lo ', ' 2ng ', ' 3er ', ' 4long '];

Print (NAME1);

Print: [' 1lo ', ' 2ng ', ' 3er ', ' 4long ']

Insert, add, Delete, change, and find actions on a list.

1. Insert (Pos,val) POS is the position index of the list, Val is the added content

Name1.insert (1, ' 5xxx ');

Print (NAME1);

Print: [' 1lo ', ' 5xxx ', ' 2ng ', ' 3er ', ' 4long ']

2. Add append (val) to add content at the end of the list

Name1.append (' 6xxx ')

Print: [' 1lo ', ' 5xxx ', ' 2ng ', ' 3er ', ' 4long ', ' 6xxxx ']

3. Delete del, Remove,clear,pop

A, Del can specify the deletion of the corresponding position of the element

Del Name1[3];

Print: [' 1lo ', ' 5xxx ', ' 2ng ', ' 4long ', ' 6xxx ']

B. Remove removes the specified element

Name1.remove (' 5xxx ');

Print: [' 1lo ', ' 2ng ', ' 4long ', ' 6xxx ']

C, pop remove the last element

Name1.pop ();

[' 1lo ', ' 2ng ', ' 4long ']

D, clear empty list elements

Name1.clear ();

Print: "Empty"

NAME1 = ['1lo','2ng','3er','4long'];Print(name1); Name1.insert (1,'5xxx');Print(name1); Name1.append ('6xxx');Print(name1);delName1[3];Print(name1); Name1.remove ('5xxx');Print(name1); Name1.pop ();Print(name1); Name1.clear ();Print(NAME1);

4. Change (Modify)

name1[2]= ' 7eee '; #不存在的话, the system will error

Print: [' 1lo ', ' 2ng ', ' 7eee ', ' 4long ']

5. Find

Print (Name1.index (' 7eee '));

Get Subscript: 2

Two, the meta-group

A tuple is a list that cannot be changed. Also called a read-only list.

Only count, and index two operations.

Third, the dictionary

A dictionary of Key-value data types. The same dictionary can also be nested.

Grammar: eg.

Info1 = {
' stu1101 ': ["Tenglan Wu", "35", "155"],
' stu1102 ': ["Longze Luola", "36", "160"],
' stu1104 ': ["Xiaoze Maliya", "30", "165"],
}

Features of the dictionary:

Dict is disordered.

Key must be unique

Print: {' stu1104 ': [' Xiaoze Maliya ', ' ['] ', ' 165 '], ' stu1101 ': [' Tenglan Wu ', ' ' 155 ', ' stu1102 ']: [' Longze Luola ', ' 36 ', ' 160 ']}

At the same time the dictionary can implement multilevel nesting.

Usage of the Python list tuple dictionary

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.