A list in Python

Source: Internet
Author: User
Tags python list

List Description:

Excerpt: http://www.w3cschool.cc/python/python-lists.html

The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.

Python has 6 built-in types of sequences, but the most common are lists and tuples.

Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.

Data items for a list do not need to have the same type

Create a list by enclosing separate data items separated by commas in square brackets.

As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.

1. Basic features of the list:

1) The contents of the list data structure can be changed at any time, in fact, each additional list of memory address is different.

2) No fixed length, can be dynamically increased or decreased.

3) The data item in the list can be any data type,

4) More than one list can be a list via the + link. Here's an example:

>>> L1 = [1,2,3,4,5]
>>> L2 = [' A ', ' B ', ' C ']
>>> L3 = L1 + L2
>>> Print L3
[1, 2, 3, 4, 5, ' A ', ' B ', ' C ']

Note: The list can be + and * operations, but * can only be a single list multiplied by an integer, which is an extension to itself.

Part I: Basic operations on a list

1. Accessing the values in the list

Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters

2. Update the list

You can modify or update data items in a list, or you can use the Append () method to add list items

3. Direct substitution of list elements

>>> Print Li
[123, ' TT ', 333, ' DD ']
>>> li[3] = li[3].replace (' dd ', ' UU ') replace is a substitution of characters and cannot be used for numeric types.
>>> Print Li
[123, ' TT ', 333, ' UU ']
>>>

Examples of use of slices:

>>> Print Li
[123, ' TT ', 333, ' UU ']
>>> li2 = li[0:2] using the list of slices, slice format: li_obj[start:end:step]
>>> Print Li2
[123, ' TT ']
>>>

4. Deleting list elements

You can use the DEL statement to delete the elements of a list

Python List script operators

The operands of the list to + and * are similar to strings. + sign for combined list, * number for repeating list

Python Expressions Results Description
Len ([1, 2, 3]) 3 Length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] Combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] Repeat
3 in [1, 2, 3] True Whether the element exists in the list
For x in [1, 2, 3]: print x, 1 2 3 Iteration

Part II: Introduction to Common functions

Some methods for list can be viewed using Help (list)

A few of these are common methods:

1. Index () method Purpose: find a value from a list the index position of the first (note is the first) match

>>> L1 = [' A ', ' B ', ' C ', ' d ', ' e ']
>>> L1
[' A ', ' B ', ' C ', ' d ', ' e ']
>>> l1.index (' C ')
The index position of the 2 character C is 2

2. Insert () the method uses: used to insert an object into the list, with two parameters, the first is the index position, the second inserted element object

>>> L1.insert (1, ' mm ') insert element in position at index 1 mm
>>> L1
[' A ', ' mm ', ' B ', ' C ', ' d ', ' e ']

3. Pop () This method uses: removes the last element in the list and returns the value of the element

>>> L1.pop ()
E
>>> L1
[' A ', ' mm ', ' B ', ' C ', ' d ']

4. Remove () This method acts: Removes the first occurrence of a value in the list: if there are two equal elements, just remove a matching element, if an element does not exist in a list, it will be an error, and can only be
Removes a single element.

>>> L1.insert (2, ' a ')
>>> L1
[' A ', ' mm ', ' A ', ' B ', ' C ', ' d ']
>>> l1.remove (' a ')
>>> L1
[' mm ', ' A ', ' B ', ' C ', ' d '] The result shows that the element with the first element straight to a is deleted.

5. Reverse () The function of this method: Reverse The elements in the list

>>> L1
[' mm ', ' A ', ' B ', ' C ', ' d ']
>>> L1.reverse ()
>>> L1
[' d ', ' C ', ' B ', ' A ', ' mm ']

6. Sort () the function of this method:the Sort method is used to sort the list, modify the original list, and not return a sorted copy of the list

>>> L1
[' d ', ' C ', ' B ', ' A ', ' mm ']
>>> L1.sort ()
>>> L1
[' A ', ' B ', ' C ', ' d ', ' mm '] characters are sorted by ASCII code,

Note:

>>> L2 = [123, ' A ', ' cc ', 22,33,1, ' ZZ ']
>>> L2.sort ()
>>> L2
[1, +, 123, ' A ', ' cc ', ' ZZ ']
>>> L2.insert (L2.index (' zz '), ' AA ')
>>> L2
[1, +, 123, ' A ', ' cc ', ' AA ', ' ZZ ']
>>> L2.sort ()
>>> L2
[1, +, 123, ' A ', ' AA ', ' cc ', ' ZZ ']

7. The role of the Count method: count the number of occurrences of an element in a list

>>> L2
[1, +, 123, ' A ', ' AA ', ' cc ', ' AA ', $, A, ' ZZ ']
>>> l2.count (' zz ')
1
>>> l2.count (' AA ')
2
>>> L2.count (33)
2

8. Extend The function of this method : Append multiple values from another sequence in the original list

>>> L3 = [123,333,444, ' abc ', ' A ', ' baidu.com ']
>>> l3.extend (List (' Gfsoso '))
>>> L3
[123, 333, 444, ' abc ', ' A ', ' baidu.com ', ' g ', ' F ' , ' s ', ' o ', ' s ', ' o '] results show that the method inserts the other list directly into the original list with a single character as the element.

9. Append The method: add elements at the end of the list, add elements at the end of the list

>>> L1
[' A ', ' B ', ' C ', ' d ', ' mm ']
>>> L2
[+, ' cc ', ' com ']
>>> l1.append (L2) Note: Only one element can be added at a time.
>>> L1
[' A ', ' B ', ' C ', ' d ', ' mm ', [+, ' cc ', ' com ']

>>> l1.append (List (' Bai '))
>>> L1
[' A ', ' B ', ' C ', ' d ', ' mm ', [$, ' cc ', ' com '], [' B ', ' A ', ' I ']]

A list in Python

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.