卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

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

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

在本文中,我们将解释Python中的复合数据类型和数据结构。

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

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

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

如果我们希望在循环中使用这些值,该怎么办?

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

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

  • 我们将主要集中在列表上。

  • 最后,我们将快速了解Sets, Tuples, and Dictionaries

列表

在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)

使用索引访问列表元素

术语 "indexing" 指的是根据元素在可迭代对象中的位置来获取元素。

索引从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)

注意

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

迭代列表

使用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 List还包括*运算符,它允许你创建一个新的列表,其中的元素重复指定的次数。

示例

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

# 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]

Example

的中文翻译为:

示例

# 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() 函数(将序列/可迭代对象转换为列表)将结果转换为列表,以打印字典的所有键。

Example

的中文翻译为:

示例

# 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]

结论

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

卓越飞翔博客
上一篇: 如何使用Python对图片进行模糊背景处理
下一篇: Python和Gator AI之间的区别
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏