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

Python learning (V) addition, deletion, modification and query of lists

編輯:Python

Container data type

Non container data type : A variable can only hold one data
Container data type : A variable can hold multiple data at the same time

list --------- >(list)
Dictionaries --------->(dict)
Tuples --------->(tuple)
aggregate ---------->(set)
character string ------>(str)

1、 list :

 list = []
  1. A list is a container data type ( A list can hold multiple data at the same time ); take [] As a sign of the list , Multiple elements are separated by commas :[ Elements 1, Elements 2, ... , Elements n]
  2. characteristic :
    1. The list is variable ( The number of elements in the list 、 The value and order of elements are variable )
    2. The list is ordered ( Each element has an index value corresponding to its position )
  3. Element requirements –>( No requirement )
    Any type of data , Can be used as elements of the list , And the same list can store different types of data .

2、 Basic operations for lists

  1. Query of list elements :

    1. Look up a single : Get an element one by one

      1. grammar : list [ Subscript ] -----> Gets the element corresponding to the specified subscript
        list : Any expression that results in a list , Usually use the variable that saves the list
        [] : Fixed grammar
        Subscript : A subscript is also called an index , It is the position information of elements in an ordered sequence
        Each element in the list has two subscripts ,( One is from 0 The subscript value that starts to increase in turn (0 Represents the first element );( One is from -1 Start decreasing the subscript value in turn (-1 Represents the last element ))
        Subscript cannot be out of bounds , Valid range of subscript : 0~ length -1 and -1~- length
      2. len( list ) : Get the number of elements in the list ( Get the length of the list )
    2. section : Get some elements

      1. Complete grammar : list [start : end : step]
        Start subscript , End subscript : Subscript value ; Used to determine the effective range of slices :[start, end); start The default is the first element in the direction , end The default is the last element in the direction
        step : Positive or negative integers ; Use the sign to determine the acquisition direction ; The absolute value is used to determine the acquisition method , The default is 1
        :: Fixed writing
        1. If the direction corresponding to the step size is inconsistent with the direction from the element corresponding to the start subscript to the element corresponding to the end subscript , Slice invalid , The result is an empty list
        2. Slice valid , First determine the effective range ([start,end)), Within the effective range, the element is obtained according to the absolute value of the step according to the direction of the step , Common new lists
      2. Omit grammar :
        1. Omit step size : list [start : end] — Omit step size , The default step size is 1
        2. Omit the beginning : list [ : end : step] — If the step size is just , from 0 Start ; If the step size is negative , from -1 Start .
        3. Omit the end : list [start : : step] — According to the direction of the step to the end , Step size is positive , To the last element , Step size is negative , To the first element
    3. Traverse : One by one , Take it out

    1. Method 1 : Get elements directly
    for Variable in list:
    pass
    # Variables in turn get the elements in the list 
    2. Method 2 : --- Get the subscript first , Get elements with subscripts
    for Variable in range(len(list)):
    pass
    # The variable gets the subscript of the element at a time 
    3. Method 3 :---- Enumeration , Directly fetch the element in the list and its corresponding index
    for Subscript , Elements in enumerate(list):
    pass
    # Get both list elements and Subscripts 
    
  2. The addition of list elements :

    1. list .appen( Elements ): Add the specified element at the end of the specified list
    2. list .insert( Subscript , Elements ) : Insert the specified data at the specified position in the specified list
  3. Deletion of list elements :

    1. list .clear(): clear list
    2. list .remove( Elements ) : Always delete the specified data in the specified list ( Element does not exist will report an error ; If there is more than one data , Which of the first ones will be deleted )
    3. del list [ Subscript ] : Delete the element with the specified subscript in the list
    4. list .pop( Indexes ): Extract the elements of the specified index in the list
      list .pop(): Take out the last element
  4. Modification of list elements :
    list [ Subscript ] = The new element : Modify the list elements

    1. Assign a value to the element of the specified index
    2. Assign a value to the specified slice

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