Python 列表

在 Python 中,列表是可变的序列类型。列表对象在方括号[]中包含一个或多个不同数据类型的项,用逗号分隔。下面声明了 lists 变量。

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(names[0]) # returns 1
print(names[1]) # returns 2
print(names[3]) # returns [4, 5, 6, [7, 8, [9]]]
print(names[4]) # returns 10
print(names[3][0]) # returns 4
print(names[3][3]) # returns [7, 8, [9]]
print(nums[3][3][0]) # returns 7
print(nums[3][3][2]) # returns [9] 

Output:

1
2
[4, 5, 6, [7, 8, [9]]]
10
4
[7, 8, [9]]
7
[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) 

Output:

<class 'list'>
['H', 'e', 'l', 'l', 'o']
[1, 2]
[10, 20, 30]
[100, 200, 300] 

迭代列表

使用循环的可以迭代列表项。

names=["Jeff", "Bill", "Steve", "Mohan"] 

for name in names:
    print(name) 

Output:

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) 

Output:

["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) 

Output:

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 

列表运算符

和字符串一样,列表也是一个序列。因此,用于字符串的运算符也可用于列表(以及元组)。

操作员 例子
+ 运算符返回一个包含第一个和第二个列表所有元素的列表。
>>> L1=[1,2,3]
>>> L2=[4,5,6]
>>> L1+L2     
[1, 2, 3, 4, 5, 6]

| | ***** 运算符连接同一列表的多个副本。 |

>>> L1=[1,2,3]
>>> L1*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

| | 切片运算符 [] 返回给定索引处的项目。负索引从右侧开始计算位置。 |

>>> L1=[1, 2, 3]
>>> L1[0] 
1                  
>>> L1[-3]
1
>>> L1[1] 
2
>>> L1[-2]
2
>>> L1[2]
3
>>> L1[-1] 
3 

| | 范围切片运算符【从索引:直到索引-1】获取由两个索引操作数指定的范围内的项目,这两个操作数用:符号分隔。 如果省略第一个操作数,范围从索引 0 开始。 如果省略第二个操作数,范围将上升到列表的末尾。 |

>>> L1=[1, 2, 3, 4, 5, 6]
>>> L1[1:]
[2, 3, 4, 5, 6]
>>> L1[:3]
[1, 2, 3]
>>> L1[1:4]
[2, 3, 4]           
>>> L1[3:] 
[4, 5, 6]
>>> L1[:3]
[1, 2, 3]
>>> L1[-5:-3]
[2, 3]

| | 如果给定列表中存在某个项目,则运算符中的返回真。 |

>>> L1=[1, 2, 3, 4, 5, 6]
>>> 4 in L1     
True                     
>>> 10 in L1             
False

| | 如果给定列表中不存在某个项目,则不在运算符中的返回真。 |

>>> L1=[1, 2, 3, 4, 5, 6]
>>> 5 not in L1          
False            
>>> 10 not in L1         
True

|

列出方法

列表方法 描述
list.append() 在列表末尾添加一个新项目。
list.clear() 从列表中移除所有项目并使其为空。
list.copy() 返回列表的浅拷贝。
list.count() 返回一个元素在列表中出现的次数。
list.extend() 将指定表的所有项目(列表、元组、集合、字典、字符串)添加到列表的末尾。
list.index() 返回指定项第一次出现的索引位置。如果找不到项目,则引发值错误。
list.insert() 在给定位置插入项目。
list.pop() 从指定的索引位置返回一个项目,并将其从列表中移除。如果未指定索引,list.pop()方法将移除并返回列表中的最后一项。
list.remove() 从列表中删除第一个出现的指定项目。如果找不到指定的项目,则会引发 ValueError。
list.reverse() 反转列表中元素的索引位置。第一个元素将位于最后一个索引处,第二个元素将位于第二个最后一个索引处,以此类推。
list.sort() 按升序、降序或自定义顺序对列表项进行排序。

本文链接:https://my.lmcjl.com/post/7661.html

展开阅读全文

4 评论

留下您的评论.