Python 将不同大小的列表进行压缩

Python 将不同大小的列表进行压缩

在Python中,列表是存储数字或字符串值的常用方法之一。它们是可变的,通过使用方括号[]进行定义。这种类型的列表可以包含不同的元素,这些元素可以是不同的数据类型。有时候,为了数据预处理的目的,我们可能需要在Python中对不同的列表进行压缩。

在本文中,我们将讨论列表的压缩操作,以及如何使用不同的方法和技巧来压缩不同大小的列表。本文将帮助您了解列表的压缩操作,并帮助您在必要时执行相同的操作。

现在让我们开始讨论列表及其压缩操作。

压缩列表

我们知道列表是常用的一种存储元素的方法,可以在其中包含数字或字符串值。它们是一种可变类型,在处理Python时常用于处理数据集。

列表的压缩操作意味着我们实际上正在压缩两个不同的列表,或者简单地说,我们正在将两个不同列表的值进行配对。

为了澄清这个想法,让我们举个例子。假设我们有两个列表:

L1 = [1, 2, 3]

L2 = [‘One’, ‘Two’, ‘Three’]

正如我们可以看到的那样,我们有两个不同的列表,一旦我们对它们执行压缩操作,输出将是:

Zipped_List = [(1, ‘One’), (2, ‘Two’), (3, ‘Three’)]

现在让我们讨论在Python中压缩列表的用例。

压缩列表的应用

压缩两个不同大小的列表可能在许多场景中都有帮助。让我们讨论一下:

字典表示: 对两个不同的列表进行压缩操作可以帮助我们将列表创建或表示为一个字典。我们可以通过将一个列表作为字典的键,另一个列表作为字典的值来实现。

数据处理: 在某些情况下,为了继续进行任务,必须进行数据处理,需要一个公共列表,而不是这么多不同的列表。在这种情况下,压缩操作可能非常有帮助。

数据迭代: 当您想要迭代列表元素并对其进行一些操作时,可以使用压缩操作。

压缩列表

有许多方法可以用来压缩不同的列表,让我们讨论其中的一些。

方法1:使用for循环和enumerate

使用for循环和enumerate是压缩两个不同大小的列表的一种最简单的方法之一。

# Using For Loop with Enumerate
#1. Define two lists 2. Run a for loop with enumerate 3. Zip the lists

# define the two lists
list1 = [1,2,3,4,5,6]
list2 = [1, 5, 6]

# print the original lists
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# for i, j run a for loop with enumerate
# append the values with j
res = []
for i, j in enumerate(list1):
  res.append((j, list2[i % len(list2)]))

# print the zipped list
print ("The Zip List from List 1 and 2 is : " + str(res))

如我们在上面的代码中看到,我们输入了两个不同大小的列表排名列表1和列表2。 这里首先我们打印出原始列表,然后我们运行一个包含enumerate函数的for循环,它会将列表元素追加并同时压缩两个列表。 输出结果为:

The input list 1 is : [1, 2, 3, 4, 5, 6] 
The input list 2 is : [1, 5, 6] 
The Zip List from List 1 and 2 is : [(1, 1), (2, 5), (3, 6), (4, 1), (5, 5), (6, 6)]

方法2:使用Zip()方法

使用Zip()方法也可以帮助我们将两个不同大小的列表压缩在一起。在循环中我们可以使用这个特定的关键词。

# using Zip()

# define the list
num_list = [1, 2, 3, 4] # numerical list
str_list = ['one', 'two', 'three', 'four', 'none', 'none'] #string list

# zip the lists with using zip()

zipped_list = [(num, s) for num, s in zip(num_list, str_list)]
print(zipped_list)

如我们在上面的代码中所看到的,我们有两个不同大小的列表,并且我们使用了zip()函数来添加列表元素并合并这些列表。

输出

以下代码的输出将是:

[(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

方法3:使用Itertools

这是将两个不同大小的列表合并的经典方法之一。在这里,我们将使用Itertools来合并这些列表。

# using the itertools 

# itertools + cycle

# import the cycle from itertools
from itertools import cycle

# define two different lists
list1 = [1,2,3,4,5,6,7]
list2 = [10, 50, 21]

# print the list1 and list2
print ("The input list 1 is : " + str(list1))
print ("The input list 2 is : " + str(list2))

# now use the cycle imported from itertools 
res = list(zip(list1, cycle(list2))
      if len(list1) > len(list2) #check for conditions
      else zip(cycle(list1), list2))

# printing the zipped list
print ("The Zip List from List 1 and 2 is: " + str(res))

如我们在上述代码中所见,itertools库已被安装并且cycle从中导入。

然后,我们定义了两个不同大小的列表并打印出来。接下来,我们使用cycle函数将这两个列表进行zip操作。

输出

此代码的输出将是:

The input list 1 is : [1, 2, 3, 4, 5, 6, 7] 
The input list 2 is : [10, 50, 21] 
The Zip List from List 1 and 2 is : [(1, 10), (2, 50), (3, 21), (4, 10), (5, 50), (6, 21), (7, 10)]

结论

在本文中,我们讨论了列表,列表的压缩操作是什么,压缩操作的应用有哪些,以及如何在Python中对两个不同大小的列表进行压缩。

我们讨论了共计3种方法,可以用这些方法来压缩Python中的列表,可以根据问题陈述和要求使用其中任一方法来压缩列表。本文将帮助读者了解列表的压缩操作,并在需要时进行相同的操作。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程