列表和字典是 Python 的集合,允许你在一个数据结构中保存类似的值。列表将使你能够以特定的顺序保存数据,而字典是无序的,不能保存多个重复的值。
将 list 转换为 dictionary 是 Python 的标准操作。所以我们来看看如何做。
Python 列表到字典
要将一个 Python list 转换为 dictionary,使用 list 理解,将连续的元素做成键值对,然后类型转换为 dictionary 类型。返回值从 list 转换为 dictionary数据类型。
请看下面的代码示例:
# app.py
def listToDict(lst):
op = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
return op
lst = ['m', 1, 'b', 2, 'k', 3]
print(listToDict(lst))
输出
➜ pyt python3 app.py
{'m': 1, 'b': 2, 'k': 3}
➜ pyt
在上面的例子中,我们使用for 循环来分离奇数和偶数元素,然后使用range()函数将奇数值组成 key,将值组成 dictionary。我们使用了dictionary comprehension 方法。
所以,我们在输出中得到了从列表转换的 dictionary。
将一个列表转换为具有相同值的字典
要将一个列表转换为具有相同值的字典,请使用 dictionary comprehension。 每个键的值都将是相同的,例如5.让我们看看如何做到这一点:
# app.py
def listToDict(lst):
op = { i : 5 for i in lst }
return op
lst = ['millie', 'caleb', 'finn', 'sadie', 'noah']
print(listToDict(lst))
请看输出结果:
➜ pyt python3 app.py
{'millie': 5, 'caleb': 5, 'finn': 5, 'sadie': 5, 'noah': 5}
➜ pyt
在上面的例子中,所有的 list 元素都变成了 key,而 5 是所有元素的值。
使用 dict.fromKeys() 将 Python 列表转换成 dict
Python DictionaryfromKeys() 方法从一个给定的元素序列中创建一个新的 dictionary,其值由用户提供。
fromKeys方法返回具有指定键和值的 dictionary。
我们从列表中创建字典,所以Python Dict fromKeys()可以从 Python List 中获得键值,并将它们转换为字典的键。我们把第二个参数传成 5,一个值,所以我们得到与上面程序相同的输出:
# app.py
def listToDict(lst):
op = dict.fromkeys(lst , 5)
return op
lst = ['millie', 'caleb', 'finn', 'sadie', 'noah']
print(listToDict(lst))
输出
➜ pyt python3 app.py
{'millie': 5, 'caleb': 5, 'finn': 5, 'sadie': 5, 'noah': 5}
➜ pyt
dict.fromKeys() 接受一个列表和默认值。它返回一个以列表中的项目为键的 dictionary。所有的 dictionary 项目都会有在fromkeys()中传递的相同值。
在 Python 中转换 List 项目为带有索引键的 dictionary 中的值
在本文的第一个例子中,我们用 list 项作为键,值作为 dictionary 的一个整数。然而,在这个例子中,我们正在创建一个带有索引键的 dictionary,而值是使用 dictionary comprehension 的 python list 项。
请看下面的代码:
# app.py
def listToDict(lst):
op = { i : lst[i] for i in range(0, len(lst) ) }
return op
lst = ['millie', 'caleb', 'finn', 'sadie', 'noah']
print(listToDict(lst))
输出
➜ pyt python3 app.py
{0: 'millie', 1: 'caleb', 2: 'finn', 3: 'sadie', 4: 'noah'}
➜ pyt
将两个列表转换为一个 dictionary
要将两个列表转换为一个 dictionary,首先使用 zip() 函数,它返回图元的列表,然后将这个图元的列表传给 dict() 函数,它返回 dictionary.
假设我们有两个列表。
lstStr = ['millie', 'caleb', 'finn', 'sadie', 'noah']
lstInt = [11, 21, 19, 29, 46]
我们将把第一个列表作为 dictionary 的键,第二个列表作为值。
为了实现这一点,从两个列表中创建一个 dictionary,我们可以使用Python zip()函数。
请看下面的完整代码:
# app.py
def listToDict(lstA, lstB):
zippedLst = zip(lstA, lstB)
op = dict(zippedLst)
return op
lstStr = ['millie', 'caleb', 'finn', 'sadie', 'noah']
lstInt = [11, 21, 19, 29, 46]
print(listToDict(lstStr, lstInt))
请看输出:
➜ pyt python3 app.py
{'millie': 11, 'caleb': 21, 'finn': 19, 'sadie': 29, 'noah': 46}
➜ pyt
tuple中的每个条目包含每个可迭代对象的一个元素。
我们在 zip() 中传递了两个列表对象,所以它将返回一个图元的列表,其中每个图元包含来自两个列表的一个条目。然后我们从这个图元列表中创建一个 dictionary 对象。
如果键列表的长度小于值列表的长度,那么值列表中的剩余元素将被跳过。
在 Python 中将图元列表转换为 dictionary
要在 Python 中将图元列表转换为 dictionary,请使用 dict() 方法并将图元列表传给 dict() 方法,它将返回 dictionary。
# app.py
def listToDict(lstTup):
op = dict(lstTup)
return op
lstTuple = [('millie', 11), ('caleb', 21),
('finn', 19), ('sadie', 29), ('noah', 46)]
print(listToDict(lstTuple))
输出
➜ pyt python3 app.py
{'millie': 11, 'caleb': 21, 'finn': 19, 'sadie': 29, 'noah': 46}
➜ pyt
我们可以直接将这个图元列表传递给 dictionary 构造函数。
第一列中的条目将成为 key,第二列中的条目将是新 dictionary 中的值。这样,最后我们就达到了从元组中获取字典的目的。
所以,我们已经涵盖了 Python list 到 dictionary 转换的几乎所有变化。