Python BASICS (3) -- list and tuples

Source: Internet
Author: User

Python contains six built-in sequences:

This article mainly discusses the two most common types: List and tuples.

The main difference between a list and a group is that a list can be modified, but a list cannot be modified. In general, the list can replace tuples in almost all cases.

For example, the sequence can represent the information of one person in the database (name, age)

>>> edward=[,42]

The sequence can also contain other sequences.

>>> edward=[,42>>> john=[,50>>> database=>>>, 42], [, 50]]

General sequence operations

All sequential operations can perform certain specific operations. These operations include indexing, partitioning, adding, multiplication, and checking whether an element belongs to a Sequence member.

Index

All the elements in the sequence are numbered-starting from 0. These elements can be accessed by numbers separately, as shown below:

>>> greeting=>>>>>> greeting[-1>>> [1

If a function call returns a sequence, you can index the returned result directly, for example:

>>> fourth=raw_input()[32005>>>
Months = endings = [,] + 17 * [+ [,] + 7 * [+ [= raw_input (= month_name = months [month_number-1 = day + endings [day_number-1 month_name + + ordinal + + yearView Code

>>>19741-12): 81-31): 161974

You can use the sharding operation to access elements within a certain range. The sharding is implemented through two indexes separated by colons:

>>> Tag >>>> tag [>>> tag [32:-4

The first index is the number of the first element to be extracted, and the last index is the number of the first element in the remaining part After partitioning.

>>> Numbers = [1, 3, 4, 5, 6, 7, 8, 9, 10 >>> numbers [3: 64, 5, 6 >>> numbers [0: 11]

1. Elegant shortcuts

Access the last three elements. Of course, you can perform the display operation.

>>> Numbers [, 9, 10 >>> numbers [-3:-18, 9 >>> numbers [-3 >>> numbers [-38, 9, 10]

Only the last part completes the task. This method also applies to the elements starting with the sequence:

>>> Numbers [: 31, 2, 3]

In fact, if you need to copy the entire sequence, you can leave both indexes blank:

>>> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2. Larger step size

There is also a third parameter for fragment-step size, which is usually set implicitly. In general, the step size is 1 and cannot be 0, but can be negative, that is, extract elements from right to left.

>>> Numbers [0:10:11, 2, 3, 4, 5, 6, 7, 8, 9, 10 >>> numbers [0:10:21, 3, 5, 7, 9 >>> numbers [::34> numbers [: 41, 5, 9 >>> numbers [8: 3:-19, 8, 7, 6, 5 >>> numbers [10: 0:-210, 8, 6, 4, 2 >>> numbers [0: 10:-2 >>> numbers [:: -210, 8, 6, 4, 2 >>> numbers [5:-26, 4, 2 >>> numbers [: 5:-210, 8]Test code

Sequence Addition

You can use the plus sign to connect the sequence:

>>> [1,2,3]+[4,5,61, 2, 3, 4, 5, 6>>> +>>> [1,2,3]+

Multiplication

Multiply the number x by a sequence to generate a new sequence. In the new sequence, the original sequence will be repeated x times.

>>> *5>>> [42]*1042, 42, 42, 42, 42, 42, 42, 42, 42, 42]

Member qualifications

To check whether a value is in a sequence, you can use the in operator, which returns a Boolean value.

>>> permissions=>>> >>> >>> subject=>>> 

Length, minimum, and maximum

Built-in functions len, min, max, and len return the number of elements in the sequence. min and max return the largest and smallest elements in the sequence, respectively.

>>> numbers=[100,34,678>>>3>>>678>>>34>>> max(2,33>>> min(9,3,2,52

List Function

The list function can create a list based on strings.

>>> list(, , , , ]

Basic list operations:

1. Change the list: Element assignment

Index tags are used to assign values to specific and location-specific elements:

>>> x=[1,1,1>>> x[1]=2>>>1, 2, 1]

2. Delete Elements

Use the del statement:

>>> names=[,,,,>>>  names[2>>>, , , ]

Note: Cecil is completely deleted, and the list length also changes from 5 to 4.

3. multipart assignment

, >>> Names [2, >>> name = list (>>>,, >>> name [2:] = list (>>>,>>> name [1:] = list (>>>,,>>> numbers = [>>> numbers [] = [2, 3, 4 >>> 1, 2, 3, 4, 5 >>> numbers [] ==>>> 1, 5]View Code

List Method:

A method is a function closely related to some objects. objects may be lists, numbers, strings, or other types of objects. The method of calling a method is as follows:

1. append

The append method is used to append a new object to the end of the list:

>>> lst=[1,2,3>>> lst.append(4>>>1, 2, 3, 4]

2. cout

The count method is used to count the number of times an element appears in the list:

>>> [,,,,,].count(2>>> x=[[1,2],1,1,[2,1,[1,2>>> x.count(12>>> x.count([1,21

3. extend

The extend method can append multiple values in another sequence at a time at the end of the list.

>>> a=[1,2,3>>> b=[4,5,6>>>>>>1, 2, 3, 4, 5, 6>>> >>> a=[1,2,3>>> b=[4,5,6>>> a+1, 2, 3, 4, 5, 6>>>1, 2, 3]

4. index

The index method is used to locate the index location of a matching item from the list:

>>> knights=[,,,,,,>>> knights.index(4>>> knights=[,,,,,,>>> knights.index(

If it is not found, an exception is thrown.

5. insert

The insert method is used to insert objects to the list:

>>> numbers=[1,2,3,5,6,7>>> numbers.insert(3,>>>1, 2, 3, , 5, 6, 7>>> >>> numbers=[1,2,3,5,6,7>>> numbers[3:3]=[>>>1, 2, 3, , 5, 6, 7]

6. pop

The pop method removes an element from the list (the last one by default) and returns the value of this element:

>>> x=[1,2,3>>>3>>>1, 2>>>1>>>2]

7. remove

The remove method is used to remove the first match of a value in the list:

>>> x=[,,,,,>>> x.remove(>>>, , , , >>> x.remove(

8. reverse

The reverse method reversely stores the elements in the list. This method also changes the list but does not return values.

>>> x=[1,2,3>>>>>>3, 2, 1]

9. sort

The sort method is used to sort the list in the original position and change the original list so that the elements in the list are sorted according to a certain number

>>> x=[4,6,2,1,7,9>>>>>>1, 2, 4, 6, 7, 9]

 

 Tuples

Like lists, tuples are also a sequence. The only difference is that tuples cannot be modified.

If some values are separated by commas, A tuples are automatically created:

>>> 1,2,31, 2, 3>>>>>> 4242>>> 4242>>> (4242,)

Tuples are also enclosed by parentheses (most of the time). Empty tuples can be expressed by two parentheses that do not contain content:

Tuple Function

The functions of the tuple function are basically the same as those of the list function: Use a sequence as a parameter and convert it to a tuples.

>>> tuple([1,2,31, 2, 3>>> tuple(, , >>> tuple((1,2,31, 2, 3)

 

 

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.