Python Study Notes (2) List and metadata

Source: Internet
Author: User

Sequence is the most basic data structure in Python. The index of the sequence starts from 0. Python has six built-in sequences, including list, tuples, strings, Unicode strings, buffer objects, and xrange objects.

 

2.1 General sequence operations

All sequence types in python can perform certain operations: Index indexing, sliceing, adding, multiplying, checking whether an element belongs to a Sequence member, and calculating the sequence length, find the maximum and minimum elements.

2.1.1 Index

Access the elements in the sequence by serial number.

2.1.2 multipart

Use the multipart operation to access a certain range of elements.

The step size in the part is usually a hidden parameter. When the last position is 2, the element is extracted with Step 2. If no number is added before and after, an element is displayed as shown in.

2.1.3 Addition

The two sequences of the same type can be added.

2.1.4 Multiplication

2.1.5 membership

Use the in operator. If it is a member, true is returned; otherwise, false is returned.

The following is a simple check for membership

'''Created on 2013-7-7@author: GinSmile'''database = [    ['Gin', 20],    ['John', 21],    ['Xu', 22]]username = raw_input("user name:")age = int(raw_input("age:"))if [username, age] in database:    print "OK"

 

The result is as follows:

2.1.6 length, minimum value, maximum value

 

 

2.2 list 2.2.1 basic operations

Simple assignment and deletion operations

Partition assignment, using colons in []

2.2.2 List Method

Append () adds a new object to the end of the list.

Extend () adds all values in another sequence at one time at the end of the list

 

Coutn () counts the number of elements in the list.

Index () returns the index of the first matching item of an element.

 

Insert () inserts an object into the list.

Pop () removes an element from another table (the last one by default) and returns the value of this element.

 

Remove () removes the first match of a value in the list.

Reverse () reversely stores the elements in the list

 

Simple sorting and Positive Sorting

Method 1: Sort () sorts the list and changes the original list, instead of generating a sorted copy. :

Method 2: The sorted () function returns the sorted sequence without changing the original sequence:

 

Simple sorting and reverse sorting

Use the sort () function to add parameters for reverse sorting

 

Sort By String Length

 

 

2.3 tuples

The tuples cannot be modified !!

The tuple () function uses a sequence as a parameter and converts it into a group.

 

The sharding, indexing, and other operations of tuples are basically the same as those of the list.

 

Tuples are irreplaceable! Because:

  • Tuples can be used as keys in ing, but not lists.
  • Tuples exist as the return values of many built-in functions and methods.

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.