Python中列表和字典的区别
列表就像用其他语言声明的数组一样。列表不必总是同质的,这使它成为Python中最强大的工具。单个列表可能包含数据类型,如整数、字符串以及对象。列表是可变的,因此即使在创建之后也可以更改。
例子:
Python3
# Python program to demonstrate
# Lists
# Creating a List with
# the use of multiple values
List = ["Geeks", "For", "Geeks"]
print("List containing multiple values: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'] , ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
Python3
# Python program to demonstrate
# dictionary
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("Dictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Python3
# Program to demonstrate
# space-time trade-off between
# dictionary and list
# To calculate the time
# difference
import time
# Creating a dictionary
d ={'john':1, 'alex':2}
x = time.time()
# Accessing elements
print("Accessing dictionary elements:")
for key in d:
print(d[key], end=" ")
y = time.time()
print("\nTime taken by dictionary:", y-x)
# Creating a List
c =[1, 2]
x = time.time()
print("\nAccessing List elements:")
for i in c:
print(i, end=" ")
y = time.time()
print("\nTime taken by dictionary:", y-x)
Python3
# Program to fetch particular
# elements of structure
import time
# Creating dictionary and list
dict_name ={"bob":12, "john":11}
list_name =[2, 3, 4, 5, 1]
# Time taken by dictionary
x = time.time()
L = dict_name["bob"]
y = time.time()
print("Time taken by dictionary:", y-x)
# Time taken by list
x = time.time()
L = list_name[2]
y = time.time()
print("\nTime taken by list:", y-x)
输出:
List containing multiple values:
Geeks
Geeks
Multi-Dimensional List:
[['Geeks', 'For'], ['Geeks']]
另一方面, Python中的字典是数据值的无序集合,用于像地图一样存储数据值,与其他仅将单个值作为元素保存的数据类型不同,字典包含键:值对。字典中提供了键值,使其更加优化。字典中的每个键值对都用冒号分隔:,而每个键都用“逗号”分隔。
例子:
Python3
# Python program to demonstrate
# dictionary
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("Dictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
输出:
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
Dictionary with the use of Mixed Keys:
{1: [1, 2, 3, 4], 'Name': 'Geeks'}
列表和字典的区别:
List Dictionary List is a collection of index values pairs as that of array in c++. Dictionary is a hashed structure of key and value pairs. List is created by placing elements in [ ] separated by commas “, “ Dictionary is created by placing elements in { } as “key”:”value”, each key value pair is separated by commas “, “ The indices of list are integers starting from 0. The keys of dictionary can be of any data type. The elements are accessed via indices. The elements are accessed via key-values. The order of the elements entered are maintained. There is no guarantee for maintaining order.
时空权衡
使用字典来查找元素更有效,因为在字典中遍历比列表花费的时间更少。
例如,让我们考虑一个依赖于数据检索速度的机器学习模型中包含 5000000 个元素的数据集。为了实现这一点,我们必须在两种数据结构(即列表和字典)之间做出明智的选择。首选字典是因为时间和空间存储更少,因为字典是从 python3.6 以哈希表的形式实现的,因此它永远不是字典中的时空权衡问题。
示例 1:
Python3
# Program to demonstrate
# space-time trade-off between
# dictionary and list
# To calculate the time
# difference
import time
# Creating a dictionary
d ={'john':1, 'alex':2}
x = time.time()
# Accessing elements
print("Accessing dictionary elements:")
for key in d:
print(d[key], end=" ")
y = time.time()
print("\nTime taken by dictionary:", y-x)
# Creating a List
c =[1, 2]
x = time.time()
print("\nAccessing List elements:")
for i in c:
print(i, end=" ")
y = time.time()
print("\nTime taken by dictionary:", y-x)
输出:
Accessing dictionary elements:
1 2
Time taken by dictionary: 1.0013580322265625e-05
Accessing List elements:
1 2
Time taken by dictionary: 3.5762786865234375e-06
示例 2:
Python3
# Program to fetch particular
# elements of structure
import time
# Creating dictionary and list
dict_name ={"bob":12, "john":11}
list_name =[2, 3, 4, 5, 1]
# Time taken by dictionary
x = time.time()
L = dict_name["bob"]
y = time.time()
print("Time taken by dictionary:", y-x)
# Time taken by list
x = time.time()
L = list_name[2]
y = time.time()
print("\nTime taken by list:", y-x)
输出:
Time taken by dictionary: 9.5367431640625e-07
Time taken by list: 4.76837158203125e-07
注意:获取列表中的单个元素比获取字典花费更多时间,因为字典使用哈希表来实现排列。