Python Learning Notes (5th lesson)

Source: Internet
Author: User

Now we're going to learn some python data structures, and this section will use the main learning list

Method of 1.list

List.append (x) adds an element at the end of the list

>>> a=[1,2,3,4]
>>> a.append (' a ')
>>> A
[1, 2, 3, 4, ' a ']

List.extend (l) Add all the elements of the list L at the end of lists

>>> a=[1,2,3,4]
>>> l=[' A ', ' B ', ' C ']
>>> A.extend (L)
>>> A
[1, 2, 3, 4, ' A ', ' B ', ' C ']

List.pop ([i]) removes the element with a list index of i-1 and returns the deleted element if I is not specified, then the last element of the list is deleted

>>> a=[1,2,3,4]
>>> A.pop ()
4
>>> A
[1, 2, 3]

>>> b=[1,2,3,4]
>>> B.pop (2)
3
>>> b
[1, 2, 4]

List.remove (x) removes the element with the value x in the list and throws an error if the element is not found in the list

>>> a=[' A ', ' B ', ' C ', ' d ']
>>> a.remove (' B ')
>>> A
[' A ', ' C ', ' d ']
>>> A.remove (9)

Traceback (most recent):
File "<pyshell#24>", line 1, in <module>
A.remove (9)
ValueError:list.remove (x): X not in List

List.index (x) returns the index of an element with a value of x in the list and throws an error if the element is not found in the list

>>> a=[' A ', ' B ', ' C ', ' d ']
>>> a.index (' C ')
2
>>> A.index (2)

Traceback (most recent):
File "<pyshell#27>", line 1, in <module>
A.index (2)
Valueerror:2 isn't in list

List.count (x) Statistics and returns the number of elements in the list that have a value of X

>>> a=[1,2,3,1,2,3,1]
>>> A.count (1)
3
>>> A.count (3)
2

>>> a.count (' a ')

0

List.insert (I,X) inserts an element x at index position I, and when I is greater than the number of lists, the element x is inserted at the end of the list

>>> a=[1,2,3,4,5,6]
>>> A.insert (2, ' a ')
>>> A
[1, 2, ' A ', 3, 4, 5, 6]

>>>a.insert (, ' a ')
>>> A
[1, 2, ' A ', 3, 4, 5, 6, ' a ']

list. Sort (cmp=none, key=none, reverse=false) to reorder the list, The default is to order from small to large (parameters are optional, we will learn the use of parameters later)

>>> a=[2,1,3,0,7,6,8]
>>> A.sort ()
>>> A
[0, 1, 2, 3, 6, 7, 8]

List.reverse () Reverses the list

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

>>> A.reverse ()
>>> A
[0, 2, 1, 4, 3, 2]

2.list slicing

The elements of the reference list can be indexed, and each list is indexed from left to right (positive) starting at 0, right-to-left (reverse) starting from 1. For example, a=[1,2,3, 4,5,6] a forward and reverse index as shown:

A[0]=1

A[-1]=6

A[-6]=1

a[1]=2

List can be obtained by list[x:y] as a subset of lists from index x to Y (not including y).

>>> a=[1,2,3,4,5,6]
>>> A[0:2]
[1, 2]
>>> a[:]
[1, 2, 3, 4, 5, 6]
>>> A[0:]
[1, 2, 3, 4, 5, 6]
>>> A[:3]
[1, 2, 3]
>>> A[-1:-4]
[]

If the element at index y of A[x:y] is at the left of the element at index X, then an empty list is returned, such as: a[4:-6]=[]

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

>>> A[4:-6]
[]
>>> A[5:3]
[]

Strings can also be used as a special list, such as: str= ' Python ', and we can also use slices to intercept substrings: str[2:4]= ' th '

>>> str= ' python '
>>> Str[2:4]
' th '

Note: For strings, we can also use addition to get a substring for a slice, adding another substring

>>> str= ' python '
>>> ' py ' +str[2:4]+ ' on '
' Python '
>>> str[:]+ ' is good '
' Pythonis good '

3. Nested lists

Sometimes the elements of a list may also be a list, which makes up a nested list, if, a=[[1,2,3,4],1,2,3,4], the 3rd element of a list is a sub-list

We can get the elements in the sub-list by list[x][y]:

>>> a=[1,2,[' A ', ' B ', ' C ', ' d '],3,4]
>>> A[2][2]
C
>>> A[2][-1]
' d '

4. Use of the X for X in list statement

We may sometimes need to iterate through the elements of the list, so we can use the for-X in list to do this:

>>> a=[1,2,3,4,5]
>>> for X in a:
Print X

1
2
3
4
5

We may sometimes want to process each element of the list, for example, we want to add 1 to each element of the list:

>>> a=[1,2,3,4,5]
>>> [x+1 for X in a]
[2, 3, 4, 5, 6]

If our list contains nested lists, we can also use this statement, for example: a=[[1,2,3],[4,5,6],[7,8,9]], and we want to add 1 to each element in the child list:

>>> a=[[1,2,3],[4,5,6],[7,8,9]]
>>> [[Sublist[i]+1 for I in range (3)] for sublist in a]
[[2, 3, 4], [5, 6, 7], [8, 9, 10]]
>>>

If we were to imagine a two-dimensional array as inverted arrays, that is, I want to a=[[1,2,3],[4,5,6],[7,8,9]] [[1,4,7],[2,5,8],[3,6,9]], we can do this:

>>> a=[[1,2,3],[4,5,6],[7,8,9]]
>>> [[Sublist[i] for sublist inch A] for I in range (3)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

The above code can also be replaced with the following code:

>>> x=[[],[],[]]
>>> a=[[1,2,3],[4,5,6],[7,8,9]]
>>> for I in range (3):
For sublist in a:
X[i].append (Sublist[i])


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

Python Learning Notes (5th lesson)

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.