如何将Python列表转换为字典(附教程)

4,931 阅读4分钟

列表和字典是 Python 的集合,允许你在一个数据结构中保存类似的值。此外,列表将使你能够以特定的顺序保存数据,而字典是无序的,不能保存多个重复的值。

将 list 转换为 dictionary 是 Python 的标准操作。所以我们来看看如何做。

Python 列表到字典

要在 Python list转换为dictionary ,使用list 理解并创建一个连续元素的键值对,然后将一个 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 方法。

所以,我们在输出中得到了从 list 转换的 dictionary。

将一个列表转换为具有相同值的 dictionary

一个列表 转换具有 相同 dictionary,请使用dictionary comprehension 。每个键的值都将是相同的。

# 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

dictionary.fromKeys() 是一个内置的 Python 方法,它从一个给定的元素序列中创建一个新的 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 中把列表元素转换为带有索引键的 dictionary 中的值

在本文的第一个例子中,我们把 list 项作为键,把值作为 dictionary 中的一个整数。但在这个例子中,我们仍然是用索引键创建一个 dictionary,而值是使用 dictionary 理解的 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

要在 Python 中 两个 列表 转换为一个字典 ,使用zip()函数,它返回图元的列表,并把这个图元的列表传给dict()函数,然后它返回字典

假设我们有两个列表。

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

zip() 函数接受几个可迭代对象并返回图元的列表。

元组中的每个条目包含每个可迭代对象的一个元素。

我们在 zip() 函数中传递了两个列表对象,所以它将返回一个图元的列表,其中每个图元包含来自两个列表的一个条目。然后我们从这个图元列表中创建一个字典对象。

如果 keys 列表的长度小于 value 列表的长度,那么 value 列表中剩余的元素将被跳过。

在 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 列表到字典转换的所有变化。

本教程到此结束。