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.

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
try:
    # Python2
    from StringIO import StringIO
except ImportError:
    # Python3
    from io import StringIO

# csv type test data
data_str = '''\
Fred,223.0
Monika,156.0
Larry,189.5
'''

# 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

print('-'*30)

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

print(arr)
print(type(arr))

print('-'*30)

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

print('-'*30)

# 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
print(arr)

'''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]
156.0
['Fred' 'Monika' 'Larry']
1
------------------------------
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 meeting, networking, learning, and sharing knowledge.