Hi guys,

With Python, I am using genfromtxt (from numpy) to read in a text file into an array:

y = np.genfromtxt("1400list.txt", dtype=[('mystring','S20'),('myfloat','float')])
Which works okay, except it doesn't seem to read my 2 columns into a 2D array. I am getting:

[('string001', 123.0),('string002', 456.0),('string002', 789.0)]

But I think would like:

[['string001', 123.0],['string002', 456.0],['string002', 789.0]]

I basically want each piece of information as a separate element that I can then manipulate.

Thank you.

Recommended Answers

They are OK, just list of tuples intead of list of tuples. What kind of errror are you getting? Your data must be homogenous to produce a 2d numpy array: http://stackoverflow.com/questions/9534408/numpy-genfromtxt-produces-array-of-what-looks-like-tuples-not-a-2d-arraywhy

Jump to Post

All 4 Replies

Can't you do:

list_of_lists=[list(x) for x in list_of_tuples]

This should make a list of lists; however, as PyTony said, this step should not be necessary for implicit compatibility w/ numpy.

Even though the numpy array is shown as a list of tuples, you can change the items in the tuples. For example:

# test module numpy loadtxt()
# Python27 or Python32

import numpy as np
    # Python2
    from StringIO import StringIO
except ImportError:
    # Python3
    from io import StringIO

# csv type test data
data_str = '''\

# create a file object
data_file = StringIO(data_str)

#mytype = np.dtype([('mystring','S20'),('myfloat','float')])
# or more descriptive
mytype = {'names': ('person','weight'), 'formats': ('S20', 'f4')} 

print(mytype)  # test


arr = np.loadtxt(data_file, delimiter=',', dtype=mytype) 



print("look at parts of the array;")
monika_ix = list(arr['person']).index('Monika')


# even though the array is shown as a list of tuples
# you can change the items in the tuples
print("change a specific item like Monika's weight:")
arr['weight'][monika_ix] = 177.5
# show change

'''result >>>
{'names': ('person', 'weight'), 'formats': ('S20', 'f4')}
[('Fred', 223.0) ('Monika', 156.0) ('Larry', 189.5)]
<type 'numpy.ndarray'>
look at parts of the array;
[ 223.   156.   189.5]
['Fred' 'Monika' 'Larry']
change a specific item like Monika's weight:
[('Fred', 223.0) ('Monika', 177.5) ('Larry', 189.5)]

The tuple that shows up in a numpy array is not a true Python tuple. In other words you can change its values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.