Python Basics (list basic usage)

Source: Internet
Author: User

List Basic usage:

 #list is separated by "[]" Definition with ","

Example:

#stu_name= ' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' ... '

#定义一个名为stu_name的list

stu_name= [' Sea Dragon ',' yangfan ',' liurong heart ', ' ...... ' ]#print(stu_name[3])# Query the first element in list # output result: Hailong

list-Add and delete changes-increase:

stu_name= [' Sea Dragon ',' yangfan ',' liurong heart ', ' ...... ' ]stu_name.append (' Xiao Yue month ')# Add an element at the end of the list Print(stu_name)# output: [' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' ... ', ' Xiao Yue Moon ']

Stu_name= ['Hailong','Yangfan','Liurong Heart','......']stu_name.insert (0,'Little Sweetheart .')#add an element at the specified position (subscript)Stu_name.insert (0,'Little Sweetheart .')#second execution if the position is the same, the last added element position is moved backPrint(Stu_name)#output: [' Little Sweetheart ', ' Little Sweetheart ', ' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' ... ']

list-additions and deletions-change (only one method of modification):

 stu_name= [ "  Hailong   ", "   Yangfan   ", "   Liurong Heart   ", "   ...   ' ] #   subscript 0, 1, 2, 3  stu_name[2]= "  Little Red  "  #   Modify the elements of subscript 2, and change the Liurong heart to small red.  print   (stu_name)  #   output: [' Sea Dragon ', ' Yangfan ', ' Little Red ', ' ... ']  

list-additions and deletions--delete:

#. Pop method: Send subscript Delete element (usage is relatively single, only the elements of list can be deleted)Stu_name= ['Hailong','Yangfan','Liurong Heart','......']stu_name.pop ()#Delete the last element, or the last element by default if you do not enter a subscript value. Stu_name.pop (1)#specifies a corner label that deletes the specified element. Specify the contents of subscript 1, delete ' Yangfan '; fill-1 is the deletion of the last subscript content. If you delete from the back, you can use "-"Print(stu_name)#输出结果:['Hailong','Liurong Heart']
#. Remove method: Pass element Delete elementStu_name= ['Hailong','Yangfan','Liurong Heart','......']stu_name.remove ('Liurong Heart')#----If there is a duplicate name, only the first element found can be deleted, and remove cannot delete more than one element at a timePrint(Stu_name)#output result: [' Sea Dragon ', ' yangfan ', ' ... ']
# del method: Delete elements by subscript (Del deletes object-oriented comparisons, such as functions, dictionaries, etc.)stu_name=[' Sea Dragon ',' yangfan ',' liurong heart ','... ' ]del# Delete the element "Liurong Heart"print(stu_name) in subscript 2
#输出结果: [' Sea Dragon ', ' yangfan ', ' ... ']

List additions and deletions-check:

Stu_name= ['Hailong','Hailong','Yangfan','Liurong Heart','Xiao Fang']Print(Stu_name[-1])#query the elements in the list using the corner label, and the last one#output Result: Xiao FangPrint(Stu_name[0])#Query the element in the list by the corner label, the first digit#output Result: HailongPrint(Stu_name.count ('Hailong'))#Query the number of occurrences of the same element in the list#Output results: 2Print(Stu_name.index ("Liurong Heart"))#find the corner label of an element#Output results: 3Print(Stu_name.reverse ())#Reverses is reverse list, the positive sequence becomes flashbacks, but does not output resultsPrint(Stu_name)#output: [' Small Fang ', ' Liurong heart ', ' Yangfan ', ' Sea Dragon ', ' Sea Dragon ']Stu_name.clear ()#clear clears the list elementPrint(Stu_name)#output Result: []

List sort:

stu_name=['Hailong','Hailong','Yangfan','Liurong Heart','Xiao Fang']stu_num=[2,3,6,1,4,5,6,7,8, 0]stu_num.sort ()#The elements are arranged in a positive order and cannot be sorted if there is a combination of the STR and int types in the list. Print(Stu_num)#output results: [0, 1, 2, 3, 4, 5, 6, 6, 7, 8]Stu_num.sort (Reverse=true)#add Reverse=true, is the flashback arrangement, pay attention to true,t must capitalize. Print(Stu_num)#Output results: [8, 7, 6, 6, 5, 4, 3, 2, 1, 0]

List Association Merge:

#List Association Mergestu_name=['Hailong','Hailong','Yangfan','Liurong Heart','Xiao Fang']stu_num=[2,3,6,1,4,5,6,7,8, 0]new_lis=stu_name+stu_num#The list can be merged, but a new list is generatedPrint(New_lis)#output: [' Sea Dragon ', ' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' small Fang ', 8, 7, 6, 6, 5, 4, 3, 2, 1, 0]Stu_name.extend (Stu_num)#To Associate multiple lists, you can add other elements under the list to the current listPrint(Stu_name)#output: [' Sea Dragon ', ' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' small Fang ', 8, 7, 6, 6, 5, 4, 3, 2, 1, 0]Print(stu_name*2)#the list element of the output, which can be +-*/#output: [' Sea Dragon ', ' Sea Dragon ', ' Yangfan ', ' Liurong heart ', ' small Fang ', 2, 3, 6, 1, 4, 5, 6, 7, 8, 0, ' Sea Dragon ', ' Sea Dragon ', ' Yangfan ', ' liurong ', ' Small Fang ', 2, 3, 6, 1, 4, 5, 6, 7, 8, 0]

List loop:

passwd=['111111','222222','333333'  ] for the passwd:#forloop, direct loop list, loop every time you take the element    in the list Print (' value per loop:', s) # Output: Value per loop: 111111 \ n per loop value: 222222 \ n per loop value: 333333

List assignment:

passwd=['111111','222222','333333']ix=0 forSinchPasswd:passwd[ix]='abc_'+s#each time the output is preceded by a value abc_Ix+=1#add 1 to each subscriptPrint(passwd)#output Result: [' abc_111111 ', ' abc_222222 ', ' abc_333333 ']

List Multidimensional array:

 nums=[1,2,3,4,5,6,[ " a  " ,  " b  " ,  " c  " ,  " d  " , [ "  a   ", "   two  ]]]#   There are several [] combinations of the several-dimensional arrays, The Nums of this demo is a three-dimensional array  print  (Nums[6][4][1])  #   output: two  
#list Basic application, check whether mobile phone number exists. iphone = ['151','138','159'] forCounterinchRange (5): IP= Input ("Mobile Number:")    ifIpinchiphone:Print("Mobile number already exists")        Continue    Else:        Print("phone number not registered")        Continue

Python Basics (list basic usage)

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.