Issues to be aware of when going from list to sequence
(unfinished, catalog only)
the list
simple data type
container data type
- list<class 'list'>
- tuple<class 'tuple'>
- dictionary<class 'dict'>
- collection<class 'set'>
- string<class 'str'>
List definition and creation
A list is an ordered collection with no fixed size and can store any number of Python objects of any type. The syntax is [element 1, element 2, ..., element n]
- create a normal list
- Use range() to create a list
- Create lists using comprehensions
Note: x = [a] * 4
In the operation, only 4 references to the list are created, so once a
changed, x
4 of them a
will also change accordingly.
- create a mixed list
- create an empty list
Adding, deleting, and obtaining elements in the list
`
Add:
list.append(obj) 在列表末尾添加新的对象,只接受一个参数,参数可以是任何数据类型,被追加的元素在
The original structure type is maintained in list`.- Note the difference between
append()
andextend()
. list.extend(seq)
Append multiple values from another sequence at once to the end of a list (extending the original list with the new list)list.insert(index, obj)
Insert at numberedindex
positionobj
.
delete:
list.remove(obj)
removes the first occurrence of a value in a listlist.pop([index=-1])
Remove an element in the list (the last element by default), and return the value of the elementdel var1[, var2 ……]
Delete single or multiple objects.- If you delete an element from a list and don't use it in any way, use the del statement; if you want to continue using it after deleting an element, use a method
pop()
.
Get: Exercise Example
Example 1
x = ['Monday', 'Tuesday', 'Wednesday', ['Thursday', 'Friday']]
print(x[0], type(x[0])) # Monday <class 'str'>
print(x[-1], type(x[-1])) # ['Thursday', 'Friday'] <class 'list'>
print(x[-2], type(x[-2])) # Wednesday <class 'str'>
Example 2
x = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(x[3:]) # ['Thursday', 'Friday']
print(x[-3:]) # ['Wednesday', 'Thursday', 'Friday']
Example 3
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[:3]) # ['Monday', 'Tuesday', 'Wednesday']
print(week[:-3]) # ['Monday', 'Tuesday']
example 4
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[1:3]) # ['Tuesday', 'Wednesday']
print(week[-3:-1]) # ['Wednesday', 'Thursday']
example 5
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[1:4:2]) # ['Tuesday', 'Thursday']
print(week[:4:2]) # ['Monday', 'Wednesday']
print(week[1::2]) # ['Tuesday', 'Thursday']
print(week[::-1])
# ['Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday']
example 6
eek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
print(week[:])
# ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Example 7 deep and shallow copy
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]
list1 = [[123, 456], [789, 213]]
list2 = list1
list3 = list1[:]
print(list2) # [[123, 456], [789, 213]]
print(list3) # [[123, 456], [789, 213]]
list1[0][0] = 111
print(list2) # [[111, 456], [789, 213]]
print(list3) # [[111, 456], [789, 213]]
Common Operators for Lists
- Equal operator:
==
- join operator
+
- repeat operator
*
- membership operator
in
,not in
「等号 ==」
, returns only when the member and member position are the same True
.
There are two ways to splicing lists, using "plus sign +
" and "multiplication sign *
".
Other methods for lists
list.count(obj)
Count the number of times an element appears in a listlist.index(x[, start[, end]])
Find the index position of the first occurrence of a value in a listlist.reverse()
reverse list elementlist.sort(key=None, reverse=False)
Sort the original list.
In addition
key --
, it is mainly an element used for comparison, and has only one parameter. The parameter of the specific function is taken from the iterable object, and an element in the iterable object is specified for sorting.
reverse --
collation, reverse = True
descending, reverse = False
ascending (default).
This method has no return value, but sorts the objects in the list.
tuple
Create and access, update and delete a tuple
Operators related to tuples
built-in method
unpack tuple
string
Commonly used escape characters:
\\
backslash symbol
\'
, single quote,
\"
double quote,
\n
newline,
\t
horizontal tab (TAB),
\r
carriage return