Python learns lesson five! List

Source: Internet
Author: User
Tags shallow copy

List lists
# list classes, lists
#中括号括起来
#, separating each element
#列表中的元素可以是数字, String, List, Boolean ... All of them can be put in.
Put anything inside #就是个 "collection"
#索引取值
#切片结果也是列表
Li = [ Zhang Mingshing ,'star', [' 19,3],' jiege  '# objects created from the list class print(li[0])print(li[3:-1]) # for Loop, while Loop  for inch Li:     Print (item)

Modify

Li = [A.'Star',['Zhang Mingshing', [19,3],'Jiege'],true]li[0]= 10Print(LI) li[1] = [11,22,22]Print(LI)#How to modify slicesLi[1:3] = [22,33]Print(LI) [10, 2, 3,'Star', ['Zhang Mingshing', [19, 3],'Jiege'], true][10, [11, 22, 22], 3,'Star', ['Zhang Mingshing', [19, 3],'Jiege'], true][10, 22, 33,'Star', ['Zhang Mingshing', [19, 3],'Jiege'], True]

Delete

#Delete, the first wayLi = [A.'Star',['Zhang Mingshing', [19,3],'Jiege'],true]delLi[1]Print(LI)#Slice the way to deletedelLi[1:3]Print(LI) [1, 3,'Star', ['Zhang Mingshing', [19, 3],'Jiege'], true][1, ['Zhang Mingshing', [19, 3],'Jiege'], True]

Operation

# the value of the list of neutrons  ' Star ', [' Zhang Mingshing ' jiege 'print (li[4][1][0])19


#Convert a string to a list#conversion, string conversion to List Li = Lists (' dfdsafsfdasf ') internal use for loop, number cannot be used for loops ='ADFSAFDFASDFDSFASDFDSFF'New_li=list (s)Print(new_li) ['a','D','F','s','a','F','D','F','a','s','D','F','D','s','F','a','s','D','F','D','s','F','F']
 #   Converts the list to a string, If the list has both a number and a string, you can write it yourself for loop   li  = [11,22,33,  " 123  , "  star   " ]s  =  " for  i in   li:s  = s + STR (i)  print   (s) 112233123star 
# converts a list to a string, and the elements in the list can be used only with a string join  Li = ['123','star''. Join (LI)  Print(v) 123star

APPEND () original value last appended

Li = [11,22,33,44]li.append (5)print(LI) [11, 22, 33, 44, 5]

Clear () Clear

Li = [11,22,33,44]li.clear ()print(LI)

Copy () copies, shallow copy

Li = [11,22,33,44= li.copy ()print(v)

Count ()

# number of occurrences of the calculated element li =[11,22,33,44,22 = Li.count ()print(v)

2
Extend () Extending the original list, parameters: objects that can be iterated
Li = [11,22,33,44]li.append ([9898,'incredibly'])#append an element as a whole to the lastLi.extend ([9898,'incredibly'])#equivalent to looping one element to the lastLi.extend ('incredibly')Print(LI) [11, 22, 33, 44, [9898,'incredibly'], 9898,'incredibly','No','Get','the']

Index () Gets the current value indexed position by value (left priority)

Li = [11,22,33,44= Li.index ()print(v)

1

Insert () Adds an element at the specified index position

Li = [11,22,33,44]li.insert (1,99)print(LI) [11, 99, 22, 33, 44]

Pop () deletes a value and can get the deleted value without specifying the index to delete the last value by default

Li = [11,22,33,44= li.pop (1)print(li)print(v) [one,  ]22

Remove () removes the specified value from the list, with the left priority removed

Li = [11,22,33,44]li.remove (P)print(LI)    

Several ways to delete a list ps:pop (), remove (), Del li[0] index, del[0:3] slice, clear ()

REverse () reverses the current list

Li = [11,22,33,44]li.reverse ()print(LI)

[44, 33, 22, 11]

Sort () Sorting of list

Li = [11,55,22,33,44]#li.sort () #从小到大排序li.sort (reverse=true)# from big to small rows Print (LI) [55, 44, 33, 22, 11]

PS: After creating a string, you can use the index value, you can not modify the value, the list can modify the value

Python learns lesson five! 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.