0

I have read a file into the Python script using:

data=np.loadtxt('myfile')

Which gives a list of numbers of type 'numpy.ndarray', in the form:

print(data) = [1, 2, 3]

I need to convert this into a list of lists, each with a single-character string 'a' and one of the above values, i.e.:

[[a,1],
 [a,2],
 [a,3]]

(Note that 'a' does not differ between each of the lists, it remains as a string consisting simply of the letter 'a')

What is the fastest and most Pythonic way of doing this? I have attempted several different forms of list comprehension, but I often end up with lines of 'None' displayed. The result does not necessarily have to be of type 'numpy.ndarray', but it would be preferred.

Also, how could I extend this method to data that has been read in from the file already as a list of lists, i.e.:

data2=np.loadtxt('myfile2',delimiter=' ')
print(data2)= [[1,2],
               [3,4],
               [5,6]]

To give the result:

[[a,1,2],
 [a,3,4],
 [a,5,6]]

Thank you for the help!

4

1 回答 1

0

Maybe something like this:

>>> import numpy as np
>>> data = [1,2,3]
>>> a = np.empty([len(data),2], dtype=object)
>>> a
array([[None, None],
       [None, None],
       [None, None]], dtype=object)
>>> a[:,0]='a'
>>> a
array([[a, None],
       [a, None],
       [a, None]], dtype=object)
>>> a[:,1]=data
>>> a
array([[a, 1],
       [a, 2],
       [a, 3]], dtype=object)
>>> data2=np.array([[1,2],[3,4],[5,6]])
>>> data2
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> b = np.empty([len(data2),3],dtype=object)
>>> b
array([[None, None, None],
       [None, None, None],
       [None, None, None]], dtype=object)
>>> b[:,0]='a'
>>> b
array([[a, None, None],
       [a, None, None],
       [a, None, None]], dtype=object)
>>> b[:,1:]=data2
>>> b
array([[a, 1, 2],
       [a, 3, 4],
       [a, 5, 6]], dtype=object)

Edit: In response to comment by OP you can label the columns by doing this:

>>> data2=np.array([[1,2],[3,4],[5,6]])
>>> c = zip('a'*len(data2),data2[:,0],data2[:,1])
>>> c
[('a', 1, 2), ('a', 3, 4), ('a', 5, 6)]
>>> d = np.array(c,dtype=[('A', 'a1'),('Odd Numbers',int),('Even Numbers',int)])

>>> d
array([('a', 1, 2), ('a', 3, 4), ('a', 5, 6)],
      dtype=[('A', '|S1'), ('Odd Numbers', '<i4'), ('Even Numbers', '<i4')])
>>> d['Odd Numbers']
array([1, 3, 5])

I don't know much about it but the array d is a record array. You can find info at Structured Arrays (and Record Arrays). I had trouble with the dtype of the "A" column. If I put ('A', str) then my a "A" column was always empty, ''. After looking at Specifying and constructing data types I tried using ('A', 'a1') and it worked.

于 2013-06-30T22:22:03.193 回答