This is the 15th day of my participation in the More text Challenge. For more details, see more text Challenge


The introduction

Lists, the most frequently used data type in Python, are often called arrays in other languages


The list of defined

  • Used specifically to store a set of information
  • The list with[]Definition,dataUsed between.separated
  • A list ofThe index0start
    • An index is the number of positions in a list. An index is also called a subindex


name_list = ["hui"."zack"."wang"]
Copy the code



Note: The program will report an error if the value from the list is outside the index range


Common List Operations

  • inIPythonTo define aThe list of, such as:name_list = []
  • The inputname_list.Press theTABThe key,IpythonYou will be promptedThe list ofusablemethodsAs follows:


List Method Description

The serial number classification methods instructions
1 increase List.insert (index, data) Inserts data at the specified location
List.append (data) Append data at the end
List. Extend (Listing 2) Append the data from Listing 2 to the list
2 Modify the List [index] = data Modify the data of the specified index
3 delete Del list [index] Deletes data for the specified index
List. Remove [data] Deletes the first occurrence of the specified data
Pop () is list. Deleting the last data
List.pop (index) Delete the specified index data
List. The clear () Empty the list
4 statistical Len (list) The length of the list of
List.count (data) The number of times data appears in a list
5 The sorting List. The sort () Ascending order
List. Sort (reverse = True) Descending order
List. The reverse () Reverse, reverse


Add data to list – IPython quiz

In [2]: animal_list = ['cattle'.'the tiger'.'rabbit']

In [3]: animal_list
Out[3] : ['cattle'.'the tiger'.'rabbit']

In [4] :# insert specifies the location where data is inserted

In [5]: animal_list.insert(0.'rat')  # Insert into the list at position 0

In [6]: animal_list
Out[6] : ['rat'.'cattle'.'the tiger'.'rabbit']


In [7] :# append Appends data at the end

In [8]: animal_list.append('dragon')

In [9]: animal_list
Out[9] : ['rat'.'cattle'.'the tiger'.'rabbit'.'dragon']
    

In [10]: animal_list2 = ['snakes'.'the horse'.'sheep']

In [11] :# expend Appends other list data to the list

In [12]: animal_list.extend(animal_list2)

In [13]: animal_list
Out[13] : ['rat'.'cattle'.'the tiger'.'rabbit'.'dragon'.'snakes'.'the horse'.'sheep']
    
Copy the code


List modification data – IPython quiz

In [15]: name_list = ['hui'.'zhangsan'.'lisi']

In [16] :# Modify data by list index

In [17]: name_list[1] = 'zack'

In [18]: name_list
Out[18] : ['hui'.'zack'.'lisi']

In [19]: name_list[2] = 'wang'

In [20]: name_list
Out[20] : ['hui'.'zack'.'wang']

In [21] :Copy the code


List delete data – IPython quiz

In [21]: lang_list = ['python'.'java'.'c'.'c++'.'php']

In [22] :The # del keyword deletes the list data

In [23] :del lang_list[0]

In [24]: lang_list
Out[24] : ['java'.'c'.'c++'.'php']


In [25] :The # remove method removes list data

In [26]: lang_list.remove('java')

In [27]: lang_list
Out[27] : ['c'.'c++'.'php']


In [28] :The # pop method removes data at the end of the list

In [29]: lang_list.pop()
Out[29] :'php'

In [30]: lang_list
Out[30] : ['c'.'c++']


In [31] :# pop delete list index data

In [32]: lang_list.pop(1)
Out[32] :'c++'

In [33]: lang_list
Out[33] : ['c']


In [34] :The # clear method clears the list

In [35]: lang_list.clear()

In [36]: lang_list
Out[36]: []

In [37]: name_list = ['hui'.'zack'.'wang']

In [38]: name_list.clear()

In [39]: name_list
Out[39] : []Copy the code


List statistics – IPython quiz

In [41]: name_list = ['hui'.'wang'.'zack'.'hui'.'hui'.'wang']

In [42] :The # len method calculates the length of the list

In [43] :len(name_list)
Out[43] :6

    
In [44] :The # count method counts the number of times the data appears in the list

In [45]: name_list.count('hui')
Out[45] :3

In [46]: name_list.count('wang')
Out[46] :2

In [47]: name_list.count('zack')
Out[47] :1
Copy the code


List data sorting – IPython quiz

In [49]: num_list = [2.5.3.1.6.4.7.8]

In [50] :The sort method defaults to ascending sort

In [51]: num_list.sort()

In [52]: num_list
Out[52] : [1.2.3.4.5.6.7.8]
    

In [53] :# reverse=True # reverse=True

In [54]: num_list.sort(reverse=True)

In [55]: num_list
Out[55] : [8.7.6.5.4.3.2.1]
    

In [56] :# reverse = reverse

In [57]: num_list.reverse()

In [58]: num_list
Out[58] : [1.2.3.4.5.6.7.8]

In [59]: name_list = ['hui'.'wang'.'zack']

In [60]: name_list.reverse()

In [61]: name_list
Out[61] : ['zack'.'wang'.'hui']

In [62] :Copy the code


Loop through the list

Traversal is the process of retrieving data from the list from beginning to end


The while loop iterates

In [62]: name_list = ['hui'.'zack'.'wang']

In [63]: i = 0

In [64] :while i < len(name_list): ... :print(name_list[i]) ... : i = i +1. : hui zack wangCopy the code


The while loop determines the length of the list and retrieves data from the list index


The for loop iterates

  • inPythonIn order to improve the list of traversal efficiency, specifically providedIteration Iteration iteration iteration
  • useforYou can do iterative traversal
In [62]: name_list = ['hui'.'zack'.'wang']
    
In [65] :for name inname_list: ... :print(name) ... : hui zack wangCopy the code


Q: How does the for loop know where it has traversed?

A: You can use A counter variable, count

In [66]: name_list = ['hui'.'zack'.'wang']

In [67]: count = 0

In [68] :for name inname_list: ... :print(count) ... :print(name) ... : count = count +1. :0
hui
1
zack
2
wang
Copy the code


A: Enumeration can also be done through the built-in function enumerate()

In [69]: name_list = ['hui'.'zack'.'wang']

In [70] :for i, name in enumerate(name_list): ... :print(i) ... :print(name) ... :0
hui
1
zack
2
wang
Copy the code


Knowledge extension

Del Describes the del keyword

  • usedelKeyword (delete) Can also delete elements from a list
  • delKeywords are essentially used toTo remove a variable from memory
  • If you are usingdelThe keyword removes the variable from memory so that subsequent code cannot use it
name_list = ['hui'.'wang'.'zack']
del name_list[1]

# Delete entire list
del name_list
Copy the code


In daily development, to remove data from a list, it is recommended to use the method provided by the list


Keywords, functions, and methods

Keywords are identifiers built into Python that have a special meaning

In [1] :import keyword
In [2] :print(keyword.kwlist)
In [3] :print(len(keyword.kwlist))
Copy the code

You do not need to use parentheses after keywords


Functions encapsulate independent functions that can be called directly

def say_hello() :
    print('hello')
    
say_hello()

Copy the code

Functions have to be learned by rote


Methods are similar to functions in that they encapsulate individual functions

Methods need to be called through an object, representing the action to be done on that object

name_list = ['hui'.'zack'.'wang']

name_list.sort()
name_list.reverse()
name_list.pop()
name_list.clear()
Copy the code

Typing a. After an object and then choosing the action to perform on that object is much easier to remember than a function


The tail language

✍ Code is used to write the world and make life more interesting. ❤ ️

✍ the long march is always love, like to go. ❤ ️

✍ code word is not easy, but also hope that you support a lot of heroes. ❤ ️