Python: List functions

Source: Internet
Author: User

List function:

Converts a string into a list, for example:

>>> name = list (' hello ')
>>> name
[' H ', ' e ', ' l ', ' l ', ' O ']


List Basic functions:

1. Change list: element assignment

Using index tags

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


2, delete elements

Del Statement implementation

>>>names = [' One ', ' two ', ' three ']
>>>del names[1]
>>>names
[' One ', ' three ']


3. Piecewise assignment (the first parameter is the starting position of the beginning fragment, the second is the next position of the end fragment)

Modify a sequence

>>>name = List (' Perl ')
>>>name[1:] = List (' Ython ')
>>>name
[' P ', ' y ', ', ' t ', ' h ' , ' O ', ' n ']

Insert Sequence

>>>num = [1, 5]
>>>num[1:1] = [2, 3, 4]
>>>num
[1, 2, 3, 4, 5]

Delete a sequence

>>>num = [1, 5]
>>>num[1:1] = [2, 3, 4]
>>>num
[1, 2, 3, 4, 5]

4, append function (change the original list) (can be implemented into the stack operation)

Add a new object at the end of the list

>>>num = [1, 5]
>>>num[1:1] = [2, 3, 4]
>>>num
[1, 2, 3, 4, 5]


5, Count function

Count the number of times an element appears in the list

>>>[' to ', ' being ', ' or ', ' not ', ' to ', ' being '].count (' to ')
2

>>>x = [[1, 2], 1, 1, [2, 1, [1, 2]]]
  >>>x.count (1)
2
>>>x.cout ([1, 2])
1

6, Extend function (modify the original sequence, connect the operation to produce a new sequence)

Append multiple values from another sequence at the end of the list (expand the original list with the new list)

>>>a = [1, 2, 3]
>>>b = [4, 5, 6]
>>>a.extend (b)
>>>a
[1, 2, 3, 4, 5, 6]

Using the fragment operation to achieve the above steps

>>>a = [1, 2, 3]
>>>b = [4, 5, 6]
>>>a[len (a):] = b
>>>a
[1, 2, 3, 4, 5, 6]

7, Index function

Find the index position of the first occurrence of a value from the list

>>>num = [' One ', ' two ', ' three ', ' four ', ' five ']
>>>num.index[' three ']
2

>> >NUM[2]
' three '


8. Insert function

Insert an object into a list

>>> numbers = [1, 2, 3, 5, 6, 7]
>>> Numbers.insert (3, ' four ')
>>> numbers
[1, 2, 3 , ' Four ', 5, 6, 7]

9, Pop function (can realize the stack operation)

Removes an element from the list (the default last element) and returns the value of the element

>>> x = [1, 2, 3]
>>> x.pop ()
3
>>> x
[1, 2]

>>> x.pop (0)
1
>>> x
[2]

10, remove function (and pop difference, modify the original sequence, but do not return)

Removes the first occurrence of a value in a list

>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']
>>> x.remove (' being ')
>>> x
[' to '], ' Or ', ' not ', ' to ', ' is ']

11. Reverse function

Reverse elements in a list

>>> x = [1, 2, 3]
>>> x.reverse ()
>>> x
[3, 2, 1]

12, Sort function (change the original list, but do not return)

Sort the original list

>>> x = [4, 6, 2, 3, 5, 1]
>>> x.sort ()
>>> x
[1, 2, 3, 4, 5, 6]

If you do not need to modify the original sequence (note: if x = y is used here, then X and y all point to the same list, even if the operation is just sorted for y, and X is actually sorted)

Method One: Create a replica y and sort the Y

>>> x = [4, 6, 2, 3, 5, 1]
>>> y = x[:]
>>> y.sort ()
>>> x
[4, 6, 2, 3, 5, 1]
>>> y
[1, 2, 3, 4, 5, 6]
Method Two: Use the sorted function (return a sorted copy, do not change the original sequence)

>>> x = [4, 6, 2, 3, 5, 1]
>>> y = sorted (x)
>>> y
[1, 2, 3, 4, 5, 6]
>& gt;> x
[4, 6, 2, 3, 5, 1]

Keyword ordering: key

Length (len) Sort:

>>> x = [' BB ', ' eeeee ', ' a ', ' dddd ', ' CCC ']
>>> x.sort (key = len)
>>> x
[' A ', ' BB ', ' CCC ', ' dddd ', ' eeeee ']
Keyword ordering: reverse (Simple Boolean sort, true from large to small, false from young to large)

>>> x = [4, 6, 2, 3, 5, 1]
>>> x.sort (reverse = True)
>>> x
[6, 5, 4, 3, 2, 1]
>>> y = [4, 6, 2, 3, 5, 1]
>>> y.sort (reverse = False)
>>> y
[1, 2, 3, 4, 5, 6]


13, CMP (x, y) functions (you can customize the compare (x, y) function to compare two elements)

Compare the size of two functions

>>> CMP
-1
>>> cmp (
1)
>>> cmp (
0).
With the sort function, you can customize sorting, sort (CMP)
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.