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

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

如何在Python中压缩不均匀的元组

如何在Python中压缩不均匀的元组

简介

在Python中,元组是广泛使用的根据需求存储和处理数据的方法之一。元组中涉及很多操作,其中数据根据问题陈述的要求进行预处理和转换。压缩操作是压缩不同元组的最常见和最广泛使用的操作之一。

在本文中,我们将讨论 Python 中不均匀元组的压缩、不均匀元组压缩的实际含义,以及通过代码解释执行相同操作的不同方法。本文将帮助人们了解压缩不均匀元组背后的核心思想,并帮助人们在必要时做同样的事情。

现在让我们首先讨论Python中压缩和Python中不均匀元组压缩的含义。

什么是非均匀元组压缩?

在Python中,zip或zipping这个词意味着我们正在将不同元组的元素相加,这意味着我们正在制作一对不同元组的元素并将其存储在单个公共元组中。

例如,如果我们有两个像这样的元组:

T1 = (1, 2, 3)

T2 = (“一”, “二”, “三”)

然后对这些元组进行压缩操作将给出以下输出:

T_Zip = ((, “一个”), (2, “两个”), (3, “三个”))

这里的不均匀元组是指两个元组的大小或长度不相同,即其中一个元组的大小比另一个元组小或大。对于具有相同大小或长度的元组来说,压缩操作是一项非常简单的任务,但是当压缩两个不同大小或不均匀的元组时,压缩操作就变得非常复杂。

但是,有一些方法可以用来压缩两个不均匀的元组。让我们一一讨论一下。

压缩不均匀元组

在 Python 中,我们主要使用三种方式来压缩不均匀元组。

  • 使用 For 循环和枚举

  • 使用列表理解

  • 使用 Numpy 库

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

我们可以使用 for 循环和枚举函数来压缩不均匀元组。它是执行此操作的最简单且有效的客户端方法之一。

# using for loop and enumerate 

# define the tuples
test_tup1 = (7, 8, 4, 5)
test_tup2 = (1, 5, 6)

# print the input tuples
print("The input  tuple 1 is : " + str(test_tup1))
print("The input  tuple 2 is : " + str(test_tup2))

res = []


# use for loop with enumerate 
for i, j in enumerate(test_tup1):
  res.append((j, test_tup2[i % len(test_tup2)]))

# Print the final resultant tuple after zipping tuple 1 and 2
print("The output zipped tuple from tuple 1 and 2 is : " + str(res))

正如我们在上面的代码中看到的,元组 1 和 2 被 () 拒绝,并且它们的大小或长度不同。

现在,for 循环与枚举一起使用,枚举附加元组 1 和元组 2 元素并以元组格式给出输出。

输出

以下代码的输出为:

The input tuple 1 is : (7, 8, 4, 5) 
The input tuple 2 is : (1, 5, 6) 
The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]

方法 2:使用列表理解

也可以使用列表理解来压缩两个不均匀元组。这里可以使用三元运算符。

# using list comprehension

# define the tuples 
tup1 = (7, 8, 4, 5)
tup2 = (1, 5, 6)

# print the input tuples 
print("The input tuple 1 is : " + str(tup1))
print("The input tuple 2 is : " + str(tup2))

# define if else conditions
res = [(tup1[i], tup2[i % len(tup2)])
  if len(tup1) > len(tup2)

  else (tup1[i % len(tup1)], tup2[i])

  # use for loop on tuple 1 and 2
  for i in range(max(len(tup1), len(tup2)))]

#Print the final resultant tuple after zipping tuple 1 and 2
print(" The output zipped tuple from tuple 1 and 2 is :" + str(res))

正如我们在上面的代码中看到的,定义了两个不同大小的元组,然后编写 if else 条件,其中首先检查元组的长度,最后的 for 循环附加两个元组并返回输出。

Output

以下代码的输出为:

The input tuple 1 is : (7, 8, 4, 5) 
The input tuple 2 is : (1, 5, 6) 
The output zipped tuple from tuple 1 and 2 is : [(7, 1), (8, 5), (4, 6), (5, 1)]

方法三:使用Numpy库

Numpy 是最广泛使用的用于对数据执行操作的库之一。这里使用数组格式的数据,我们几乎可以做任何事情,并使用 numpy 将数据转换为任何内容。

#using numpy module to zip the uneven tuples 

# Importing the numpy module
import numpy as np

# define the tuples
test_tup1 = (7, 8, 4, 5)
test_tup2 = (1, 5, 6)

# convert the tuples into array format
arr1 = np.array(test_tup1)
arr2 = np.array(test_tup2)

# use np.tile 
arr2_tiled = np.tile(arr2, (len(arr1) // len(arr2) + 1))[:len(arr1)]

#use column_stack on array 1 and tiled array 2 to zip the tuples 
res_arr = np.column_stack((arr1, arr2_tiled))

# convert the array output to the tuple
res = tuple(map(tuple, res_arr))

# Print the final resultant tuple after zipping tuple 1 and 2
print("The output zipped tuple from tuple 1 and 2 is : " + str(res))

正如我们在上面的代码中看到的,我们首先导入了 numpy 库,然后定义了两个不同大小的元组。

然后如上所述,numpy 库需要数组格式数据才能处理相同的数据,因此元组被传递到 np.array,后者将数据转换为数组格式。

一旦我们有了数组形式的元组,np.column_stack就被用来追加数组的元素,并且元组被压缩。

然后使用 tuple() 函数再次将最终数组转换为元组。

Output

以下代码的输出为:

The output zipped tuple from tuple 1 and 2 is : ((7, 1), (8, 5), (4, 6), (5, 1))

结论

在本文中,我们讨论了两个不均匀元组或两个不同大小(长度)元组的压缩操作。上面讨论的压缩不均匀元组的三种不同方法将帮助人们理解压缩操作,并帮助人们在必要时执行相同的操作。

卓越飞翔博客
上一篇: C++程序用于找到给定矩阵的迹和法线
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏