Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky
Define and use lists
In Python, a list is a data structure composed of a series of elements in a specific order, which means that list-type variables can store multiple data and can be repeated.
Define a list
-
Use [] literal syntax to define variables, multiple elements in a list are separated by commas, as shown in the following code:
List1 = [" Hello ", "a bowl of week", "Hello"] list2 = [1, 2, 3, 4, 5] print (list1) # [' Hello ', 'a bowl of weeks',' Hello '] print (list2) # [1, 2, 3, 4, 5)Copy the code
-
Programming lists of other sequences using Python’s built-in list is shown in the following example:
list1 = list(range(10)) list2 = list("hello") print(list1) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(list2) # ['h', 'e', 'l', 'l', 'o'] Copy the code
A list is a mutable data type, that is, the elements of a list can be changed. This is significantly different from a string, where changes to the string type return a new string
Access the values in the list
To access a value in a list, use a subscript index to access the value in the list, using square brackets to intercept the character as if it were a string:
List1 = [" Hello ", "a bowl of week", "Hello"] # list index of print (list1 [1]) # # list slice print a bowl weeks (list1 [1-3]) # [' a bowl of weeks', 'Hello']Copy the code
List operator
Lists, like strings, also support concatenation, repetition, and member operations. Example code is as follows:
list1 = ["Hello"] list2 = ["World"] list3 = [1, 2, 3, 4, 5] list4 = list(range(1, 6)) list5 = list1 + list2 # ['Hello', 'World'] print(list5) list6 = list1 * 3 # ['Hello', 'Hello', 'Hello'] list7 = list3 * 2 # [1, 2, 3, 4, 5, 1, 2, 3, 4, Print ("W" in list1) print("W" in list2) print("W" in list2 Print (list3 == list4) # True list8 = list(range(1, 7)) print(list3 == list8) # FalseCopy the code
Traversal of list elements
Traversing a list is the same as traversing a string, as shown in the following code:
List1 = ["H", "e", "l", "l", "o"] # print(list1[index]) # print(list1[index]) # print(list1[index]) # print(list1[index]) print(ch)Copy the code
List methods
Add and remove elements
Go straight to code
List1 = ["cute", "beautiful", "cute"] # append() print(list1) # ['cute', 'beautiful', Insert (2, "prefect") print(list1) # ['cute', 'beautiful', 'prefect', 'lovely', 'lovely'] # remove() delete the specified element list1.remove("lovely") print(list1) # ['cute', 'beautiful', 'prefect', Pop (2) print(list1) # ['cute', 'beautiful', Clear () print(list1) # []Copy the code
List elements can also be deleted in Python using the del keyword, similar to pop, in the example code ↓
List1 = ["cute", "beautiful", "sweet "] del list1[1] print(list1) # Del list1 print(list1) # NameError: name 'list1' is not definedCopy the code
Element positions and times
Use index() to find the location of an element, and count() to count the number of occurrences
List1 = ["beautiful", "cute", "beautiful", 'prefect', "beautiful", "Bowl week ", Print (list1.index("beautiful")) # 0 # Search for the last occurrence of "beautiful" from the fourth element Print (list1. Count ("beautiful")) # 3 print(list1. Count ("beautiful")) # 4 print(list1Copy the code
Element sorting and inversion
The sort() method can be used to sort list elements, while the reverse() method can be used to reverse elements, with example code ↓
List1 = ["cute", "cute"] list2 = list(range(10)) # print(list1) # [' cute', 'cute', Print ('cute', 'beautiful') print('cute', 'beautiful') print('cute', 'beautiful') print('cute', 'beautiful') 4, 3, 2, 1, 0] Copy () list3.sort() print(list2) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(list3) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]Copy the code
conclusion
methods | describe | parameter |
---|---|---|
append() |
Add a new object at the end of the list | Object added to the end of the list. |
insert() |
Inserts the specified object at the specified position in the list | Index – The index to which the object obj needs to be inserted. Obj — Objects to be inserted into the list. |
remove() |
The remove () function removes the first match of a value in a list. | Objects from the list to be removed. |
pop() |
The function removes an element from a list (the last element by default). | This parameter is optional. To remove the index value of a list element, the value cannot exceed the total length of the list. The default value is index=-1. |
clear() |
The list empties function | There is no |
index() |
The index position used to find the first match for a value from a list. | X — The object to look for. Start — Optional, the starting position to search. End – Optional, the end of the search. |
count() |
Counts the number of occurrences of an element in a list. | Objects counted in the list. |
sort() |
Use to sort the original list | Key — Primarily an element used for comparison Reverse — sorting rules,reverse = TrueIn descending order,reverse = FalseAscending order (default) |
reverse() |
Used to reverse elements in a list | There is no |
copy() |
For copying lists | There is no |
A list generator
Create a list of cartesian products for string 123 and string ABC as follows:
The original method
a = "123"
b = "ABC"
list1 = []
for x in a:
for y in b:
list1.append(x + y)
print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']
Copy the code
Generate column method
a = "123"
b = "ABC"
list1 = [x + y for x in a for y in b]
print(list1) # ['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']
Copy the code
Not only does this method have less code, but it also performs better than normal for loops and append appends
Nested lists
Because the variables in the list can store multiple data types, there is a list inside the list, called the list nesting, example code is as follows:
List1 = [[" express ", "beautiful", "a bowl of weeks"], "express", "beautiful", "a bowl of weeks"] print (list1 [0]) # [' express ', 'beautiful', Print (list1[0][0]) # cute # Print (list1[0]) # cuteCopy the code
The same is true no matter how much nesting there is