卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章17340本站已运行3326

在Python中压缩不同大小的列表

在Python中压缩不同大小的列表

简介

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

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

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

列表压缩

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

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

为了阐明其背后的想法,让我们举个例子。假设我们有两个列表:

L1 = [1,2,3]

L2 = [‘一’、‘二’、‘三’]

正如我们在上面看到的,我们有两个不同的列表,一旦我们对其执行压缩操作,输出将是:

Zipped_List = [(1, ‘一’), (2, ‘二’), (3, ‘三’)]

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

压缩列表的应用

压缩两个相同大小或不同大小的不同列表可能在许多情况下有所帮助。让我们讨论一下:

字典表示:对两个不同列表的压缩操作可以帮助我们将列表创建或表示为字典。我们可以通过获取一个包含键的列表和包含字典值的其他列表来执行相同的操作。

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

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

压缩列表

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

方法 1:使用 For 循环和枚举

将 for 循环与枚举结合使用是压缩两个不同大小的列表的最简单方法之一。

'
# 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,它们的大小不同。

首先,我们打印原始列表,然后使用枚举函数运行 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')]

方法三:使用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库已经安装并且循环是从它导入的。

然后我们定义了两个不同大小的列表并打印了相同的列表。接下来,循环用于通过将两个列表传递到同一个列表来压缩列表。

输出

这段代码的输出是:

'
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 中压缩列表,并且任何人都可以根据问题陈述和要求来压缩列表。本文将帮助人们了解列表的压缩操作,并帮助人们在需要时执行相同的操作。

卓越飞翔博客
上一篇: 什么是 Kestral C# Asp.net Core?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏