Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > turn list of letters into an array of integers

Reply
Thread Tools

turn list of letters into an array of integers

 
 
seektime
Guest
Posts: n/a
 
      10-24-2012
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:

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']
>>> s=' '.join(L)
>>> seq1=('a','b')
>>> seq2=('1','2')
>>> d = dict(zip(seq1,seq2))
>>> # Define method to replace letters according to dictionary (got this from http://gomputor.wordpress.com/2008/0...s-with-python/).

.... def replace_all(text, dic):
.... for i, j in dic.iteritems():
.... text = text.replace(i, j)
.... return text
....

>>> seq = replace_all(s,d)
>>> print seq

1 2 1
2 2 1

>>> seq

'1 2 1\n 2 2 1\n'

My question is how can I turn "seq" into a python array?

Thanks
Michael
 
Reply With Quote
 
 
 
 
Demian Brecht
Guest
Posts: n/a
 
      10-24-2012
On 2012-10-23, at 10:23 PM, seektime <(E-Mail Removed)> wrote:
> My question is how can I turn "seq" into a python array?



Something like this perhaps?:

>>> alpha = ('a', 'b')
>>> numeric = ('1', '2')
>>> L = ['a b a\n', 'b b a\n']
>>> s = ' '.join(L)
>>> d = dict(zip(alpha, numeric))
>>> list_ = [d[c] for c in s.strip('\n').split()]
>>> list_

['1', '2', '1', '2', '2', '1']

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




 
Reply With Quote
 
 
 
 
David Hutto
Guest
Posts: n/a
 
      10-24-2012
On Wed, Oct 24, 2012 at 1:23 AM, seektime <(E-Mail Removed)> 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:
>
> 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']
>>>> s=' '.join(L)
>>>> seq1=('a','b')
>>>> seq2=('1','2')
>>>> d = dict(zip(seq1,seq2))
>>>> # Define method to replace letters according to dictionary (got this from http://gommeitputor.wordpress.com/20...s-with-python/).

> ... def replace_all(text, dic):
> ... for i, j in dic.iteritems():
> ... text = text.replace(i, j)
> ... return text
> ...
>
>>>> seq = replace_all(s,d)
>>>> print seq

> 1 2 1
> 2 2 1
>
>>>> seq

> '1 2 1\n 2 2 1\n'
>

I'd suggest, if this is what you're referring to:

x = seq.split('\n ')
array_list = [ ]
next_3_d_array = []
range_of_seq = len(seq)
for num in range(0,range_of_seq):
if num % 3 != 0:
next_3_d_array.append(num)
if num % 3 == 0:
array_list.append(next_3_d_array)
next_3_d_array = [ ]

--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com
 
Reply With Quote
 
Demian Brecht
Guest
Posts: n/a
 
      10-24-2012

On 2012-10-23, at 10:45 PM, Demian Brecht <(E-Mail Removed)> wrote:

>>>> list_ = [d[c] for c in s.strip('\n').split()]
>>>> list_

> ['1', '2', '1', '2', '2', '1']



Of course, if you want these to be ints, then you can either change the format of your int list, or map(int, list_) if you don't have control over it.

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




 
Reply With Quote
 
Demian Brecht
Guest
Posts: n/a
 
      10-24-2012

> Of course, if you want these to be ints, then you can either change the format of your int list, or map(int, list_) if you don't have control over it.



Ugh, I'm tired. Shouldn't map it, the conversion should be done in the list comprehension to avoid a needless second list iteration.

K, I'm going to sleep now.

Demian Brecht
@demianbrecht
http://demianbrecht.github.com




 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-24-2012
On Tue, Oct 23, 2012 at 10:23 PM, seektime <(E-Mail Removed)> 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/stdty...string-methods
http://docs.python.org/tutorial/data...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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-24-2012
Chris Rebert wrote:

> line.strip().split()


No need to strip() if you are going to split on whitespace:

>>> line = " a b c \n"
>>> line.split() == line.strip().split()

True

Lest the new idiom takes on while you are bravely fighting the immortable
readlines()

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-24-2012
Peter Otten wrote:

Brave new words:

> immortable


should be "immortal"

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      10-24-2012
Chris Rebert於 2012年10月24日星期三UTC+8下午2時07分29 寫道:
> On Tue, Oct 23, 2012 at 10:23 PM, seektime <(E-Mail Removed)> 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.


The list in python is a list of valid python objects.
For the number crunching part, please use arrays in numarray and scipy.

>
> > 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/stdty...string-methods
>
> http://docs.python.org/tutorial/data...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.


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      10-24-2012
Chris Rebert於 2012年10月24日星期三UTC+8下午2時07分29 寫道:
> On Tue, Oct 23, 2012 at 10:23 PM, seektime <(E-Mail Removed)> 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.


The list in python is a list of valid python objects.
For the number crunching part, please use arrays in numarray and scipy.

>
> > 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/stdty...string-methods
>
> http://docs.python.org/tutorial/data...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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Converting a list of strings into a list of integers? Jan Riechers Python 4 07-23-2012 03:31 PM
Re: Converting a list of strings into a list of integers? David Robinow Python 0 07-22-2012 05:03 PM
Re: Converting a list of strings into a list of integers? Paul Rubin Python 0 07-22-2012 05:03 PM
Re: Converting a list of strings into a list of integers? Roy Smith Python 1 07-22-2012 04:30 PM
How to instantaneously convert array of Integers into an array onint's? Royan Java 32 08-15-2008 02:20 AM



Advertisments