Python 如何定义列表
在本文中,我们将向您展示如何在Python中定义或创建一个列表。
什么是列表
在Python中,列表是一个有序的序列,可以容纳多种对象类型,如整数、字符或浮点数。在其他编程语言中,列表相当于一个数组。它们用方括号表示 [ ]。
列表不一定总是同质的,因此可以同时存储多个数据类型的对象。列表是最有用的工具。列表是一个Python容器数据结构,可以同时保存多个数据。当我们需要迭代某些元素同时仍然保持对元素的控制时,列表很有用。
创建/定义一个同质列表
示例
以下程序创建一个空列表、一个包含数字和字符串元素的列表,并返回它们:
# creating a list using empty square brackets
emptyList = []
print("Created empty list: ", emptyList)
# creating a list containing some random numbers
inputList = [1, 12, 5, 6, 9]
print("\nInput list containing some random numbers:")
print(inputList)
# creating a list containing some string elements
stringList = ["hello", "tutorialspoint", "python", "codes"]
print("\nInput list containing string elements:", stringList)
# accessing the first element of the list using positive indexing
print("First element of the list:", stringList[0])
# accessing the last element of the list using negative indexing
print("Last element of the list", stringList[-1])
输出
在执行上述程序时,将生成以下输出:
Created empty list: []
Input list containing some random numbers:
[1, 12, 5, 6, 9]
Input list containing string elements: ['hello', 'tutorialspoint', 'python', 'codes']
First element of the list: hello
Last element of the list codes
创建包含重复值的同质列表
示例
以下程序创建一个包含重复值的列表,并将它们返回 –
# creating a list containing some duplicate numbers(numbers that are repeated)
duplicateList = [4, 5, 5, 6, 6, 6, 7, 5, 4, 1, 2, 2, 1]
# printing the list containing duplicates
print("Printing the Input list containing some duplicate numbers:")
# traversing through each element of the list
for i in duplicateList:
# printing list element
print(i)
输出
执行上述程序后,将产生以下输出:
Printing the Input list containing some duplicate numbers:
4
5
5
6
6
6
7
5
4
1
2
2
1
创建一个异构列表
示例
以下程序创建一个包含重复值和混合数据类型元素的列表,并返回它们:
# creating a list containing mixed datatypes like integers, strings, floats
mixedList = [10, 'hello', 4, 'tutorialspoint', 6.567, 'python']
# printing the list containing mixed datatypes(Heterogenous List)
print("Input list containing mixed datatypes: ")
print(mixedList)
输出
执行上述程序将会生成以下输出结果:
Input list containing mixed datatypes:
[10, 'hello', 4, 'tutorialspoint', 6.567, 'python']
使用list()函数创建列表
在Python中,list()函数接受任何可迭代对象作为参数,并返回一个列表。可迭代对象是Python中可被迭代的对象。元组、字符串和列表都是可迭代对象的示例。
语法
list(iterable)
参数
iterable − 一个可以是序列(字符串、元组)或集合(集合、字典)的对象,或迭代器对象。
如果没有传递参数,list() 函数将返回一个没有元素的列表(空列表)。
示例
以下程序使用 list() 函数创建一个空列表 –
# creating a list using empty square brackets
emptyList = list()
# printing the above created empty list
print("Created empty list: ",emptyList)
输出
执行上述程序后,将产生以下输出-
Created empty list: []
创建多维列表(列表的列表)
在Python中,列表可以有多个附加维度。记住列表可以包含其他列表,这个基本概念可以反复使用。
列表内部的列表构成了一个多维列表。在Python中, 字典 通常比多维列表更好用。
示例
下面的程序创建了一个多维列表(列表的列表)并返回它:
# creating a multi-dimensional list
multidimensional_list = [[1, 3, 6, 2], [5, 7, 8, 10], [2, 4, 1, 6]]
# printing the multi-dimensional list
print("The created multi-dimensional list:")
# traversing through each element of the multi-dimensional list
for element in multidimensional_list:
# printing each list element
print(element)
输出
执行上述程序后,将生成以下输出-
The created multi-dimensional list:
[1, 3, 6, 2]
[5, 7, 8, 10]
[2, 4, 1, 6]
创建嵌套列表
一个包含另一个列表作为元素的列表被称为 嵌套列表 (子列表)。如果你需要将子列表与其他数据类型一起存储或者想创建一个矩阵,嵌套列表非常有用。
示例
下面的程序创建一个嵌套列表并返回:
# creating a nested list
nestedList = ['python','tutorialspoint',['code',[1,6,[2,4]],[3,5,8],20],['p','qr'],[12,[3,6]]]
# printing above created nested list
print("Created nested list:\n",nestedList)
输出
执行以上程序后,将生成以下输出 –
Created nested list:
['python', 'tutorialspoint', ['code', [1, 6, [2, 4]], [3, 5, 8], 20], ['p', 'qr'], [12, [3, 6]]]
结论
本文将向您展示使用两种不同的方法创建列表。在Python中,我们还学习了如何定义同质和异质列表。简而言之,我们学习了多维列表和嵌套列表,以及如何创建它们。