Python basic----List

Source: Internet
Author: User

Definition list-->l=[]

stus=[' Zhang San ',' John Doe ',' Harry ']

List additions and deletions change

#IncreaseStus.append ('Dee')#Add---Append add an element to the end of the listStus.insert (0,'Chenkai')#Inster (subscript, value) adds an element at the specified location if the specified subscript does not exist, or the end is added#Changestus[1]='Momo'#use the subscript of an element to assign a value directly#CheckPrint(Stus)#Print the value of the entire listPrint(stus[subscript])#view the elements corresponding to the subscriptPrint(Stus.count ('Xiaohei'))#count looks at the number of elements in the array, returns 0 if it does not exist, and for the uniqueness check, you can use count greater than 1 to remove the weightPrint(Stus.index ('Chenkai'))#Index finds the subscript of an element, if there is more than one, returns only the first, if there is no error#DeleteStus.pop ()#pop (), do not send subscript delete the last one, specify the subscript is the deletion of the specified element, the specified subscript does not exist The Times wrongStus.remove ('Zhang San')#Delete the value of the passed element, multiple duplicate values delete the firstdel Stus[0] #delStus.clear ()#Clear List
#注: Pop can return the deleted element, remove No, pop the subscript, remove the value of the element

List built-in functions

Reverse----element Inversion

Stus.reverse ()

Sort----can be sorted on a pure or pure character type, not an array of integers and strings mixed

nu=[12,3,4,5,4,65,665,5,4,4545]nu.sort ()#nu.sort (reverse=true)#  Descending requires specifying Reverse=true

Extend---Merging of two lists

nu=[1,2,]name=[3,4]nu.extend (name)#name appended to Nu,name value invariant print(NU)

List Slice

#Slice---a way to take a list of valuesPrint(Names[0:3])#Remove the value of the marked 0,1,2, the slice is Gu Tou regardless of the tail,Print(Names[:8])#the previous subscript does not write, the default is written from the beginningPrint(names[3:])#After the subscript does not write, take the last onePrint(names[:])#the subscript is not written, the full take#Step Sizenu=[1,2,3,4,5,6,7,8,9,10]Print(Nu[::2])#步长为正数, take from left to right#步长为2, take one from every other.
Print (Nu[-1:-11:-1])#step is negative, take from right to left
Print(Nu[9:-11:-1])
The list also removes the label and value---enumerate
t=[11,22,33] for in Enumerate (t):#Enumerate loop subscript and value at the same time, the first represents the index, the second represents a value    Print('%s:%s'% (i,v))

Example: Registration-function: input uname and password, written as a dead loop has been registered, if the user name already exists, prompting the user name already exists

names=[] whileTrue:uname=input ('Please enter user name:')    ifUnameinchNames#can also if Names.count (uname) ==0: Determine if the user name exists        Print('user name already exists')    Else: Names.append (uname) #用户名不存在追加到names数组中 passwd= Input ('Please enter your password:')        Print('Registration Successful')

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