Methods for listing in No2.python

Source: Internet
Author: User

First, the index
1 li = ["trony""shi""nvshen"  "nanshen"]2 li = li[0]3  Print(LI)4 >>>trony
IndexSecond, slicing
1 #List Index "Gu Tou regardless of tail"2Li = ["Trony","Shi","Nvshen","Nanshen"]3L1 = Li[0:2]4 Print(L1)5>>>["Trony","Shi"]6L2 = Li[0:-1]7 Print(L2)8>>>['Trony','Shi','Nvshen']9 #returns a list with a step size of 1TenL3 = [0:3:1] One Print(L3) A>>>['Trony','Shi','Nvshen']
slicesThird, increase
1Li = ["Trony","Shi","Nvshen","Nanshen"]2 #Append at end3Li.append ("Tiger")4 Print(LI)5>>>['Trony','Shi','Nvshen','Nanshen','Tiger']6 #inserting insert (index, value)7Li.insert (3,"Tiger")8 Print(LI)9>>>['Trony','Shi','Nvshen','Tiger','Nanshen']Ten #the Add extend () parameter of the iteration must be an iterative object OneLi.extend ("Monkey") A Print(LI) ->>>['Trony','Shi','Nvshen','Nanshen','m','o','N','k','y'] -Li.extend ([1, 2, 3]) the Print(LI) ->>>['Trony','Shi','Nvshen','Nanshen', 1, 2, 3]
IncreaseIv. deletion of
1Li = ["Trony","Shi","Nvshen","Nanshen"]2 #pop () is deleted by index, the deleted element is returned, and the last element is deleted by default without parameters and returned3Name = Li.pop (1)4 Print(name)5>>>Shi6 #Remove () Delete by element7Li.remove ("Trony")8 Print(LI)9>>>["Shi","Nvshen","Nanshen"]Ten #Clear One li.clear () A Print(LI) ->>>[] - #Delete List the delLi - #Slice Delete - delLi[0:2] - Print(LI) +>>>['Nvshen','Nanshen']
DeleteV. Change
1Li = ["Trony","Shi","Nvshen","Nanshen"]2 #Change by index3Li[0] ="Tiger"4 Print(LI)5>>>["Tiger","Shi","Nvshen","Nanshen"]6 #According to Slice change, first take out slice position, after iteration join7Li[0:3] = [1, 2, 3, 4]8 Print(LI)9>>>[1, 2, 3, 4,'Nanshen']
ChangeSix, check
1Li = ["Trony","Shi","Nvshen","Nanshen"]2 #use a circular check3  forIinchLi:4     Print(i)5>>>Trony6>>>Shi7>>>Nvshen8>>>Nanshen9 #Slice CheckTen Print(Li[0:2]) One>>>['Trony','Shi']
CheckVii. Public methods
1Li = ["Trony","Shi","Nvshen","Nanshen"]2 #Len () returns the length of the list3L =Len (LI)4 Print(L)5>>>46 7 #count () returns the number of occurrences of an element8num = Li.count ("Trony")9 Print(num)Ten>>>1 One  A #Sort -Num_list = [2, 1, 4, 8, 3, 5] - #forward Sort the Num_list.sort () - Print(num_list) ->>>[1, 2, 3, 4, 5, 8] - #Flashback Sort +Num_list.sort (reverse=True) - Print(num_list) +>>>[8, 5, 4, 3, 2, 1] A  at #reversal - Num_list.reverse () - Print(num_list) ->>>[5, 3, 8, 4, 1, 2]
Public Methods

Methods for listing in No2.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.