How can I convert at 3-Dimensinal tuple into an array
a = []
a.append((1,2,4))
a.append((2,3,4))
in a array like:
b = [1,2,4,2,3,4]
To convert Python tuple to array, use the np. asarray() function. The np. asarray() is a library function that converts input to an array.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Method #1 : Using set() + tuple() This is the most straight forward way to remove duplicates. In this, we convert the tuple to a set, removing duplicates and then converting it back again using tuple().
Using list comprehension:
>>> a = [] >>> a.append((1,2,4)) >>> a.append((2,3,4)) >>> [x for xs in a for x in xs] [1, 2, 4, 2, 3, 4]
Using itertools.chain.from_iterable
:
>>> import itertools >>> list(itertools.chain.from_iterable(a)) [1, 2, 4, 2, 3, 4]
The simple way, use extend method.
x = [] for item in a: x.extend(item)
If you mean array as in numpy array, you can also do:
a = []
a.append((1,2,4))
a.append((2,3,4))
a = np.array(a)
a.flatten()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With