Compression of two tuples in python
DescriptionPython tuple method cmp() compares elements of two tuples. Show SyntaxFollowing is the syntax for cmp() method − cmp(tuple1, tuple2) Parameters
Return ValueIf elements are of the same type, perform the compare and return the result. If elements are different types, check to see if they are numbers.
If we reached the end of one of the tuples, the longer tuple is "larger." If we exhaust both tuples and share the same data, the result is a tie, meaning that 0 is returned. ExampleThe following example shows the usage of cmp() method. #!/usr/bin/python tuple1, tuple2 = (123, 'xyz'), (456, 'abc') print cmp(tuple1, tuple2) print cmp(tuple2, tuple1) tuple3 = tuple2 + (786,); print cmp(tuple2, tuple3) When we run above program, it produces following result − -1 1 -1 python_tuples.htm Is there a way to reversibly compress a tuple of integers in Python? I have a large number of 5 digit tuples (values range from 0-100000) that I want to be able to store in a more memory efficient way but I also need to use their original values at a later point. If I had a tuple like this:
I'd like
to be able to compress it to a single integer value, similar to pythons default So something like:
Where
martineau 115k25 gold badges160 silver badges284 bronze badges asked Oct 23, 2021 at 20:09
8 This probably isn’t the fastest or smallest form of compressing but it’s prolly the easiest to follow. Get the lengths of all the integers and append that to the integers as a string:
Output:
Then to decompress, since all tuples contain 5 integers and the lengths
won’t be over 9 you know the first 5 characters will be the lengths, so just use that to unpack the integers again. I used
Output:
answered Oct 23, 2021 at 20:42
JabJab 25.8k21 gold badges74 silver badges113 bronze badges 2
A longer version that includes checking sizes and unwinding back to the original array is
Running I get
The compressed size depends on how random your integers are, but still, that ain't bad! answered Oct 23, 2021 at 21:38
tdelaneytdelaney 66k5 gold badges74 silver badges106 bronze badges Sometimes the obvious solution pops up after going through an answer. Since this is the best solution (IMHO), I'm posting it separately.... Just pickle it.
More complicated with data measurements...
Output
answered Oct 23, 2021 at 22:08
tdelaneytdelaney 66k5 gold badges74 silver badges106 bronze badges 2 Can we compare 2 tuples in Python?Practical Data Science using Python
Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.
Can you zip two tuples?12.5 Lists and tuples
zip is a built-in function that takes two or more sequences and “zips” them into a list of tuples where each tuple contains one element from each sequence. In Python 3, zip returns an iterator of tuples, but for most purposes, an iterator behaves like a list.
How do you merge two tuples in Python?Concatenation is done with the + operator, and multiplication is done with the * operator. Because the + operator can concatenate, it can be used to combine tuples to form a new tuple, though it cannot modify an existing tuple. The * operator can be used to multiply tuples.
Is there tuple comprehension in Python?There is no tuple comprehension in Python. Comprehension works by looping or iterating over items and assigning them into a container, a Tuple is unable to receive assignments.
|