📜  Python中List、Tuple、Set、Dictionary的区别及应用

📅  最后修改于: 2021-09-06 06:22:40             🧑  作者: Mango

列表:就像动态大小的数组,在其他语言中声明(C++ 中的向量和Java的ArrayList)。列表不必总是同质的,这使其成为Python最强大的工具。

元组:元组是由逗号分隔的Python对象的集合。在某些方面,元组在索引、嵌套对象和重复方面类似于列表,但与可变列表不同,元组是不可变的。

Set: Set 是一种无序的集合数据类型,它是可迭代的、可变的并且没有重复的元素。 Python 的集合类表示集合的数学概念。

字典: 在Python是一个无序的数据值集合,用于像映射一样存储数据值,与其他只保存单个值作为元素的数据类型不同,字典保存键:值对。字典中提供了键值以使其更加优化。

List、Tuple、Set 和 Dictionary 是Python中的数据结构,用于以高效的方式存储和组织数据。

List Tuple Set Dictionary
List is a non-homogeneous data structure which stores the elements in single row and multiple rows and columns Tuple is also a non-homogeneous data structure which stores single row and multiple rows and columns Set data structure is also non-homogeneous data structure but stores in single row Dictionary is also a non-homogeneous data structure which stores key value pairs
List can be represented by [ ]

Tuple can be represented by  

( )

Set can be represented by { } Dictionary  can be represented by { }
List allows duplicate elements Tuple allows duplicate elements Set will not allow duplicate elements Set will not allow duplicate elements but keys are not duplicated
List can use nested among all Tuple can use nested among all Set can use nested among all Dictonary can use nested among all
Example: [1, 2, 3, 4, 5] Example: (1, 2, 3, 4, 5) Example: {1, 2, 3, 4, 5} Example: {1, 2, 3, 4, 5}
List can be created using list() function Tuple can be created using tuple() function. Set can be created using set() function Dictonary can be created using dict() function.
List is mutable i.e we can make any changes in list. Tuple  is immutable i.e we can not make any changes in tuple Set is mutable i.e we can make any changes in set. But elements are not duplicated. Dictionary is mutable. But Keys are not duplicated.
List is ordered Tuple is ordered Set is unordered Dictionary is ordered

Creating an empty list

l=[]

Creating an empty Tuple

t=()

Creating a set

a=set()

b=set(a)

Creating an empty dictionary

d={}

下面是List、tuple、set、dictionary的实现程序:

Python3
# Python3 program for explaining
# use of list, tuple, set and 
# dictonary
  
# Lists
l = []
  
# Adding Element into list
l.append(5)
l.append(10)
print("Adding 5 and 10 in list", l)
  
# Popping Elements from list
l.pop()
print("Popped one element from list", l)
print()
  
# Set
s = set()
  
# Adding element into set
s.add(5)
s.add(10)
print("Adding 5 and 10 in set", s)
  
# Removing element from set
s.remove(5)
print("Removing 5 from set", s)
print()
  
# Tuple
t = tuple(l)
  
# Tuples are immutable
print("Tuple", t)
print()
  
# Dictonary
d = {}
  
# Adding the key value pair
d[5] = "Five"
d[10] = "Ten"
print("Dictonary", d)
  
# Removing key-value pair
del d[10]
print("Dictonary", d)


输出:
Adding 5 and 10 in list [5, 10]
Popped one element from list [5]

Adding 5 and 10 in set {10, 5}
Removing 5 from set {10}

Tuple (5, )

Dictonary {10: 'Ten', 5: 'Five'}
Dictonary {5: 'Five'}

列表、集合、元组和字典的应用

列表:

  • 以 JSON 格式使用
  • 对数组操作有用
  • 在数据库中使用

元组:

  • 用于通过SQL查询一次在数据库中插入记录
    例如:(1.’sravan’, 34).(2.’geek’, 35)
  • 在括号检查器中使用

  • 寻找独特的元素
  • 加入操作

字典

  • 用于创建带有列表的数据框
  • 在 JSON 中使用