Python Learning Notes (1)-List

Source: Internet
Author: User

What is a list?

A list consists of a series of elements arranged in a particular order. The list looks like this: [1,2,3,4,1]. In a list, you can consist of 0 or more elements, separated by commas, with the same value element allowing multiple occurrences

Create a list using [] or list ()

For example, create an empty list

Empty_list = [] or empty_list = List ()

Use list () to convert other data types into lists

List of strings:

>>> list (' Cat ')

[' C ', ' a ', ' t ']

Tuple list:

>>> num = (all in all)

>>> list (num)

[1, 2, 3]

Get an element using [offset]

Starting from 0, you can also use a negative number and a tail of-1

>>> a = [2,3,4,5]

>>> A[0]

2

>>> A[-1]

5

Use the position of the index list element

>>> A

[2, 3, 4, ' A ', ' B ', ' C ']

>>> A.index (' a ')

3

A indexerror error occurs when the specified offset exceeds the list range.

Using slices to get a sub-list

[First:end: (STEP)]

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

>>> A[::1]

[1, 2, 3, 4, 5, 6]

>>> A[::2]

[1, 3, 5]

>>> A[::-2]

[6, 4, 2]

modifying elements using [offset]

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

>>> a[0]=10

>>> A

[10, 2, 3, 4, 5, 6]

Add an element at the end of the list (append)

>>> a = [+ +]

>>> A.append (4)

>>> A

[1, 2, 3, 4]

Add an element at the specified position in the list (insert)

>>> A

[1, 2, 3, 4]

>>> A.insert (1, ' happy ')

>>> A

[1, ' Happy ', 2, 3, 4]

One list merged into another list (extend)

>>> a = [1,2,3,4]

>>> B = [' A ', ' B ', ' C ']

>>> A.extend (b)

>>> A

[1, 2, 3, 4, ' A ', ' B ', ' C ']

Delete a location element

Delete any location element using the DEL statement

>>> A

[1, 2, 3, 4, ' A ', ' B ', ' C ']

>>> del A[0]

>>> A

[2, 3, 4, ' A ', ' B ', ' C ']

Using method pop to delete an element

The list is like a stack, and deleting the element at the end of the list is equivalent to ejecting the top element

>>> A

[2, 3, 4, ' A ', ' B ', ' C ']

>>> A.pop ()

C

>>> A

[2, 3, 4, ' A ', ' B ']

>>> A.pop (2)

4

>>> A

[2, 3, ' A ', ' B ']

Remove an element by value (remove)

>>> A

[2, 3, ' A ', ' B ']

>>> A.remove (2)

>>> A

[3, ' A ', ' B ']

Working with lists

List for saving variables

>>> [1,2,3,4,5]

List for looping

>>> a = [0,1,2,3,4]

>>> for I in a: #等价于for I in rang (5)

Print (i)

0

1

2

3

4

In and not in operators

You can determine whether a value is in the list, return a Boolean value

>>> 1 in [0,1,2,3,4]

True

>>> 5 Not in [0,1,2,3,4]

True

>>> 1 not in [0,1,2,3,4]

False

>>> 5 in [0,1,2,3,4]

False

Multiple assignment techniques

>>> One,two,three = [+]

>>> One

1

>>>

2

>>> Three

3

The number and list of variables must be strictly equal, otherwise python will ValueError

Common methods

Get the list length using Len ()

>>> A

[0, 1, 2, 3, 4]

>>> Len (a)

5

Use COUNT () to record the number of occurrences of a specific value

>>> a = [1,2,3,4,3,5,1,1,3,1,3]

>>> A.count (1)

4

>>> A.count (3)

4

Rearrange elements using sort ()

Python provides two functions

The list method sort () reads the original list and changes the contents of the original list;

The general function sorted () returns a copy of the ordered list and the contents of the original list are unchanged.

>>> a = [5,4,3,2,1]

>>> A.sort ()

>>> A

[1, 2, 3, 4, 5]

>>> a = [5,4,3,2,1]

>>> Sorted (a)

[1, 2, 3, 4, 5]

>>> A

[5, 4, 3, 2, 1]

Sort () Method note

    1. sort the list on the spot, do not write a = A.sort () such a code that attempts to record the return value
    2. You cannot sort a list that has both numeric and string values, causing TypeError Error
    3. when sorting strings, use the ASCII character Order "

Convert to a string using join ()

Join () is the inverse process of split ()

>>> a = [' Hello ', ' world ']

>>> b = ', '. Join (a)

>>> b

' Hello,world '

>>> C = b.split (', ')

>>> C

[' Hello ', ' world ']

Use = assignment, use copy () to assign a value

If you use = To assign multiple variables, changing any of them will cause the values of other variables to be modified as well.

>>> a = [+ +]

>>> B = A

>>> b[0] = ' a '

>>> A

[' A ', 2, 3]

>>> b

[' A ', 2, 3]

>>> a[2] = ' B '

>>> b

[' A ', 2, ' B ']

>>> A

[' A ', 2, ' B ']

Because A and B point to the same object, whether we modify the contents of the list either through a or through B, the result will be on the other side.

There are 3 ways we can copy the value of one list to another new list:

    1. b = A.copy ()
    2. c = List (a)
    3. D = a[:]

b, C, D are all copies of a, changing any one variable will not change the other variables

Python Learning Notes (1)-List

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.