Python - 列表
2023-09-17 22:15:54
在 Python 中,列表是一种可变序列类型。 列表对象在方括号 [] 中包含一个或多个不同数据类型的项目,以逗号分隔。 下面声明列表变量。
mylist=[] # empty list
print(mylist)
names=["Jeff", "Bill", "Steve", "Mohan"] # string list
print(names)
item=[1, "Jeff", "Computer", 75.50, True] # list with heterogeneous data
print(item)
列表可以包含无限的数据,具体取决于计算机内存的限制。
nums=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60]
可以使用方括号 [] 中的从零开始的索引访问列表项。
索引从零开始,每个项目递增 1。使用比列表总项目数更大的索引访问项目将导致IndexError
。
names=["Jeff", "Bill", "Steve", "Mohan"]
print(names[0]) # returns "Jeff"
print(names[1]) # returns "Bill"
print(names[2]) # returns "Steve"
print(names[3]) # returns "Mohan"
print(names[4]) # throws IndexError: list index out of range
一个列表可以包含多个内部列表作为可以使用索引访问的项目。
nums=[1, 2, 3, [4, 5, 6, [7, 8, [9]]], 10]
print(nums[0]) # returns 1
print(nums[1]) # returns 2
print(nums[3]) # returns [4, 5, 6, [7, 8, [9]]]
print(nums[4]) # returns 10
print(nums[3][0]) # returns 4
print(nums[3][3]) # returns [7, 8, [9]]
print(nums[3][3][0]) # returns 7
print(nums[3][3][2]) # returns [9]
列表类
所有列表对象都是 Python 中 list
类的对象。使用 list()
构造函数从其他序列类型(如元组、集合、字典、字符串)转换为列表。
nums=[1,2,3,4]
print(type(nums))
mylist=list('Hello')
print(mylist)
nums=list({1:'one',2:'two'})
print(nums)
nums=list((10, 20, 30))
print(nums)
nums=list({100, 200, 300})
print(nums)
迭代列表
可以使用for循环循环循环列表项。
names=["Jeff", "Bill", "Steve", "Mohan"]
for name in names:
print(name)
Jeff
Bill
Steve
Mohan
更新列表
该列表是可变的。可以使用 append()
或 insert()
方法在列表中添加新项目,并使用索引更新项目。
names=["Jeff", "Bill", "Steve", "Mohan"]
names[0]="Newton" # update 1st item at index 0
names[1]="Ram" # update 2nd item at index 1
names.append("Abdul") # adds new item at the end
print(names)
["Newton", "Ram", "Steve", "Mohan", "Abdul"]
请注意,如果指定索引处的元素不存在,则会抛出错误"索引超出范围"。
删除项目
使用 remove()
、pop()
方法或 del
关键字删除列表项或整个列表。
names=["Jeff", "Bill", "Steve", "Mohan"]
del names[0] # removes item at index 0
print("After del names[0]: ", names)
names.remove("Bill") # removes "Bill"
print("After names.remove("Bill"): ", names)
print(names.pop(0)) # return and removes item at index 0
print("After names.pop(0): ", names)
names.pop() # return removes item at last index
print("After names.pop(): ", names)
del names # removes entire list object
print(names) #error
After del names[0]: ["Bill", "Steve", "Mohan"]
After names.remove("Bill"): ["Steve", "Mohan"]
"Steve"
After names.pop(0):["Mohan"]
"Mohan"
After names.pop(): []
NameError: name 'names' is not defined
List operators
与字符串一样,列表也是一个序列。因此,与字符串一起使用的运算符也可用于列表(以及元组)。
运算符 | 例子 |
---|---|
The + operator returns a list containing all the elements of the first and the second list. |
|
The * operator concatenates multiple copies of the same list. |
|
The slice operator [] returns the item at the given index. A negative index counts the position from the right side. |
|
The range slice operator [FromIndex : Untill Index - 1] fetches items in the range specified by the two index operands separated by : symbol.
If the first operand is omitted, the range starts from the index 0. If the second operand is omitted, the range goes up to the end of the list. |
|
The in operator returns true if an item exists in the given list. |
|
The not in operator returns true if an item does not exist in the given list. |
|
列出方法
List Method | 描述 |
---|---|
list.append() | 在列表末尾添加一个新项。 |
list.clear() | 从列表中删除所有项目并将其设为空。 |
list.copy() | 返回列表的浅表副本。 |
list.count() | 返回元素在列表中出现的次数。 |
list.extend() | 将指定可迭代对象(列表、元组、集合、字典、字符串)的所有项添加到列表末尾。 |
list.index() | 返回指定项第一次出现的索引位置。如果未找到项目,则引发值错误。 |
list.insert() | 在给定位置插入项目。 |
list.pop() | 从指定的索引位置返回项,并将其从列表中删除。如果未指定索引,则 list.pop() 方法将删除并返回列表中的最后一项。 |
list.remove() | 从列表中删除指定项的第一个匹配项。如果未找到指定的项目,则会抛出值错误。 |
list.reverse() | 反转列表中元素的索引位置。第一个元素将位于最后一个索引,第二个元素将位于倒数第二个索引处,依此类推。 |
list.sort() | 按升序、降序或自定义顺序对列表项进行排序。 |