Python 复合数据类型和数据结构是什么

Python 复合数据类型和数据结构是什么

在这篇文章中,我们将解释Python中的复合数据类型和数据结构是什么。

到目前为止,变量只存储了一个值。如果我们想保存许多相关的值呢?

我们可以简单地为每个值创建不同的变量。

但是如果我们不知道有多少个值存在呢?

如果我们希望在循环中使用这些值呢?

复合数据结构 是一种可以存储大量值的数据类型。

在Python中,有各种不同的类型的复合数据结构。

  • 我们主要会集中讨论 列表

  • 最后,我们会简要地了解一下 集合、元组和字典

列表

在Python中,一个列表是一个有序的序列,可以容纳多种对象类型,例如整数、字符或浮点数。在其他编程语言中,列表相当于数组。

列表只是一系列用逗号分隔且用方括号[]括起来的值。

inputList = [“hello”, “tutorialspoint”, 1, 3.5, “python”]

列表操作

有许多操作可以对列表进行,以便从它们创建表达式。

1) 使用len()函数获取列表的大小

使用 len()函数 获取列表的长度/大小(len()方法返回一个对象中的项数。当对象是一个列表时,len()函数返回列表中的项数),并创建一个变量来存储它。

示例

# input list
lst = ["Hello", "TutorialsPoint", 78, "Hi", "Everyone"]

# getting list length
listLength = len(lst)

# Printing the size of a list
print("Size of a List = ", listLength)

输出

('Size of a List = ', 5)

访问列表元素使用索引

术语” 索引 “指的是基于元素在可迭代对象内的位置。

索引从0开始。序列中的第一个元素由索引0表示。

负索引从-1开始。序列中的最后一个元素由索引-1表示。

示例

# input list
inputList =[1, 4, 8, 6, 2]

# accessing the list element at index 2 using positive indexing
print("Element at index 2:", inputList[2])

# accessing the last element in list using negative indexing
print("last element of an input list:", inputList[-1])

输出

('Element at index 2:', 8)
('last element of an input list:', 2)

注意

当我们尝试使用不存在或太大的索引时,它会抛出一个 索引错误

遍历列表

使用for循环

以下程序使用 for循环 打印出所有列表元素。

# input list
inputList = [10, 20, 30, 40, 50]

print("Input list elements:")
# traversing through all elements of the list using for loop
for element in inputList:
   # printing each element of the list
   print(element)

输出

Input list elements:
10
20
30
40
50

列表项上的重复运算符(*)

Python列表还包括*运算符,它允许您使用指定的次数重复元素创建一个新列表。

示例

以下程序使用*运算符将列表重复给定的次数 –

# input list
inputList = [5, 6, 7]

# Repeating the input list 2 times using the * operator
print(inputList * 2)

输出

[5, 6, 7, 5, 6, 7]

这里,我们使用*运算符将随机数值列表乘以两次,这样输出就是给定列表重复两次。

Python中的元组

元组是一种不可变的序列数据类型,可以包含各种数据类型的元素。元组只是由逗号分隔的Python对象的集合。由于元组的静态性质,元组比列表更快。

列表和元组的语法有些不同。列表由方括号[]表示,而元组由括号()表示。

元组切片

我们可以使用元组切片。它类似于我们使用字符串和列表的方式。元组切片用于获取各种项目。我们还使用切片操作符执行元组切片。切片操作符可以由语法表示

[start:stop:step]

示例

# Input tuple
givenTuple = ("Welcome", "this", "is", "TutorialsPoint", "Website", 10)

# Slicing with start and stop values(indices)
print('Tuple slicing from index 1 to index 6 :', givenTuple[1:6])

# Slicing with only stop values(indices)
print("Tuple slicing till index 7: ", givenTuple[:7])

输出

Tuple slicing from index 1 to index 6 : ('this', 'is', 'TutorialsPoint', 'Website', 10)
Tuple slicing till index 7: ('Welcome', 'this', 'is', 'TutorialsPoint', 'Website', 10)

使用索引访问元组元素

和列表一样,元组也使用索引来访问它的元素。唯一的区别是元组是不可变的(不能改变),而列表是可变的。

示例

# input tuple
inputTuple = (1, 4, 8, 6, 2)

# accessing the tuple element at index 2 using positive indexing
print("Element at index 2:", inputTuple[2])

# accessing the last element in tuple using negative indexing
print("last element of an input tuple:", inputTuple[-1])

输出

('Element at index 2:', 8)
('last element of an input tuple:', 2)

注意

当我们尝试使用不存在或者太大的索引时,会抛出一个 IndexError

Python中的字典

使用dict.keys()方法获取字典中所有键的列表

将输入字典应用到 keys() 函数,并将结果转换为一个列表,使用 list() 函数(将序列/可迭代对象转换为列表)打印出字典的所有键的列表。

示例

# input dictionary
demoDictionary = {10: 'TutorialsPoint', 12: 'Python', 14: 'Codes'}

# Printing the list of keys of a dictionary using the keys() function

# list() methods convert an iterable into a list
print(list(demoDictionary.keys()))

输出

[10, 12, 14]

结论

在本文中,我们学习了复合数据类型和数据结构,以及它们的一些示例。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程