About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python Basic operations for lists , About insertion 、 Operation syntax of assignment and search .
Use insert() Method to insert the specified object into the specified location of the list , The format is as follows .
list.insert(index,obj)
for example :
number = [1, 2, 4, 5] # Create a list of number And the assignment
number.insert(2, 3) # Use insert() Method will element 3 The Index added to the list is 2 In the
print(number)
give the result as follows .
Returns the first occurrence of the specified element in the list , If the element is not in the list, an exception is thrown , The format is as follows .
list.index(obj)
example : Find elements in the list , If you find , Output the index position of the element in the list , Otherwise, the output is not found .
animal = ['elephant', 'monkey', 'snake', 'tiger'] # Create a list of animal
x = input(' Please enter the name of the animal you want to find :') # Enter the element to find
if x in animal: # Find if it exists
a = animal.index(x) # Returns the index
print(' Elements {0} The index in the list is :{1}'.format(x, a)) # Output index number
else:
print(' The element does not exist in the list ') # Output information not found
give the result as follows .
Count the number of times the specified element appears in the list , The format is as follows .
list.count(obj)
for example :
x = [1, 2, 1, 2, 1, 2] # Create a list of
b = x.count(1) # Use count() Methods statistics 1 In variables x The number of occurrences in
print(b)
give the result as follows .
x = [1, 7] # Create a list and assign values
x[1:1] = [2, 3, 4, 5, 6] # In variables x The index for 1 Insert the list at the location of [2,3,4,5,6]
print(x)
x = [1, 2, 3, 4] # Create a list and assign values
x[2:] = [5, 6, 7] # Replace variable x Index from 2 The elements from the beginning to the end
print(x)
x = [1, 2, 3, 4, 5, 6, 7] # Create a list and assign values
x[1:6] = [] # Empty list x Middle index 1 To the index 6 Element replacement between
print(x)
give the result as follows .
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
The above is about Python Basic operations for lists , About insertion 、 Operation syntax of assignment and search . You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .