List slicing operations in Python

Source: Internet
Author: User

1a=['Zhao','Qian','Sun','Li','Zhou',"']2 3 #Change and delete4 #check section []5 Print(a[1:])#take the last6 Print(A[1:-1])#take the second-lowest value7 Print(A[1:-1:1])#from left to right one to fetch8 Print(A[1::2])#take it from left to right.9 Print(a[3::-1])TenB=a[3::-1] One Print(b)#[' Sanpang ', ' Xiaohu ', ' jinxin ', ' Wuchao '] A Print(a[-2::-1]) - Print(A[1:-1:-2])

Results:

[' Qian ', ' sun ', ' Li ', ' Zhou ', ' Wu ']    #5 [' Qian ', ' sun ', ' Li ', ' Zhou '] #6 [' Qian ', ' sun ', ' Li ', ' Zhou '] #7 [' Qian ', ' Li ', ' Wu '] #8 [' Li ', ' Sun '                      , ' Qian ', ' Zhao '] #9 [' Li ', ' Sun ', ' Qian ', ' Zhao '] #11 [' Zhou ', ' Li ', ' Sun ', ' Qian ', ' Zhao '] #12 [] #13

  

1a=['Zhao','Qian','Sun','Li','Zhou','Wu']2 3 #Add append Insert4 5A.append ('Xuepeng')#default plug to last position6 Print(a)7A.insert (1,'Xuepeng')#inserting data into any location8 Print(a)

Results:

['Zhao','Qian','Sun','Li','Zhou','Wu','Xuepeng']['Zhao','Xuepeng','Qian','Sun','Li','Zhou','Wu','Xuepeng']
1a=['Zhao','Qian','Sun','Li','Zhou','Wu']2 #Modify3 #4a[1]='Haidilao'5 Print(a)6a[1:3]=['a','b']7 Print(a)

Results

1['Zhao','Haidilao','Sun','Li','Zhou','Wu']2['Zhao','a','b','Li','Zhou','Wu']
1 #Remove the Remove pop del2a=['Zhao','Qian','Sun','Li','Zhou','Wu']3 A.remove (a[0])4 Print(a)5B=a.pop (1)6 Print(a)7 Print(b)8 delA[0]9 Print(a)

Results:

1['Qian','Sun','Li','Zhou','Wu']2['Qian','Li','Zhou','Wu']3 Sun4['Li','Zhou','Wu']
1 #Count: Count the number of occurrences of an element2t=[' to',' be','or',' not',' to',' be'].count (' to')3 Print(t)4 5 #Extend6A = [1, 2, 3]7b = [4, 5, 6]8 A.extend (b)9 Print(a)Ten Print(b)

1 2

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

3 [4, 5, 6]

Summarize:

1 list, tuple2 Check3 index (subscript), all starting from 04 slices5 . Count checks the number of occurrences of an element6 . Index to find its corresponding location according to the content7             "Haidilao GE" incha8 Increase9 a.append () AppendTenA.insert (Index,"content") One a.extend Extension A  - Modify -A[index] ="the new value" theA[start:end] =[A,b,c] -  - Delete -Remove"content") + Pop (index) - del A, del A[index] + a.clear () empty A  at Sort - sort () -Reverse ()

List slicing operations in Python

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.