Python 如何压缩不均匀的元组
在Python中,元组是一种广泛使用的方法,根据需求存储和处理数据。元组中涉及许多操作,根据问题陈述的要求对数据进行预处理和转换。压缩操作是压缩不同元组最常见和广泛使用的操作之一。
在本文中,我们将讨论在Python中压缩不均匀元组的含义,以及使用不同方法进行相同操作的代码解释。本文将帮助人们理解压缩不均匀元组的核心思想,并在必要时进行相同操作。
因此,让我们从讨论Python中压缩的含义和压缩不均匀元组开始。
什么是压缩不均匀的元组
在Python中,压缩或压缩一词意味着我们将不同元组的元素相加,也就是说,我们将不同元组的元素配对并将其存储在一个共同的元组中。
例如,如果我们有两个这样的元组:
T1 = (1, 2, 3)
T2 = (“one”, “two”, “three”)
那么对这些元组进行压缩操作将得到以下输出:
T_Zip = ((1, “one”), (2, “two”), (3, “three”))
这里“不均匀的元组”意味着两个元组的大小或长度不相同,意味着其中一个元组的大小或长度比另一个元组小或大。当要压缩两个不同大小或不均匀的元组时,压缩操作非常简单。然而,有一些方法可以压缩两个不均匀的元组。让我们一一讨论。
压缩不均匀的元组
在Python中,主要有三种方法可以压缩不均匀的元组。
- 使用for循环和枚举函数
- 使用列表理解
- 使用Numpy库
方法1:使用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被()括起来,它们的大小或长度是不同的。
现在我们使用了带有enumerate的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循环将两个元组附加并返回输出。
输出
以下代码的输出将是:
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)]
方法3:使用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()函数。
输出
以下代码的输出结果将是:
The output zipped tuple from tuple 1 and 2 is : ((7, 1), (8, 5), (4, 6), (5, 1))
结论
在本文中,我们讨论了两个不均匀元组或两个不同大小(长度)元组的合并操作。上述讨论的三种不同方法可以帮助人们理解合并操作,并在必要时执行相同的操作。