python notes-----list additions, deletions and changes

Lists are represented by '[]' in python

List query operations

 

 

  • slice of list

 

 

names = ["a","b","c"]             #Define a list with double quotes to indicate the elements of the list, which look like numbers but are actually strings

print(names[0],names[b])      #Slice names[0] represents the first element in the list, names[a] represents the second element in the list, and so on
print(names[:b])                    # The slice starts to the third
print(names[-a])       #take the last
print(names[-b])                    #the second from the right
print(names[-b:])                   #take the last two

output result

a c

['a', 'b']

c

b

['b', 'c']

  • index method - find element position

names = ["a","b","c"]
print(names.index("c"))
print(names[names.index("c")])

 output result

                  ##The element whose subscript is 2 is equivalent to the third one in the list

c

  • count method - how many elements are specified in the query list

names = ["a","b","c","c"]
print(names.count("c"))

 output result

2

  • reverse method - reverse the list

names = ["a","b","c","c"]
names.reverse()
print(names)

 output result

['c', 'c', 'b', 'a']

or

names = ["a","b","c"]

a = names[::-1]
print(a)

output result

['c', 'b', 'a']

  • sort method - list sorting

Rules: special symbols - numbers - uppercase - lowercase

names = ["b","a","c","c"]
names.sort()
print(names)

 output result

['a', 'b', 'c', 'c']

  • list step size

names = ["b","a","www","qqq","c","d","e","f"]
print(names[0:-1:2])
print(names [::2])                #From the beginning to the end, take one every other

 output result

['b', 'www', 'c', 'e']

['b', 'www', 'c', 'e']

 

Adding operations to the list

  • append method - appends elements

names = ["a","b","c"]
names.append("4")       #追加一个元素
print(names)

output result

['a', 'b', 'c', '4']

  • insert method - inserts an element

names = [ "a" , "b" , "c" ]
names.insert( a , "d" ) #Insert "5" at position a
print (names)

output result

['a', 'd', 'b', 'c']

  • extend method - merges lists

names = ["b","a","c","c"]
names2 = [1,2,3,4]
names.extend(names2)
print(names)

output result

['b', 'a', 'c', 'c', 1, 2, 3, 4]

  • copy method - copy list

names = ["b","a",["www","qqq"],"c","c"]
name2 = names.copy()
print(names)
print(name2)

 output result

['b', 'a', ['www', 'qqq'], 'c', 'c']

['b', 'a', ['www', 'qqq'], 'c', 'c']

List modification operations

  • Modify the list

names = ["a","b","c"]
names[1] = "d" #The               element with subscript 1 is changed to "d"

print(names)

output result

['a', 'd', 'c']

  • Modify a sublist in a list

names = ["b","a",["www","qqq"],"c","c"]

names[2][0]="Wwe" #The    first print(names)                    in the third element

 output result

['b', 'a', ['Wwe', 'qqq'], 'c', 'c']

List delete operation

  • remove ,del,pop delete method

================================Method 1================== ===========

names = ["a","b","c"]
names.remove("a")
print(names)

 output result

['b', 'c']

================================Method 2================== ===========

names = ["a","b","c"]

del names[0]                        #=names.pop(1)
print(names)

  output result

['b', 'c']

================================Method 3================== ===========

names = ["a","b","c"]

names.pop()                            #default remove the last one
print (names)

  output result

['a', 'b']

 

  • clear method - clears the list

names = ["a","b","c","c"]

names.clear()
print(names)

 output result

[]

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325307177&siteId=291194637