python base ---- day2 (magic list)

--- --- restore content begins

# List # class list

li = [1, 12, 9, "age", [ "lishijie"], "Shijie"] # objects created by the class list

1. The basic format of the list

        # In brackets

        #, Split each element

2. A list can be nested to any type

        # Elements in the list can be numbers, strings, lists, Boolean, can be put to all

3. Value Index

print (li [3])

4. Slice, slice the result is a list of

5. for loop

6. while loop

 

note: # # list elements can be modified

7. The index ########### ##########

#modify

li = [1, 12, 9, "age", ["lishijie", ["19", 10]], True]
li[1] = 120
print(li)

 

 # Delete (the first way)

li = [1, 12, 9, "age", ["lishijie", ["19", 10]], True]
del li[1]
print(li)

 

 

8. slice ########## ###########

#modify

li [1: 3] = [120, 90 ]
 print (li)

#delete

of you [2: 6 ]
 print (you)

 

9. in operation

li = [1, 12, 9, "age", ["lishijie", ["19", 10]], True]
v = 12 in li
print(v)

 

 

10. Conversion

      # String conversion list li = list ( "shdhshgj"), used for the internal loop

     # List into a string, Ⅰ, a need to write your own for a loop handle (both numbers string)

 

 

                                        Ⅱ, directly join method of strings: String only elements in the list

li = ["123","alex"]
v = "".join(li)
print(v)

 

 

Method ################################### list class provided ######### #################

1. added after the original

li= [11,22,33,44]
li.append(5)
li.append("lishi")
li.append([123])
print(li)

 

2. Clear List

li.clear()

print (li)

 

3. Calculate the number of elements appearing

= li [11,22,33,44,44,55 ] 
v = li.count (44 )
 print (v)

 

 

4. Extension of the original list of parameters: iterables

li= [11,22,33,44,44,55]
li.extend("刘翔宇")
print(li)

 

 The current value of the index value obtained in accordance with the position (leftmost priority)

= li [11,22,33,44,44,55 ] 
v = li.index (33 )
 print (v)

 

 

6. Insert element specified index

= [11,22,33,44,44,55 ] 
li.insert (0, 99 )
 print ()

 

 

7. pop delete a value, and get value for deletion (1. Specify the index, 2. not specified, the default is the last one)

    

= li [11,22,33,44,44,55 ] 
v = li.pop ()
 print (li)
 print (v)

      remove: delete the specified value list, left priority

 

8.reverse reverse the current list

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

Sort 9.sort

 


 

 notes: string can not be modified once created

 

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/lishijie-/p/11622513.html