Python basic learning 04

Keywords: Python Machine Learning AI Deep Learning

Creation and indexing of list objects  

The split method returns a list object ------ > ['h ',' LLO ']

In Python, list objects are represented by lists

List creation:

1.[ ]

l=[1,2,3]
l
[1,2,3]
type(l)
list

Essence of list: multi-element container ------------ > first, a list object can be composed of one or more objects. The objects constituting the list are called the elements of the list

The elements of the list can be one or more numeric values, a string (a single character must also be enclosed in double quotation marks or single quotation marks) or an empty list; The list can store different types of objects; Function or list

2. Use list to create a list: list is a function. The primary usage and the most common usage is to split a string into characters, and then output these characters in the form of a list.

list('hello')
['h', 'e', 'l', 'l', 'o']

3. Create a list using range: in practical work, it is often necessary to create a numerical list with a certain regular distribution. For example, [1, 2, 3], or [1, 3, 5], the range function will be used. It should be noted that the object created by the range function itself is not a list, and it needs to be further transformed with the above list function. l

#Range creates an object of type range
r=range(3)
type(r)
range
r
range(0, 3)
#Use the list function to convert it into a list object
list(r)
[0, 1, 2]
#Front closing and rear opening
list(range(2,8))
[2, 3, 4, 5, 6, 7]
list(range(2,8,2))
[2, 4, 6]
list(range(8,2,-2))
[8, 6, 4]

  Index and slice of list object (basically the same as string)

ls=list(range(6))
print(ls)
[0, 1, 2, 3, 4, 5]
#Index by index value
ls[1]#Second element
1
ls[-1]#Penultimate element
5
#Slice by index value
ls[1:3]#Front closing and rear opening
[1, 2]
ls[1:4:2]
[1, 3]
[1, 3]
[5, 3, 1]

Unlike a string, a string cannot be modified by an index, but a list can

ls[1]=9
print(ls)
[0, 9, 2, 3, 4, 5]

In Python, this internal immutable object is also called immutable object, and the internal element modifiable object is also called mutable object. String is immutable object, and list is mutable object.

Common methods of listing:

1. List operator

Lists also have operators. Using operators to modify lists is the simplest list operation method. Unlike numeric objects, list objects only support addition and multiplication operators.

***The plus operator simply combines the list elements to form a new list

l1=[1,2,3,4,5,6]
l2=[2,4,"Hi"]
l1+l2
[1, 2, 3, 4, 5, 6, 2, 4, 'Hi']

  The order of list elements remains unchanged after merging, and duplicate elements will be retained.

l2*2
[2, 4, 'Hi', 2, 4, 'Hi']

  Multiplying lists is equivalent to copying before splicing.

2.append method

The append method is mainly used to append elements to list objects.

l=list(range(3))
print(l)
[0, 1, 2]
l.append(3)
l
[0, 1, 2, 3]
#You can append any type of object
l.append([3,2,1])
l
[0, 1, 2, 3, [3, 2, 1]]
l.append(print)
print(l)
[0, 1, 2, 3, [3, 2, 1], <built-in function print>]

3.extend method  

The append method can only append one object at a time. To append multiple objects at a time, you need to use the extend method. When using the extend method, its parameter number is a list composed of multiple object elements to be added, that is, multiple objects to be appended must be installed in a list container, and then passed into the extend method.

l=list(range(4))
print(l)
[0, 1, 2, 3]
l.extend([1,2])
l
[0, 1, 2, 3, 1, 2]
# Of course, the extend function can append only one element, but even an element should be placed in a list container
l.extend([1])
print(l)
[0, 1, 2, 3, 1, 2, 1]
# When trying to append a list element, you need to use the nesting of the list
l.extend([[1, 2, 3]])
print(l)
[0, 1, 2, 3, 1, 2, 1, [1, 2, 3]]
# Like append, extend can append different types of objects
l.extend(["hello",1,2])
print(l)
[0, 1, 2, 3, 1, 2, 1, [1, 2, 3], 'hello', 1, 2]

4.insert method

Append and extend can only be appended at the end of the list. If you want to add elements at any position, you need to use the insert function. Like the append function, the insert function can only add one element at a time, and you still need the help of the index value when locating the position.

l=list(range(3))
print(l)
[0, 1, 2]
# The first parameter of the insert method refers to the index value of the inserted element, and the second element refers to the inserted element object
l.insert(1, [2, 3])
print(l)
[0, [2, 3], 1, 2]

5.pop method

Contrary to the above method of adding elements, the pop method is mainly used to delete the specified elements in the list.

l=list(range(5))
print(l)
[0, 1, 2, 3, 4]
# By default (without entering any parameters), pop deletes the last element in the list and returns it
l.pop()
4
l
[0, 1, 2, 3]
# The pop method can also delete the specified element in the list by entering the index value
l.pop(0)
0
l
[1, 2, 3]

6.remove method

pop method locates the object to be deleted through the index value, which is also called index value deletion. In addition, in practical applications, it is often necessary to delete according to the specific element content, and delete by specifying the deletion content. At this time, the remove method is needed.

l=list("helalo")
print(l)
['h', 'e', 'l', 'a', 'l', 'o']
# Deletes the h character object from the list
l.remove('h')
# Results after deletion
l
['e', 'l', 'a', 'l', 'o']
# Delete the character L, but note that l appears more than once. At this time, the remove function will delete the first occurrence of L
l=list("hellllollo")
l
['h', 'e', 'l', 'l', 'l', 'l', 'o', 'l', 'l', 'o']
l.remove('l')
l
['h', 'e', 'l', 'l', 'l', 'o', 'l', 'l', 'o']

7.del function

Del function is the most commonly used deletion function in Python, and it is not a unique method of the list. Using del function, you can directly delete some functions in the list according to the index slice, or delete the whole object.

l=list(range(5))
l
[0, 1, 2, 3, 4]
del l[2]
# Results after deletion
l
[0, 1, 3, 4]
# Delete a slice
del l[1:]
l
[0]
# Delete entire list object
del l
l
 report errors

8.clear method

To empty all the elements in the list object at once, use the clear method

l=list(range(5))
print(l)
[0, 1, 2, 3, 4]
# clear list 
l.clear()
l
# Return empty list
l
[]

9.index method

The index method of the list is mainly used to find the index value of the specified content.

l=list("hello")
print(l)
['h', 'e', 'l', 'l', 'o']
# Finds the index value of the character H in the list
l.index('h')
0
# When the lookup object appears more than once in the list, the element index value of the first occurrence is returned
l.index('l')
2
# The index method can also specify an index interval
l.index('l',3,5)
3

10.count method

The count method is used to count the number of occurrences of the specified element in the list.

l=list("hello")
print(l)
['h', 'e', 'l', 'l', 'o']
# Count the number of times the character l appears
l.count('l')
2

11.in function

The in function is used to determine whether an element appears in the list and return the result of a Boolean object.

l=list(range(4))
print(l)
[0, 1, 2, 3]
0 in l
True
4 in l
False

12.sort method

The sort method is mainly used for sorting lists, especially those composed of numerical objects.

l=[1,0,2,3]
print(l)
[1, 0, 2, 3]
# The sort method will be modified based on the original object
l.sort()
l
# View the modified results
l
[0, 1, 2, 3]
# The sort method has a reverse parameter. When the parameter value is True or 1, it will be arranged in descending order.
l.sort(reverse = True)
l
# View the results of the final descending arrangement
l
[3, 2, 1, 0]

13.reverse method

The reverse method is used to flip sort the element arrangement of the sequence

l=list("hello")
print(l)
['h', 'e', 'l', 'l', 'o']
l.reverse()
print(l)
['o', 'l', 'l', 'e', 'h']

Posted by jmaccs64 on Sat, 27 Nov 2021 18:05:13 -0800