Python列表

清单 就像其他语言中声明的动态大小数组(C++中的向量和java中的ARARYLIST)。列表不一定总是同质的,这使得它成为了世界上最强大的工具 python 。单个列表可能包含整数、字符串以及对象等数据类型。列表是可变的,因此即使在创建之后也可以更改。

null

Python中的列表是有序的,并且有一个确定的计数。列表中的元素按照一定的顺序进行索引,列表的索引以0作为第一个索引。列表中的每个元素在列表中都有其明确的位置,这允许复制列表中的元素,每个元素都有其独特的位置和可信度。

注释- 列表是一个有用的工具,用于保存数据序列并进一步对其进行迭代。

目录:

创建列表

Python中的列表可以通过将序列放在方括号内[]来创建。不像 设置 ,列表不需要内置函数来创建列表。

注—— 与集合不同,列表可能包含可变元素。

Python3

# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print ( "Blank List: " )
print ( List )
# Creating a List of numbers
List = [ 10 , 20 , 14 ]
print ( "List of numbers: " )
print ( List )
# Creating a List of strings and accessing
# using index
List = [ "Geeks" , "For" , "Geeks" ]
print ( "List Items: " )
print ( List [ 0 ])
print ( List [ 2 ])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [[ 'Geeks' , 'For' ], [ 'Geeks' ]]
print ( "Multi-Dimensional List: " )
print ( List )


输出:

Blank List: []List of numbers: [10, 20, 14]List ItemsGeeksGeeksMulti-Dimensional List: [['Geeks', 'For'], ['Geeks']]

创建包含多个不同或重复元素的列表

列表可能包含具有不同位置的重复值,因此,在创建列表时,可以将多个不同或重复的值作为序列传递。

Python3

# Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [ 1 , 2 , 4 , 4 , 3 , 3 , 3 , 6 , 5 ]
print ( "List with the use of Numbers: " )
print ( List )
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]
print ( "List with the use of Mixed Values: " )
print ( List )


输出:

List with the use of Numbers: [1, 2, 4, 4, 3, 3, 3, 6, 5]List with the use of Mixed Values: [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

知道列表的大小

Python3

# Creating a List
List1 = []
print ( len (List1))
# Creating a List of numbers
List2 = [ 10 , 20 , 14 ]
print ( len (List2))


输出:

03

向列表中添加元素

使用append()方法

可以使用内置的 附加() 作用使用append()方法一次只能将一个元素添加到列表中,对于使用append()方法添加多个元素,使用循环。还可以使用append方法将元组添加到列表中,因为元组是不可变的。与集合不同,还可以使用append()方法将列表添加到现有列表中。

Python3

# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = []
print ( "Initial blank List: " )
print ( List )
# Addition of Elements
# in the List
List .append( 1 )
List .append( 2 )
List .append( 4 )
print ( "List after Addition of Three elements: " )
print ( List )
# Adding elements to the List
# using Iterator
for i in range ( 1 , 4 ):
List .append(i)
print ( "List after Addition of elements from 1-3: " )
print ( List )
# Adding Tuples to the List
List .append(( 5 , 6 ))
print ( "List after Addition of a Tuple: " )
print ( List )
# Addition of List to a List
List2 = [ 'For' , 'Geeks' ]
List .append(List2)
print ( "List after Addition of a List: " )
print ( List )


输出:

Initial blank List: []List after Addition of Three elements: [1, 2, 4]List after Addition of elements from 1-3: [1, 2, 4, 1, 2, 3]List after Addition of a Tuple: [1, 2, 4, 1, 2, 3, (5, 6)]List after Addition of a List: [1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]

使用insert()方法

append()方法仅适用于在列表末尾添加元素,对于在所需位置添加元素,使用insert()方法。与只接受一个参数的append()不同,insert()方法需要两个参数(位置、值)。

Python3

# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = [ 1 , 2 , 3 , 4 ]
print ( "Initial List: " )
print ( List )
# Addition of Element at
# specific Position
# (using Insert Method)
List .insert( 3 , 12 )
List .insert( 0 , 'Geeks' )
print ( "List after performing Insert Operation: " )
print ( List )


输出:

Initial List: [1, 2, 3, 4]List after performing Insert Operation: ['Geeks', 1, 2, 3, 12, 4]

使用extend()方法

除了append()和insert()方法之外,还有一种方法可以添加元素, 扩展() ,此方法用于在列表末尾同时添加多个元素。

注—— append()和extend() 方法只能在末尾添加元素。

Python3

# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = [ 1 , 2 , 3 , 4 ]
print ( "Initial List: " )
print ( List )
# Addition of multiple elements
# to the List at the end
# (using Extend Method)
List .extend([ 8 , 'Geeks' , 'Always' ])
print ( "List after performing Extend Operation: " )
print ( List )


输出:

Initial List: [1, 2, 3, 4]List after performing Extend Operation: [1, 2, 3, 4, 8, 'Geeks', 'Always']

访问列表中的元素

要访问列表项,请参阅索引号。使用索引运算符[]访问列表中的项目。索引必须是整数。使用嵌套索引访问嵌套列表。

Python3

# Python program to demonstrate
# accessing of element from list
# Creating a List with
# the use of multiple values
List = [ "Geeks" , "For" , "Geeks" ]
# accessing a element from the
# list using index number
print ( "Accessing a element from the list" )
print ( List [ 0 ])
print ( List [ 2 ])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [[ 'Geeks' , 'For' ], [ 'Geeks' ]]
# accessing an element from the
# Multi-Dimensional List using
# index number
print ( "Accessing a element from a Multi-Dimensional list" )
print ( List [ 0 ][ 1 ])
print ( List [ 1 ][ 0 ])


输出:

Accessing a element from the listGeeksGeeksAccessing a element from a Multi-Dimensional listForGeeks

负索引

在Python中,负序列索引表示数组末尾的位置。不必像List[len(List)-3]那样计算偏移量,只需写List[-3]就足够了。负索引表示从结尾开始,-1表示最后一项,-2表示最后一项,以此类推。

Python3

List = [ 1 , 2 , 'Geeks' , 4 , 'For' , 6 , 'Geeks' ]
# accessing an element using
# negative indexing
print ( "Accessing element using negative indexing" )
# print the last element of list
print ( List [ - 1 ])
# print the third last element of list
print ( List [ - 3 ])


输出:

Accessing element using negative indexingGeeksFor

从列表中删除元素

使用remove()方法

可以使用内置的 删除() 函数,但如果列表中不存在该元素,则会出现错误。 删除() 方法一次只移除一个元素,要移除一系列元素,使用迭代器。remove()方法删除指定的项。

注—— 列表中的Remove方法将只删除搜索元素的第一个匹配项。

Python3

# Python program to demonstrate
# Removal of elements in a List
# Creating a List
List = [ 1 , 2 , 3 , 4 , 5 , 6 ,
7 , 8 , 9 , 10 , 11 , 12 ]
print ( "Initial List: " )
print ( List )
# Removing elements from List
# using Remove() method
List .remove( 5 )
List .remove( 6 )
print ( "List after Removal of two elements: " )
print ( List )
# Removing elements from List
# using iterator method
for i in range ( 1 , 5 ):
List .remove(i)
print ( "List after Removing a range of elements: " )
print ( List )


输出:

Initial List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]List after Removal of two elements: [1, 2, 3, 4, 7, 8, 9, 10, 11, 12]List after Removing a range of elements: [7, 8, 9, 10, 11, 12]

使用pop()方法

Pop() 函数还可以用于从列表中删除并返回元素,但默认情况下,它只删除列表中的最后一个元素。要从列表的特定位置删除元素,元素的索引将作为参数传递给pop()方法。

Python3

List = [ 1 , 2 , 3 , 4 , 5 ]
# Removing element from the
# Set using the pop() method
List .pop()
print ( "List after popping an element: " )
print ( List )
# Removing element at a
# specific location from the
# Set using the pop() method
List .pop( 2 )
print ( "List after popping a specific element: " )
print ( List )


输出:

List after popping an element: [1, 2, 3, 4]List after popping a specific element: [1, 2, 4]

列表的切片

在Python列表中,有多种方法可以打印包含所有元素的整个列表,但要打印列表中特定范围的元素,我们使用 切片操作 .Slice操作使用冒号(:)对列表执行。要将元素从开始打印到某个范围,请使用[:Index],要从最终使用[:-Index]打印元素,要将元素从特定索引打印到最终使用[Index:],要打印某个范围内的元素,请使用[Start Index:end Index],要使用切片操作打印整个列表,请使用[:]。此外,要以相反的顺序打印整个列表,请使用[:-1]。

注—— 要从后端打印列表元素,请使用负索引。

python-list-slicing

Python3

# Python program to demonstrate
# Removal of elements in a List
# Creating a List
List = [ 'G' , 'E' , 'E' , 'K' , 'S' , 'F' ,
'O' , 'R' , 'G' , 'E' , 'E' , 'K' , 'S' ]
print ( "Initial List: " )
print ( List )
# Print elements of a range
# using Slice operation
Sliced_List = List [ 3 : 8 ]
print ( "Slicing elements in a range 3-8: " )
print (Sliced_List)
# Print elements from a
# pre-defined point to end
Sliced_List = List [ 5 :]
print ( "Elements sliced from 5th "
"element till the end: " )
print (Sliced_List)
# Printing elements from
# beginning till end
Sliced_List = List [:]
print ( "Printing all elements using slice operation: " )
print (Sliced_List)


输出:

Initial List: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Slicing elements in a range 3-8: ['K', 'S', 'F', 'O', 'R']Elements sliced from 5th element till the end: ['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Printing all elements using slice operation: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

负索引列表切片

Python3

# Creating a List
List = [ 'G' , 'E' , 'E' , 'K' , 'S' , 'F' ,
'O' , 'R' , 'G' , 'E' , 'E' , 'K' , 'S' ]
print ( "Initial List: " )
print ( List )
# Print elements from beginning
# to a pre-defined point using Slice
Sliced_List = List [: - 6 ]
print ( "Elements sliced till 6th element from last: " )
print (Sliced_List)
# Print elements of a range
# using negative index List slicing
Sliced_List = List [ - 6 : - 1 ]
print ( "Elements sliced from index -6 to -1" )
print (Sliced_List)
# Printing elements in reverse
# using Slice operation
Sliced_List = List [:: - 1 ]
print ( "Printing List in reverse: " )
print (Sliced_List)


输出:

Initial List: ['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']Elements sliced till 6th element from last: ['G', 'E', 'E', 'K', 'S', 'F', 'O']Elements sliced from index -6 to -1['R', 'G', 'E', 'E', 'K']Printing List in reverse: ['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']

列表理解

列表解析 用于从元组、字符串、数组、列表等其他ITerable创建新列表。 列表理解由包含表达式的括号组成,表达式与for循环一起对每个元素执行,以迭代每个元素。

语法:

newList=[oldList中元素的表达式(元素)如果条件为]

例子:

Python3

# Python program to demonstrate list
# comprehension in Python
# below list contains square of all
# odd numbers from range 1 to 10
odd_square = [x * * 2 for x in range ( 1 , 11 ) if x % 2 = = 1 ]
print (odd_square)


输出:

[1, 9, 25, 49, 81]

为了更好地理解,上述代码类似于——

Python3

# for understanding, above generation is same as,
odd_square = []
for x in range ( 1 , 11 ):
if x % 2 = = 1 :
odd_square.append(x * * 2 )
print (odd_square)


输出:

[1, 9, 25, 49, 81]

请参阅以下文章以获取有关列表理解的详细信息。

名单上的行动

列出方法

作用 描述
附加() 在列表末尾添加一个元素
扩展() 将列表中的所有元素添加到另一个列表中
插入() 在定义的索引处插入项
删除() 从列表中删除一项
Pop() 移除并返回给定索引处的元素
清除() 从列表中删除所有项目
索引() 返回第一个匹配项的索引
计数() 返回作为参数传递的项数的计数
排序() 按升序对列表中的项目进行排序
反向 颠倒列表中项目的顺序
复印件() 返回列表的副本

带有列表的内置函数

作用 描述
减少 将参数中传递的特定函数应用于所有列表元素,存储中间结果并仅返回最终求和值
sum() 总结列表中的数字
ord() 返回一个整数,该整数表示给定Unicode字符的Unicode代码点
cmp() 如果第一个列表“大于”第二个列表,则此函数返回1
麦克斯() 返回给定列表的最大元素
min() 返回给定列表的最小元素
全部() 如果all元素为true或列表为空,则返回true
任何() 如果列表中的任何元素为true,则返回true。如果列表为空,则返回false
len() 返回列表的长度或大小
列举 返回列表的枚举对象
积累 将参数中传递的特定函数应用于所有列表元素,返回包含中间结果的列表
过滤器() 测试列表中的每个元素是否为真
地图() 将给定函数应用于给定iterable的每个项后,返回结果列表
lambda() 此函数可以有任意数量的参数,但只能有一个表达式,该表达式将被计算并返回。

最近关于名单的文章

Python列表上的更多视频: Python列表–集合2

更多关于Python列表的信息-

有用的链接:

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享