I want to convert tuple like
t = [(4,10),(9,7),(11,2),(2,2)]
into 2D array like:
a = [[4,10],[9,7],[11,2],[2,2]]
I tried
a = []
for i in t:
a.append(np.asarray(i))
print a
is there any easier way?
Using a tuple to specify the size of the array in the first argument of the ones function creates a multidimensional array, in this case a two-dimensional array with the two elements of the tuple specifying the number of rows and columns, respectively.
In Python a multidimensional tuple or list is one that contains another tuple or list as one of its elements. For example, take this tuple, which is composed of three separate tuples.
Python Tuple is a collection of objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists which are mutable.
Yes, Any sequence that has an array-like structure can be passed to the np. array function.
Use a list
comprehension as follows:
>>> t = [(4,10),(9,7),(11,2),(2,2)]
>>> [list(item) for item in t]
[[4, 10], [9, 7], [11, 2], [2, 2]]
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