Python Learning notes (list)

Source: Internet
Author: User

List

1 ordered Collections

2 index by offset to read data

3 Nesting Support

4 variable types

a=[1,2,3]

Print (a[-1]) result 3

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

Print (b) Results [[1, 2, 3], [4, 5, 6]]

Print (b[0]) results [1, 2, 3]

Print (b[0][1]) result 2

1 slices

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

Print (A[0:3:1]) results: [1, 2, 3] #正向索引

Print (A[-1:-5:-1]) results: [7, 6, 5, 4] #反向索引

Print (a[1:]) Results: [2, 3, 4, 5, 6, 7] #默认索引

Print (A[1::2]) results: [2, 4, 6] #默认索引, step value 2

2 Adding actions

+

Extend accepts the parameter and adds each element of the parameter to the original list, modifying the list in place instead of the new list

Append add a reference to any object to the end of the list

Insert inserts a reference from any object into the list to control the insertion position

To create a list:

Member=[' Wu ', ' Jia ', ' Dong '

Member.append (' Xiang ') #append () function can only add one element or a list

Print (member) results [' Wu ', ' Jia ', ' dong ', ' Xiang ']

Member.extend ([' Bi ', ' ye ', ' le ', ' ya ') #exten () function can add multiple elements to the list, but with square brackets (in list form)

Print (member) results [' Wu ', ' Jia ', ' dong ', ' Xiang ', ' bi ', ' ye ', ' le ', ' ya ']

Member.insert (3, ' Hao ') # inserts an element into the list at the specified location

Print (member) results [' Wu ', ' Jia ', ' Dong ', ' hao ', ' Xiang ', ' bi ', ' ye ', ' le ', ' ya ']

3 List Modification

Modify the list itself simply by assigning a value directly to the action.

4 deletion of the list

Del What we do by indexing what takes out the element at the specified position

Del A deletes a reference to a list object

Del a[:] Empty the elements in the list object

Remove removes the first matching value from the specified value in the list. If you don't find it, you'll

Pop returns the last element and removes it from the list

Member.remove (' Hao ') # only one element can be deleted

Print (member) results [' Wu ', ' Jia ', ' dong ', ' Xiang ', ' bi ', ' ye ', ' le ', ' ya ']

Del Member[0] #del后面不能有点号, only numbers are in parentheses. Deletes the element at the specified position

Print (Member) Results ' Jia ', ' dong ', ' Xiang ', ' bi ', ' ye ', ' le ', ' ya '

List element interchange location (Xiang and bi interchange)

TEMP=MEMBER[2]

MEMBER[2]=MEMBER[3]

Member[3]=temp

Print (member) results [' Jia ', ' Dong ', ' bi ', ' Xiang ', ' ye ', ' le ', ' ya ']

Member.pop ()

Print (member) results [' Jia ', ' Dong ', ' bi ', ' Xiang ', ' ye ', ' le ']

Member.pop (5)

Print (member) results [' Jia ', ' Dong ', ' bi ', ' Xiang ', ' ye ']

5 List index value "square brackets []"

Member "1:3"

6 List Operators

* Comparison Operators

* Logical operator

* Connection operator

* Repeat operator

* Member relationship operator

Comparison operators:

list1=[1,2,3]

list2=[2,3,4]

Print (LIST1>LIST2) result false #列表与列表的比较是比较0元素

Logical operators:

list1=[1,2,3]

list2=[2,3,4]

lst3=[4,5,6]

Print (List1<list2 and LIST2<LIST3) results true

Connection operators:

List5=list1+list2

Print (LIST5) results: [1, 2, 3, 2, 3, 4]

repeating operator

list1=[1,2,3]

list2=[2,3,4]

List1*=3

Print (List1) results: [1, 2, 3, 1, 2, 3, 1, 2, 3]

Member relationship operators;

list1=[1,2,3]

list2=[2,3,4]

Print (1 in List1 and 5 not in List1) results true #注意这里的元素不要引号

list3=[1,2,3,[4,5,6],7,8,9] # [4,5,6] is an element in List3

Print (4 in list3) Result: False #应该要写 [4,5,6] is an element in List3

Print (4 in list3[3]) results: True

Print (list3[3][2]) results: 6

7 list built-in functions (DIR (list))

Count function

list1=[1,2,3,1,2,3,1,1,1,1]

Print (List1.count (2)) Results 2 #count函数, calculates the number of times his argument is in the list, and 2 appears 2 times

Index function

list1=[1,2,3,1,2,3,1,1,1,1]

Print (List1.index (1,3,8)) Results 3 #后面参数3和8表示范围

Reverse () function

list1=[1,2,3,1,2,3,1,1,1,1]

List1.reverse ()

Print (List1) results [1, 1, 1, 3, 2, 1, 3, 2, 1]

Sort () function

list1=[1,2,3,1,2,3,1,1,1,1]

List1.sort () #没加参数, default from small to large

Print (List1) results [1, 1, 1, 1, 1, 1, 2, 2, 3, 3]

List1.sort (Reverse=true) #加上参数, in turn, from large to small

Print (List1) results: [3, 3, 2, 2, 1, 1, 1, 1, 1, 1]

The Shard copy of the list complements:

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

list2=list1[:] #只是复制了list1中的值

List3=list1

Print (List1) results [6, 5, 4, 3, 2, 1]

Print (LIST2) results [6, 5, 4, 3, 2, 1]

Print (LIST3) results [6, 5, 4, 3, 2, 1]

Print (' \ n ')

List1.sort () results

Print (List1) results [1, 2, 3, 4, 5, 6]

Print (LIST2) results [6, 5, 4, 3, 2, 1]

Print (LIST3) results [1, 2, 3, 4, 5, 6]

8 list derivation: [Expr for Iter_var in iterable]

First iterate over all the contents of the iterable, each iteration, put the corresponding content in the iterable into Iter_var, and then apply the content of the Iter_var in the expression, and then generate a list with the calculated value of the expression. For example, we're going to generate a list that contains 1 to 18

print ([x for X in range (1,11)])

Print (List range (1,11))

[Expr for Iter_var in iterable if COND_EXPR]

print ([x for X in range (1,11) if x% 2 = = 1])

6 Built-in list

List (obj)-Returns a list

7 Range compared to xrange

Python Learning notes (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.