程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Modification of Python list basic operation

編輯:Python

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 .


One 、 background

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 .


Two 、 Insert elements

1、insert() Method

Use insert() Method to insert the specified object into the specified location of the list , The format is as follows .

  • list Represents a list
  • index Indicates the index position where the object needs to be inserted
  • obj Represents the object to insert into the list

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 .


3、 ... and 、 Find count element

1、index() Method

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 Represents a list
  • obj Represents the object to find

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 .

2、count() Method

Count the number of times the specified element appears in the list , The format is as follows .

  • list Represents a list
  • obj Indicates the object to be counted

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 .


Four 、 Slice assignment

  • Piecewise assignment is to assign a value to a sequence in the form of piecewise assignment , You can assign values to multiple elements at once .
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)
  • Slice assignment can also insert new elements without replacing any original elements .
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)
  • Slice assignment can also be used to delete elements .
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 .


5、 ... and 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


6、 ... and 、 summary

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved