background
Two days ago, we studied together. Python Variables, Operators and Data Types And the most commonly used in programming Branch statement and loop statement.
Today, let's learn about one of the most common structures in Python: lists, and another similar structure: tuples.
code implementation
1. List
- Lists are ordered collections with no fixed size and can store any number of Python objects of any type.
Create lists
- Create a common list
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] print(week) # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] number = [2, 3, 4, 5, 6, 7] print(number) # [2, 3, 4, 5, 6, 7]
- Create a mixed list
mix = [1, 'lsgo', 3.14, [1, 2, 3]] print(mix) # [1, 'lsgo', 3.14, [1, 2, 3]]
- Create an empty list
empty = [] print(empty) # []
Adding elements to the list
- Appnd (obj) method: Add a new object at the end of the list
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] week.append('Thursday') print(week) # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday'] print(len(week)) # 6
- Extension (seq) method: Append multiple values in another sequence at the end of the list at once (expand the original list with a new list)
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] week.extend(['Thursday', 'Sunday']) print(week) # ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Thursday', 'Sunday']
- insert(index, obj) method:
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] week.insert(0, 'Sunday') print(week) # ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Get elements from the list
- Like arrays, we can get a single element from the list by the index value of the element. Note that the index value of the list starts at 0.
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] print(week[0]) # Monday
Delete elements from the list
- remove(obj) method: Remove the first match of a value in the list
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] week.remove('Monday') print(week) # ['Tuesday', 'Wednesday', 'Thursday', 'Friday']
- del var1[, var2... ] Statement: Delete one or more objects
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] del week[0], week[1] print(week) # ['Tuesday', 'Thursday', 'Friday']
- pop([index=-1]) method: Remove an element from the list (default last element) and return the value of that element
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] day = week.pop() print(day) # Friday day = week.pop(0) print(day) # Monday day = week.pop(-2) print(day) # Wednesday
List fragmentation
Example 1:
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] print(week[1:3]) # ['Tuesday', 'Wednesday'] print(week[:3]) # ['Monday', 'Tuesday', 'Wednesday'] print(week[3:]) # ['Thursday', 'Friday'] print(week[:]) # Copies of week ['Monday','Tuesday','Wednesday','Thursday','Friday']
Example 2:
list1 = [123, 456, 789, 213] list2 = list1 list3 = list1[:] print(list2) # [123, 456, 789, 213] print(list3) # [123, 456, 789, 213] list1.sort() print(list2) # [123, 213, 456, 789] print(list3) # [123, 456, 789, 213]
Common operators of lists
- Comparison operator
- Logical Operator
- Connection operator+
- Repeat operator*
- Membership operators in, not in
list1 = [123, 456] list2 = [234, 123] print(list1 > list2) # False list3 = [123, 456] print((list1 < list2) and (list1 == list3)) # True list4 = list1 + list2 # extend() print(list4) # [123, 456, 234, 123] list5 = list3 * 3 print(list5) # [123, 456, 123, 456, 123, 456] list3 *= 3 print(list3) # [123, 456, 123, 456, 123, 456] print(123 in list3) # True print(456 not in list3) # False
Other methods of listing
- count(obj) method: counting the number of times an element appears in a list
list1 = [123, 456] * 3 print(list1) # [123, 456, 123, 456, 123, 456] num = list1.count(123) print(num) # 3
- index(obj[, start[, end]]) method: Find the index location of the first match of a value from the list
list1 = [123, 456] * 5 print(list1.index(123)) # 0 print(list1.index(123, 1)) # 2 print(list1.index(123, 3, 7)) # 4
- reverse() method: elements in the reverse list
list1 = [123, 456, 789] list1.reverse() print(list1) # [789, 456, 123]
- sort(key=None, reverse=False) method: Sort the original list
list1 = [123, 456, 789, 213] list1.sort() print(list1) # [123, 213, 456, 789] list1.sort(reverse=True) print(list1) # [789, 456, 213, 123]
2. Tuples
Create and access a tuple
- Python tuples are similar to lists, except that tuples cannot be modified after they are created, similar to strings.
- Tuples are bracketed and lists are bracketed.
- Tuple creation is simple, just add elements in parentheses and use commas to separate them.
tuple1 = (1, 2, 3, 4, 5, 6, 7, 8) print(tuple1[1]) # 2 print(tuple1[5:]) # (6, 7, 8) print(tuple1[:5]) # (1, 2, 3, 4, 5) tuple2 = tuple1[:] print(tuple2) # (1, 2, 3, 4, 5, 6, 7, 8)
- When only one element is contained in a tuple, a comma is added after the element, otherwise parentheses are used as operators:
Example 1:
temp = (1) print(type(temp)) # <class 'int'> temp = 2, 3, 4, 5 print(type(temp)) # <class 'tuple'> temp = [] print(type(temp)) # <class 'list'> temp = () print(type(temp)) # <class 'tuple'> temp = (1,) print(type(temp)) # <class 'tuple'>
Example 2:
print(8 * (8)) # 64 print(8 * (8,)) # (8, 8, 8, 8, 8, 8, 8, 8)
Update and delete a tuple
Example 1:
week = ('Monday', 'Tuesday', 'Thursday', 'Friday') week = week[:2] + ('Wednesday',) + week[2:] print(week) # ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')
Example 2:
t1 = (1, 2, 3, [4, 5, 6]) print(t1) # (1, 2, 3, [4, 5, 6]) t1[3][0] = 9 print(t1) # (1, 2, 3, [9, 5, 6])
Tuple-related operators
- Comparison operator
- Logical Operator
- Connection operator+
- Repeat operator*
- Membership operators in, not in
t1 = (2, 3, 4, 5) t2 = ('Lao Ma's Procedural Life', 'Pony's Procedural Life') t3 = t1 + t2 print(t3) # (2, 3, 4, 5,'Lao Ma's Procedural Life','Pony's Procedural Life')
summary
So far, the list and tuples are covered. That's all for today! Come on, See You!
Relevant pictures and texts:
- Data Sharing: Data Sharing in Mathematical Modeling - Graph Theory
- Data Sharing: Data Sharing in Mathematical Modeling - Neural Network
- How to implement K-nearest neighbor algorithm with C#?
- How to use C# to implement K-D Tree structure?
- How to implement K-nearest neighbor algorithm with C# + KDTree?
- How to use C# to abstract the neural network model?
- How to implement the perceptron model of the neural network with C#?
- How to use C # to implement Delta learning rules?
- How to use C # to implement error back propagation learning rules?
- How to use C # to crawl website data with Token authentication?
- How to use C # to insert large amounts of data into Access database?
- How to use C+Python to crack the anti-reptile mechanism of cat-eye movies?