[Tutor] create numpy array from list of strings

washakie washakie at gmail.com
Wed Jun 4 04:35:22 CEST 2008


Thank you again.

I can see your point about not wanting the tempDATA var hanging around, so I
guess this is better as a function. And yes, the transpose is for the 'next
step' - sorry if that cause anyone confusion.



Danny Yoo-3 wrote:
> 
> 
> If you already have the values as a list of rows, where each row is a list 
> of numbers, then the 'array' function may be appropriate.
> 
>      http://www.scipy.org/Cookbook/BuildingArrays
> 
> I see that you already know about array() from your second solution.
> 
> 

Yes, I printed that out today to have a closer look at!


"Danny Yoo-3 wrote:
> 
> Alhtough it's simple, if you can avoid I/O, do so: touching disk can raise 
> its own problems.  I like your second approach much better.
> 
> 

I know, besides, it certainly seems to slow things down - particularly with
large arrays / files.


"Danny Yoo-3 wrote:
> 
> 
>> 2) I've done the following, but I doubt it is the most efficient method:
>>
>>
>> Is there a better way?
> 
> This looks good.
> 
> 

Wow, really? I guess I assumed there would be a way to 'nest' list
comprehension somehow.


"Danny Yoo-3 wrote:
> 
> It's actually very easy to turn what you have there into a function.  I'd 
> recommend doing so.  Here's what it looks like.
> 
> #######################################################
> def convert(data):
>      tempDATA = []
>      for i in data:
>          tempDATA.append([float(j) for j in i.split()])
>      return array(tempDATA)
> #######################################################
> 
> To get back the same behavior as your code above, you can call this 
> function as:
> 
>      outdata = convert(data[stind:-1]).transpose()
> 
> Functions give you a way to bundle up a collection of related operations. 
> More importantly, that 'tempDATA' variable doesn't stick around hanging 
> out in space: it only lives in the convert() function.
> 


Good point!

Thanks again!
-- 
View this message in context: http://www.nabble.com/create-numpy-array-from-list-of-strings-tp17634509p17637945.html
Sent from the Python - tutor mailing list archive at Nabble.com.



More information about the Tutor mailing list