Home  >  Article  >  Backend Development  >  How to compress uneven tuples in Python

How to compress uneven tuples in Python

WBOY
WBOYforward
2023-08-30 11:09:121360browse

How to compress uneven tuples in Python

Introduction

In Python, tuples are one of the widely used methods to store and process data according to requirements. There are many operations involved in tuples, where the data is preprocessed and transformed as per the requirements of the problem statement. The compression operation is one of the most common and widely used operations for compressing different tuples.

In this article, we will discuss the compression of uneven tuples in Python, what uneven tuple compression actually means, and the different ways to perform the same operation through code explanation. This article will help people understand the core idea behind compressing uneven tuples and help people do the same if necessary.

Now let us first discuss the meaning of compression in Python and uneven tuple compression in Python.

What is non-uniform tuple compression?

In Python, the word zip or zipping means that we are adding the elements of different tuples, which means we are making a pair of elements of different tuples and storing them in a single common tuple.

For example, if we have two tuples like this:

T1 = (1, 2, 3)

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

The compression operation on these tuples will then give the following output:

T_Zip = ((, "one"), (2, "two"), (3, "three"))

The uneven tuple here means that the size or length of the two tuples is not the same, that is, the size of one tuple is smaller or larger than the other tuple. The compression operation is a very simple task for tuples of the same size or length, but becomes very complex when compressing two tuples of different sizes or unevenly.

However, there are ways to compress two uneven tuples. Let’s discuss them one by one.

Compressing uneven tuples

In Python, we mainly use three ways to compress uneven tuples.

  • Use For Loops and Enumerations

  • Use list comprehension

  • Using Numpy library

Method 1: Using For Loops and Enumerations

We can use for loops and enumeration functions to compress uneven tuples. It's one of the simplest and efficient client-side ways to do this.

# 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))

As we can see in the above code, tuples 1 and 2 are rejected by () and they are not the same size or length.

Now, for loop is used with enumeration which appends tuple1 and tuple2 elements and gives the output in tuple format.

Output

The output of the following code is:

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)]

Method 2: Use list comprehension

You can also use list comprehension to compress two uneven tuples. The ternary operator can be used here.

# 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))

As we can see in the above code, two tuples of different sizes are defined and then the if else condition is written where first the length of the tuple is checked and the last for loop appends the two tuples and returns the output .

Output

The output of the following code is:

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)]

Method 3: Use Numpy library

Numpy is one of the most widely used libraries for performing operations on data. Here using data in array format we can do almost anything and use numpy to convert the data to anything.

#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))

As we can see in the above code, we first imported the numpy library and then defined two tuples of different sizes.

Then as mentioned above, the numpy library requires array format data to handle the same data, so the tuple is passed to np.array which converts the data into array format.

Once we have the tuple as an array, np.column_stack is used to append the elements of the array, and the tuple is compressed.

Then use the tuple() function to convert the final array to a tuple again.

Output

The output of the following code is:

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

in conclusion

In this article, we discuss the compression operation of two uneven tuples or two tuples of different sizes (lengths). The three different ways of compressing uneven tuples discussed above will help one understand the compression operation and help one perform the same if necessary.

The above is the detailed content of How to compress uneven tuples in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete