Python 04-List,

Source: Internet
Author: User

List

Table name = ["Name1", "name2"]

Print (Names[1:3]) #切片

The starting position includes, and the end position is not included. will take 1 and 2, without taking 3
names = ["name1","name2","Name3","Name4"]Print(names)Print(names[1],names[2])Print(Names[1:3])#slicesPrint(Names[-1])#take the last one .Print(names[-2:])#take the last twoPrint(Names[0:3])#take the top twoNames.append ("Name5")#insert inside the arrayNames.insert (1,"Name6")#inserts into the array position number 1thNames.insert (3,"Name7")#inserts into the array position number 3rdnames[2]="Name8"#2nd element changed to Name8Names.remove ("Name7")#Delete element Name7delNAMES[2]#remove element Number 2ndNames.pop ()#The default is to delete the last one, enter the subscript to delete the number of elementsPrint(Names.index ("name1"))#where to find name1?Print(Names[names.index ("name1")])#find the name1 and print it out.Names.append ("Name5") Names.append ("Name5")Print(Names.count ("Name5"))#count how many name5Names.reverse ()#List ReversalNames.sort ()#Sort by first alphabetical orderNames2= [1,2,3,4]names.extend (names2)#put another list and come overnames.clear ()#List EmptyPrint(names)

Shallow copy

names = ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]name2= Names.copy ()#Copy only the first layer of the list, the second layer is the pointer, when printed from the pointer to find the original addressPrint(names)Print(name2) names[0]="Name 1"names[3][0]="Alex"#name2 also changed inside.Print(names)Print(name2)

Deep copy

ImportCopynames= ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]name2= Copy.deepcopy (names)#deep Copy, full copy, second level list does not follow changePrint(names)Print(name2) names[0]="Name 1"names[3][0]="Alex"Print(names)Print(name2)

names = ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]Print(Names[0:-1:2])#Start,stop,step

Python 04-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.