Python 字典压缩两个不等长度的列表

Python 字典压缩两个不等长度的列表

在Python中,列表和字典是最常用的数据收集和处理方法之一。有很多与列表和字典相关的操作常用于以所需的形式获取数据。有时候我们也需要将两个不同的列表压缩成字典形式。

本文将讨论如何压缩两个长度不等的列表,并将输出结果作为字典。本文将帮助你了解列表压缩操作的含义,并从中生成字典。

让我们开始我们关于压缩两个不等列表的讨论。

压缩两个不等长度的列表

在Python中,当收集和处理数据时,压缩是最常见的操作之一,它涉及以键-值对的方式添加两个列表。简单来说,它是将列表中的值或元素按照某种方式排序或表示,使其看起来像是输出结果中的键-值对。

这个操作是最常见的操作之一,因为有时候我们可能需要一个由两个不同列表组合而成的列表或字典。我们可以有两个不同大小或长度的列表,可以将其压缩并以字典形式输出,以便更轻松高效地处理数据。

有很多方法可以实现这个目标。让我们讨论其中的一些。

方法1:使用Itertools + Cycle

我们可以使用itertools库并导入cycle来压缩两个列表,并将结果作为字典输出。

# Itertools + Cycle Method 


# Import the cycle from itertools
from itertools import cycle

# define two lists
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = [1, 2, 3, 4]

# zip the lists and pass them into the dictionary form
res = dict(zip(list1, cycle(list2)))

# print the final results
print("Final Output Dictionary : ", str(res))

如上所示,我们首先从itertools中导入了cycle,然后定义了两个不同大小的列表。

然后使用itertools中的cycle将两个不等长的列表压缩成字典形式的输出。

输出

以下代码的输出将是:

Final Output Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 1}

方法2:使用deque

与itertools的cycle方法相同,我们可以使用collections模块中的deque方法。通过导入deque,我们可以将两个列表进行压缩,并得到一个字典。

# using deque for zipping lists

from collections import deque

# define two list that are to be zipped 
ini_lis1 = ['a', 'b', 'c', 'd', 'e']
ini_lis2 = deque([1, 2, 3, 4])

# zip teh lists using deque
result = {}
for letter in ini_lis1:
  number = ini_lis2.popleft()
  result[letter] = number
  ini_lis2.append(number)


# print the final results
print("Output Dict : ", str(result))

如我们在上面的代码中所看到的,在从collections导入deque之后定义了两个不同大小的列表。

然后使用for循环使用append将两个列表进行压缩。最后的结果将以字典的形式打印出来。

输出

这段代码的输出结果将是:

Output Dict : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 1}

方法3: 使用默认类

默认类也可以用于压缩两个不同大小的列表,并将字典作为输出。

# using default class method

# import default dict from collections
from collections import defaultdict

# define two lists
ini_lis1 = ['a', 'b', 'c', 'd', 'e']
ini_lis2 = [1, 2, 3, 4]

# use default dict
result = defaultdict(int)

# add values to the keys respectively
for i in range(len(ini_lis1)):
    result[ini_lis1[i]] += ini_lis2[i % len(ini_lis2)]

# print the final results
print("Output Dict: ", str(dict(result)))

如我们在上述代码中可以看到,在导入默认类之后定义了两个列表,并使用for循环将值添加到相应的键中。 在这里需要注意的是,如果键在数据中不存在,它将返回一个默认值。这里我们取默认值为0。 输出 以下代码的输出将是:

Output Dict: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 1}

方法4:使用Zip() + Dict()

这是一种最简单的方法,通过zip()函数将两个不同的列表合并,并将输出作为一个字典。

# using zip + dict method
# define two lists that are to be zipped
ini_lis1 = ['a', 'b', 'c', 'd', 'e']
ini_lis2 = [1, 2, 3, 4]

# use zip()
result = dict(zip(ini_lis1, ini_lis2 *
        ((len(ini_lis1) + len(ini_lis2) - 1) // len(ini_lis2))))

# print the final results
print("Output Dict: ", str(result))

从上面的代码中可以看到,我们首先定义了两个不同的列表,然后在定义结果时将语法或代码传入dict(),它将以字典数据格式返回一个输出。在这里使用zip关键字来合并两个列表的值。

输出

以下代码的输出将是:

Output Dict: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 1}

方法5:使用Itertools() + enumerate()

在这种方法中,我们将使用Itertools库,并在两个列表进行zip处理的过程中使用enumerate函数。

# using itertools + enumerate
# Import itertools
from itertools import cycle

# define two lists
ini_lis1 = ['a', 'b', 'c', 'd', 'e']
ini_lis2 = [1, 2, 3, 4]

# zip the two lists using for loop and enumerate
result = {v: ini_lis2[i % len(ini_lis2)]
    for i, v in enumerate(ini_lis1)}

# print the final results
print("Output Dict : ", str(result))

如上所示的代码中,我们首先从itertools导入cycle,然后定义两个不同大小的列表。然后使用for循环和enumerate函数,我们将两个不同列表的值或元素进行累加(压缩),然后这些值以字典的形式表示。

输出

以下代码的输出结果将是:

Output Dict : {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 1}

结论

在本文中,我们讨论了使用六种不同的方法在Python中对两个不同大小的列表进行压缩操作。本文将帮助读者在需要时执行类似的操作。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程