List of python foundation summary

Keywords: Python Back-end

1, Format of list

A list is an element wrapped in a pair of brackets, called a list. For variable data types, each data in the list is called an element. You can use subscripts to get and slice elements.
1. Define the format of the list: [element 1, element 2,..., element n]
2,t = [ ]
3. T = list (iteratable object)
The elements in the list can be different data types

2, List operation

2.1 added elements

The append method adds an element at the end of the list. The address of the list remains unchanged before and after the list is added or deleted.

2.1.1 append method

append source code explanation:

    def append(self, *args, **kwargs): # real signature unknown
        """
        Append a single item to the end of the bytearray.
        
          item
            The item to be appended.
        """
        pass

Example code:

heros = ['Acor ', 'Ying Zheng', 'Han Xin', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang']
print(id(heros))
# append adds an element and appends an element to the last side of the list
heros.append('Huang Zhong')
print(heros)
print(id(heros))

# Output results:
['Acor ', 'Ying Zheng', 'Han Xin', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang']
1806544791560
['Acor ', 'Ying Zheng', 'Han Xin', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang', 'Huang Zhong']
1806544791560

2.1.2 insert method

The insert method inserts an element at any position in the list. When the insert inserts an element at the end, the address remains unchanged. When an element is inserted or deleted in another location, the address of the list will change, so it is generally not recommended.

insert source code explanation:

   def insert(self, *args, **kwargs): # real signature unknown
        """
        Insert a single item into the bytearray before the given index.
        
          index
            The index where the value is to be inserted.
          item
            The item to be inserted.
        """
        pass

Example code:

# insert adds an element, index represents a subscript, inserts data at that position, object represents an object, and the specific inserted data
heros.insert(3, 'Li Bai')
print(heros)
print(id(heros))

# Output results:
['Acor ', 'Ying Zheng', 'Han Xin', 'Li Bai', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang', 'Huang Zhong']
2044774311880

2.1.3 extend method

The extend method adds an iteratable object to the end of the list
extend source code explanation:

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend list by appending elements from the iterable. """
        pass

Example code:

# An iteratable object is required to add an element to extend
x = ['Mark pineapple', 'Milady ', 'Di Renjie']
heros.extend(x)
print(heros)
print(id(heros))

# Output results:
['Acor ', 'Ying Zheng', 'Han Xin', 'Li Bai', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang', 'Huang Zhong', 'Mark pineapple', 'Milady ', 'Di Renjie']
2876149655560

2.2 deleting elements

2.2.1 pop method

pop method source code explanation:

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return a single item from B.
        
          index
            The index from where to remove the item.
            -1 (the default value) means remove the last item.
        
        If no index argument is given, will pop the last item.
        """
        pass

The pop method deletes the last element by default and returns the deleted element. If the list is empty or the index is out of range, an index error message will be thrown

# The pop method deletes the last element by default and returns the value of the deleted element
pop_list = heros.pop()
print(pop_list)

# Output results:
Acor 

# You can also specify the index of the element to be deleted
pop_list = heros.pop(1)
print(pop_list)

# Output results:
Milady 

2.2.2 remove method

The parameter of the remove method is the value to be deleted
Example code:

# The parameter of the remove method is the value to be deleted
heros.remove("Luna")
print(heros)

# Output results:
['Di Renjie', 'Mark pineapple', 'Huang Zhong', 'Li Yuanfang', 'Arthur', 'Hou Yi', 'Li Bai', 'Han Xin', 'Ying Zheng', 'Acor ']

2.2.3 clear method

The clear method is to clear all elements in the list. After clearing, the list becomes an empty list.

# clear method
heros.clear()
print(heros)

# Output results:
[]

2.3 modifying elements

To modify an element, you only need to make the corresponding subscript equal to the new value

heros[1] = 'Zhang San'
print(heros)

# Output results:
['Acor ', 'Zhang San', 'Han Xin', 'Luna', 'Hou Yi', 'Arthur', 'Li Yuanfang', 'Hou Yi']

2.4 finding elements

2.4.1 finding elements by index

print(heros[1])

# Output results:
Ying Zheng

2.4.2 index method

Index method: returns the index of the search element. If the element does not exist, an error is reported

# Index method: returns the index of the search element. If the element does not exist, an error is reported
print(heros.index('Han Xin'))

# Output results:
2

2.5 reverse list

The reverse method reverses the list

heros.reverse()
print(heros)

# Output results:
['Hou Yi', 'Li Yuanfang', 'Arthur', 'Hou Yi', 'Luna', 'Han Xin', 'Ying Zheng', 'Acor ']

2.6 list sorting

2.6.1 sort method

The sort method changes the list to produce a new list

heros.sort()
print(heros)

2.6.2 sorted method

sorted method is a global sorting method of Python. It will generate a new list, and the original list will remain unchanged

sorted_list = sorted(heros)
print(sorted_list)

Posted by webmasternovis on Thu, 11 Nov 2021 17:58:33 -0800