Python data type (list) Learning notes

Source: Internet
Author: User

List
To create a list:
Name_list = ['Alex'seven'Eric'  ] or name_list = List (['Alex'seven'  'Eric'])



In fact, the main content of today's study is
Index
Slice
Additional
Delete
Length
Slice
Cycle
Contains


Indexes and slices:
Flexible values can be obtained by subscript
Define a list name = ["Baobao","Wangdleong", "Wangwenzhe","Wangweiyi","Wangda","Wanger"]
name = ["Baobao","Wangdleong","Wangwenzhe","Wangweiyi","Wangda","Wanger"]Print(Name[0])#default subscript starting from 0Print(Name[2])#take out a third valuePrint(Name[0:2])#remove the value from 0 to twoPrint(Name[-1])#Remove the first value of the countdownPrint(Name[::2])#To take a value by stepPrint(name[-3:])#the third-to-last value of the countdownPrint(Name[::2][0])#Remove the value with a step of 2 and a subscript of 0Print(Name[::2][0][0])#Slice the value againname[0]="Baoshan" #use subscript to modify contentName.insert (3,"Wangdejiang")#directly insert subscript 3 position contentName.append ("lingling")#append content to the lastName.remove ("Baoshan")#Delete content directlyPrint(name)

The printed content is as follows:

Small exercise:

name = ["Baobao","Wangdleong","Xiaodongfang","Wangwenzhe","Wangweiyi","Wangda","Wanger"]name.insert (-1,"Dalongfeng")#Insert the front of subscript-1Name.insert (3,"Dabaozi")#Insert to the position labeled 3Name2=name[2:8]#defines a variable that is equal to the first value of the subscript between 2 and 7Print(name2)Print(name)delNAME[7]#Use the subscript to delete the inserted content using Del (if it is continuous, you can use [2:5])delName[3]name[1]="Wangdelongzuzhang" #Add notes with modificationsPrint(name)Print(Name[::2])#isolated printing using step size, the number of isolates is determined according to the step size

Print the contents as follows:

Copies of lists, and so on:

name = ["Baobao","Wangdleong", 9,44,25,634,478,9,"Xiaodongfang","Wangwenzhe","Wangweiyi","Wangda","Wanger", 9,424,25,631,4748,99]Print(9inchName#Determines whether the list contains element 9, if one is truePrint(Name.count (9))#There are several 9 elements in the judging list .Print(Name.index (9))#finds the first subscript value of a containing elementname2=["Dede", 2, 8,9,3,7,1,58,"Sdjagoew"]name.extend (name2)#Merge Listname[0]="Baobao"Print(name) Name3=name.copy ()#Copy name becomes Name3Print(Name3) name.reverse ()#Flip List#Name.sort () #排序, sorted only by string after 3.0Name2.pop ()#Delete the last one by defaultName2.pop (1)#use subscript to delete the specified valuePrint(name2)Print(name)#You can use a good loop to modify all of the equal tuple elements in the list. forIinchRange (Name.count (9)): Name[name.index (9)]=999999Print(name)

Printed results:

Shallow copy Contrast

name = ["Baobao","Wangdleong", [9,44,25,634,478,9],"Xiaodongfang","Wangwenzhe","Wangweiyi","Wangda","Wanger", 9,424,25,631,4748,99]name3=name.copy () name[0]="Baobao" #modifications in this position will not be brought into the name3.name[2][1]=58247#if the Name3 in the set-up chat table will change.name3[2][2]="dddddd" #if modified through Name3, it will change with each other.#This occurs because the list of nested lists uses a separate memory spacePrint(name)Print(Name3)

Deep copy application, below is a how to see the number of elements Len
ImportSys,copyname= ["Baobao","Wangdleong", [9,44,25,634,478,9],"Xiaodongfang","Wangwenzhe","Wangweiyi","Wangda","Wanger", 9,424,25,631,4748,99]name3=name.copy () name4=copy.copy (name)#like a neme copy, it's a shallow copy.Name5=copy.deepcopy (name)#deep Copy is a direct copy of a block of memoryname[0]="Baobao" #modifications in this position will not be brought into the name3.name[2][1]=58247#if the Name3 in the set-up chat table will change.name3[2][2]="dddddd" #if modified through Name3, it will change with each other.#This occurs because the list of nested lists uses a separate memory spacePrint(name)Print(Name3)Print(NAME4)Print(NAME5)
print (len (name5)) #列表长度, number of elements
print (ID (name),ID (name3),ID (name4),ID (name5)) #可以通过id, view memory location

Python data type (list) Learning notes

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.