turn list of letters into an array of integers

seektime michael.j.krause at gmail.com
Thu Oct 25 00:27:28 EDT 2012


On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote:
> On Tue, Oct 23, 2012 at 10:23 PM, seektime <michael.j.krause at gmail.com> wrote:
> 
> > Here's some example code. The input is a list which is a "matrix" of letters:
> 
> >    a  b  a
> 
> >    b  b  a
> 
> >
> 
> > and I'd like to turn this into a Python array:
> 
> 
> 
> You mean a Python list. The datatype Python calls an `array` is very
> 
> different and relatively uncommonly used.
> 
> Although, confusingly, Python's lists are implemented using C arrays
> 
> rather than linked lists.
> 
> 
> 
> >   1 2 1
> 
> >   2 2 1
> 
> >
> 
> > so 1 replaces a, and 2 replaces b. Here's the code I have so far:
> 
> >
> 
> >>>> L=['a b a\n','b b a\n']
> 
> <snip>
> 
> >>>> seq
> 
> > '1 2 1\n 2 2 1\n'
> 
> >
> 
> > My question is how can I turn "seq" into a python array?
> 
> 
> 
> I'd say you're asking the wrong question. The better question is "Why
> 
> wasn't the result a list in the first place?". Many transformations
> 
> are cumbersome to express over just strings, which is why the first
> 
> job of most programs is to parse their input into a more convenient
> 
> structure that is suited to their main task(s).
> 
> 
> 
> This (along with some other improvements) leads to a better, somewhat
> 
> different program/algorithm:
> 
> 
> 
> letter2number = {'a': 1, 'b': 2}
> 
> with open("path/to/file.txt", "r") as f:
> 
>     result = [[letter2number[letter] for letter in
> 
> line.strip().split()] for line in f]
> 
> 
> 
> If it's safe to assume that the correspondence between the letters and
> 
> numbers isn't completely arbitrary, some further improvements are also
> 
> possible.
> 
> 
> 
> Some relevant docs:
> 
> http://docs.python.org/library/stdtypes.html#string-methods
> 
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions
> 
> 
> 
> Cheers,
> 
> Chris
> 
> 
> 
> P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
> 
> it is worth noting for future reference that the readlines() method is
> 
> considered somewhat deprecated.

Thanks to everyone lots of great comments are actionable suggestions. 

My intension is to used the numpy/scipy packages to solve the task at hand. I agree that there's no point in loading a file into a format which only needs to be converted right after loading. But I'm new to Python the f.readline(s) command, according to the 2.7.3 tutorial and manual, is pretty much all there is for file i/o. If, as you indicated, f.readlines() is deprecated then what should I use instead? I'm using ver. 2.6 on Linux (it's a bit dated, I know).



More information about the Python-list mailing list