On 26 February 2016 at 16:46, Ek Esawi <esaw...@gmail.com> wrote: > > I used genfromtxt to read a file with multiple data types. The result was a > 1D array of tuples; for example > > [(1, 2, 3), (‘a’, b’, ’c’), (‘12/12/2009’, ’2/4/2014’, ‘3/4/200)’] > > > > I am trying to convert this structured array to a 2D array. Is this > possible where the data types in the 1D array are preserved?
Eke you need to be more careful in the way that you describe your problem. If you showed an example of the kind of file you're reading and the *exact* code that you used to read it then we would have a much better idea what the data structure is. You use the word "array" in a way that is essentially incorrect in Python. Python has many things that take the role of what would be "arrays" in a language like C. The most basic and common ones are lists and tuples. The data you show above is a list of tuples and not a "1D array". You say that you want a 2D array but actually it seems what you really wanted was a list of lists instead of a list of tuples. If you get your terminology right then you can easily search for the answers to questions like this. For example I typed "python convert list of tuples to list of lists" into google and the top hit was: http://stackoverflow.com/questions/14831830/convert-a-list-of-tuples-to-a-list-of-lists On that Stack Overflow page someone has asked the exact question that you have asked here and been given two different answers. One is the list_of_lists = map(list, list_of_tuples) answer presented by Emile which actually only works for Python 2. In Python 3 the equivalent is list_of_lists = list(map(list, list_of_tuples)) The other answer from Stack Overflow is list_of_lists = [list(t) for t in list_of_tuples] which works under Python 2 or 3. Please try carefully to learn the correct terminology and it will help you to ask the questions that you want to ask and to find the answers that you want. Finally I assume that what you sent was a small example of the kind of data that you have. It looks to me as if you have you data transposed from its natural ordering as a sequence of "records". If I change it to [(1, 'a', ‘12/12/2009’), (2, 'b', ’2/4/2014’), (3, 'c', '3/4/200')] then it looks like a more sensible data structure. This is now a list of tuples but where each tuple represents a "record" (that's not a Python type but rather a way of thinking about the data). You can store a data structure like this in numpy in a 1D record array (the word array is appropriate in when talking about "numpy arrays"). -- Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor