[Tutor] Convert structured 1D array to 2D array

Emile van Sebille emile at fenx.com
Sat Feb 27 17:41:19 EST 2016

On 2/26/2016 7:26 PM, Ek Esawi wrote:
> The result i am looking for is a 2D array (matrix) instead of 1D array
> of tuples. That's when i read a file using genfromtxt, it generated a 1D
> array of n tuples (like the example shown below); each tuple has one row
> from the original file. The file contains multiple data types and so i used
> genfromtxt to specify data types for each column.
> In another way, i just want to convert a 1D array of tuples to 2D array
> where each tuple is converted multiple columns. Here is an example: the
> output from genfromtxt is
> [(1, 2, 3), (‘a’, b’, ’c’), (‘12/12/2009’, ’2/4/2014’, ‘3/4/200’)]
> I want this be converted to:
> [[1,                         2,              3],
> [‘a’,                        b’,           ’c’],
> [‘12/12/2009’, ’2/4/2014’, ‘3/4/200’]]

Being that the white space in lists and tuples is irrelevant, this has 
simply converted the inner tuples to lists --

tuples = [(1,2,3),('a','b','c'),('12/12/2009','2/4/2014','3/4/200')]
lists =map(list,tuples)


More information about the Tutor mailing list