是一个有序的序列。有序,就意味着list和string一样,有index;可以通过index调用对应的元素(element)。另外还有一种结构叫做Tuple,用()表示,是一种不可变的list;因此,其应用形式与list完全相同,除了tuple的元素不可变之外(list可改变元素的函数tuple都不能用)。
# ‘tuple’object does not support item assessment
values = [32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65]
# index 0 1 2 3 4 5 6 7 8 9
根据list上述特性,我们就可以进行各种操作:
| []
| [elem1, elem2, ..., elemn] | Creates a new empty list or a list that contains the initial elements provided. |
|---|---|
| len(l) | Returns the number of elements in list l. |
| list(sequence) | Creates a new list containing all elements of the sequence. |
| values * num | Creates a new list by replicating the elements in the values list num times. |
| values + moreValues | Creates a new list by concatenating elements in both lists. |
| l[from : to] | Creates a sublist from a subsequence of elements in list l starting at position from and going through but not including the element at position to. Both from and to are optional. |
| sum(l) | Computes the sum of the values in list l. |
| min(l) max(l) | Returns the minimum or maximum value in list l. |
| l1 == l2 | Tests whether two lists have the same elements, in the same order. |
| l.pop() | |
| l.pop(position) | Removes the last element from the list or from the given position. All elements following the given position are moved up one place. |
| l.insert(position, element) | Inserts the element at the given position in the list. All elements at and following the given position are moved down. |
| l.append(element) | Appends the element to the end of the list. |
| l.index(element) | Returns the position of the given element in the list. The element must be in the list. |
| l.remove(element) | Removes the given element from the list and moves all elements following it up one position. |
| l.sort() | Sorts the elements in the list from smallest to largest. |
| (reverse = true) | sort参数,逆序排列 |
| (key = len) | sort参数,按长度排序 |
| l1.extend(l2) | 合并两个list |
# 一个一个来~
①print all the value in the list(两种方法)
for i in range(len(values)) :
print(i, values[i])
for element in values :
print(element)
②appending elements
friends = []
friends.append("Harry")
friends.append("Emily")
friends.append("Bob")
friends.append("Cari")
# friend = ["Harry", "Emily", "Bob", "Cari"]
③inserting an element
friends.insert(1, "Cindy")
friends.insert(5, "Bill")
# friend = ["Harry", "Cindy", "Emily", "Bob", "Cari", "Bill"]
④Finding an element
friends = ["Harry", "Emily", "Bob", "Cari", "Emily"]
if "Cindy" in friends:
print("She's a friend")
n = friends.index("Emily")
# n = 1,返回的是Emily第一次出现的位置;
# 如果想看下一个在哪
n2 = friends.index("Emily", n+1)
⑤removing an element
friends = ["Harry", "Cindy", "Emily", "Bob", "Cari", "Bill"]
friends.pop(1)
# friends = ["Harry", "Emily", "Bob", "Cari", "Bill"]
friends.remove("Cari")
# friends = ["Harry", "Emily", "Bob", "Bill"]
⑥Concatenation and replication
myFriends = ["Fritz", "Cindy"]
yourFriends = ["Lee", "Pat", "Phuong"]
ourFriends = myFriends + yourFriends
# ourFriends = ["Fritz", "Cindy", "Lee", "Pat", "Phuong"]
monthInQuarter = [1, 2, 3] * 4
# [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]