Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
turn list of letters into an array of integers
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  23 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
seektime  
View profile  
 More options Oct 24 2012, 1:23 am
Newsgroups: comp.lang.python
From: seektime <michael.j.kra...@gmail.com>
Date: Tue, 23 Oct 2012 22:23:11 -0700 (PDT)
Local: Wed, Oct 24 2012 1:23 am
Subject: turn list of letters into an array of integers
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/09/27/search-replace-multiple-word...).

... 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Demian Brecht  
View profile  
 More options Oct 24 2012, 1:45 am
Newsgroups: comp.lang.python
From: Demian Brecht <demianbre...@gmail.com>
Date: Tue, 23 Oct 2012 22:45:07 -0700
Local: Wed, Oct 24 2012 1:45 am
Subject: Re: turn list of letters into an array of integers
On 2012-10-23, at 10:23 PM, seektime <michael.j.kra...@gmail.com> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Hutto  
View profile  
 More options Oct 24 2012, 1:51 am
Newsgroups: comp.lang.python
From: David Hutto <dwightdhu...@gmail.com>
Date: Wed, 24 Oct 2012 01:50:38 -0400
Local: Wed, Oct 24 2012 1:50 am
Subject: Re: turn list of letters into an array of integers

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Demian Brecht  
View profile  
 More options Oct 24 2012, 1:51 am
Newsgroups: comp.lang.python
From: Demian Brecht <demianbre...@gmail.com>
Date: Tue, 23 Oct 2012 22:51:26 -0700
Local: Wed, Oct 24 2012 1:51 am
Subject: Re: turn list of letters into an array of integers

On 2012-10-23, at 10:45 PM, Demian Brecht <demianbre...@gmail.com> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Demian Brecht  
View profile  
 More options Oct 24 2012, 1:54 am
Newsgroups: comp.lang.python
From: Demian Brecht <demianbre...@gmail.com>
Date: Tue, 23 Oct 2012 22:54:30 -0700
Local: Wed, Oct 24 2012 1:54 am
Subject: Re: turn list of letters into an array of integers

> 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. :P

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Oct 24 2012, 2:07 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Tue, 23 Oct 2012 23:07:26 -0700
Local: Wed, Oct 24 2012 2:07 am
Subject: Re: turn list of letters into an array of integers

On Tue, Oct 23, 2012 at 10:23 PM, seektime <michael.j.kra...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 24 2012, 3:47 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Wed, 24 Oct 2012 09:47:57 +0200
Local: Wed, Oct 24 2012 3:47 am
Subject: Re: turn list of letters into an array of integers

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() ;)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 24 2012, 5:04 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Wed, 24 Oct 2012 11:04:38 +0200
Local: Wed, Oct 24 2012 5:04 am
Subject: Re: turn list of letters into an array of integers

Peter Otten wrote:

Brave new words:

> immortable

should be "immortal"

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 24 2012, 8:03 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 24 Oct 2012 05:03:52 -0700 (PDT)
Local: Wed, Oct 24 2012 8:03 am
Subject: Re: turn list of letters into an array of integers
Chris Rebert於 2012年10月24日星期三UTC+8下午2時07分29秒寫道:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
88888 Dihedral  
View profile  
 More options Oct 24 2012, 8:04 am
Newsgroups: comp.lang.python
From: 88888 Dihedral <dihedral88...@googlemail.com>
Date: Wed, 24 Oct 2012 05:03:52 -0700 (PDT)
Local: Wed, Oct 24 2012 8:03 am
Subject: Re: turn list of letters into an array of integers
Chris Rebert於 2012年10月24日星期三UTC+8下午2時07分29秒寫道:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kern  
View profile  
 More options Oct 24 2012, 8:22 am
Newsgroups: comp.lang.python
From: Robert Kern <robert.k...@gmail.com>
Date: Wed, 24 Oct 2012 13:22:13 +0100
Local: Wed, Oct 24 2012 8:22 am
Subject: Re: turn list of letters into an array of integers
On 10/24/12 1:03 PM, 88888 Dihedral wrote:

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

Your bot's database is laughably out of date.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Oct 24 2012, 11:56 am
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Wed, 24 Oct 2012 11:56:19 -0400
Local: Wed, Oct 24 2012 11:56 am
Subject: Re: turn list of letters into an array of integers
On 10/24/2012 1:23 AM, seektime 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.

If you are going to replace single characters (letters) with single
characters (digits), use maketrans and translate.

 >>> 'a b c'.translate(str.maketrans('abc', '123'))
'1 2 3'

--
Terry Jan Reedy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Oct 24 2012, 1:05 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Wed, 24 Oct 2012 18:05:43 +0100
Local: Wed, Oct 24 2012 1:05 pm
Subject: Re: turn list of letters into an array of integers
On 2012-10-24 07:07, Chris Rebert wrote:

If you're using .split() then you don't need to use .strip() as well:

     result = [[letter2number[letter] for letter in line.split()] for
line in f]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wxjmfa...@gmail.com  
View profile  
 More options Oct 24 2012, 1:27 pm
Newsgroups: comp.lang.python
From: wxjmfa...@gmail.com
Date: Wed, 24 Oct 2012 10:27:27 -0700 (PDT)
Local: Wed, Oct 24 2012 1:27 pm
Subject: Re: turn list of letters into an array of integers
Le mercredi 24 octobre 2012 07:23:11 UTC+2, seektime a écrit :

Not so sure what you mean by an "array of integers".

>>> def z(s):

...     a = s.splitlines()
...     b = [e.split() for e in a]
...     for row in range(len(b)):
...         for col in range(len(b[row])):
...             b[row][col] = ord(b[row][col]) - ord('a')
...     return b
...    
>>> z('a b a\n b b a')

[[0, 1, 0], [1, 1, 0]]

>>> # or
>>> table = {'a': 111, 'b': 222}

>>> def z2(s, table):

...     a = s.splitlines()
...     b = [e.split() for e in a]
...     for row in range(len(b)):
...         for col in range(len(b[row])):
...             b[row][col] = table[b[row][col]]
...     return b
...    
>>> z2('a b a\n b b a', table)

[[111, 222, 111], [222, 222, 111]]

>>> # note
>>> z('a\n b b b b b\n a a')

[[0], [1, 1, 1, 1, 1], [0, 0]]

jmf


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Demian Brecht  
View profile  
 More options Oct 24 2012, 1:36 pm
Newsgroups: comp.lang.python
From: Demian Brecht <demianbre...@gmail.com>
Date: Wed, 24 Oct 2012 10:36:49 -0700
Local: Wed, Oct 24 2012 1:36 pm
Subject: Re: turn list of letters into an array of integers

On 2012-10-24, at 10:27 AM, wxjmfa...@gmail.com wrote:

> Not so sure what you mean by an "array of integers".

I wasn't entirely sure about that either. I assumed given the subject that it was just a 1-D array and could then be accessed by arr[(y * width) + x].

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
seektime  
View profile  
 More options Oct 25 2012, 12:27 am
Newsgroups: comp.lang.python
From: seektime <michael.j.kra...@gmail.com>
Date: Wed, 24 Oct 2012 21:27:28 -0700 (PDT)
Local: Thurs, Oct 25 2012 12:27 am
Subject: Re: turn list of letters into an array of integers

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).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
seektime  
View profile  
 More options Oct 25 2012, 12:27 am
Newsgroups: comp.lang.python
From: seektime <michael.j.kra...@gmail.com>
Date: Wed, 24 Oct 2012 21:27:28 -0700 (PDT)
Local: Thurs, Oct 25 2012 12:27 am
Subject: Re: turn list of letters into an array of integers

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).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Oct 25 2012, 12:52 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Wed, 24 Oct 2012 21:52:07 -0700
Local: Thurs, Oct 25 2012 12:52 am
Subject: Re: turn list of letters into an array of integers

On Wed, Oct 24, 2012 at 9:27 PM, seektime <michael.j.kra...@gmail.com> wrote:
> On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote:
<snip>
>> 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).

Just iterate over the file directly using a for-loop (e.g. `for line
in some_file:`). Each iteration yields one line of the file. I used a
very minor variation of this approach in my code (a list comprehension
is just syntax sugar for a for-loop).

Cheers,
Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 25 2012, 1:47 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Thu, 25 Oct 2012 07:47:48 +0200
Local: Thurs, Oct 25 2012 1:47 am
Subject: [OT] Re: turn list of letters into an array of integers

Dennis Lee Bieber wrote:
> On Wed, 24 Oct 2012 11:04:38 +0200, Peter Otten <__pete...@web.de>
> declaimed the following in gmane.comp.python.general:

>> Peter Otten wrote:

>> Brave new words:

>> > immortable

>> should be "immortal"

> Readlines() isn't immortal... It's a lich
> http://en.wikipedia.org/wiki/Lich

Wasn't there a Monty Python sketch where a man carrying a parrot in a cage
comes into a shop full of stuffed animals and complains: No, I don't admire
the taxidermist for making that parrot look like it were alive -- that beast
bit me!

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Oct 25 2012, 3:49 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 25 Oct 2012 07:49:42 GMT
Local: Thurs, Oct 25 2012 3:49 am
Subject: Re: [OT] Re: turn list of letters into an array of integers

On Thu, 25 Oct 2012 07:47:48 +0200, Peter Otten wrote:
> Wasn't there a Monty Python sketch where a man carrying a parrot in a
> cage comes into a shop full of stuffed animals and complains: No, I
> don't admire the taxidermist for making that parrot look like it were
> alive -- that beast bit me!

I don't think so. Are you thinking of the famous Monty Python "Dead
Parrot Sketch"? Here's one of many versions:

http://www.youtube.com/watch?v=4vuW6tQ0218

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Oct 25 2012, 4:25 am
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Thu, 25 Oct 2012 10:25:31 +0200
Local: Thurs, Oct 25 2012 4:25 am
Subject: Re: [OT] Re: turn list of letters into an array of integers

Steven D'Aprano wrote:
> On Thu, 25 Oct 2012 07:47:48 +0200, Peter Otten wrote:

>> Wasn't there a Monty Python sketch where a man carrying a parrot in a
>> cage comes into a shop full of stuffed animals and complains: No, I
>> don't admire the taxidermist for making that parrot look like it were
>> alive -- that beast bit me!

> I don't think so. Are you thinking of the famous Monty Python "Dead
> Parrot Sketch"? Here's one of many versions:

> http://www.youtube.com/watch?v=4vuW6tQ0218

My rendition was meant to be a travesty of that one, an "undead" parrot as a
follow-up to Dennis' "lich" post. I'm sorry I forgot the smiley ;)

- He didn't move, that was just the wind stirring his plumage.
- No, that parrot is alive and kicking, fresh as a daisy, full of beans...
- Aren't the glass eyes beautiful?
- Glass eyes -- he just blinked!

And so on. I'm off to work on my laden swallow branch of Python. It's going
to be a real heavy-weight...

--
Always look on the dark side of death


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Lawrence  
View profile  
 More options Oct 25 2012, 4:51 am
Newsgroups: comp.lang.python
From: Mark Lawrence <breamore...@yahoo.co.uk>
Date: Thu, 25 Oct 2012 09:55:37 +0100
Local: Thurs, Oct 25 2012 4:55 am
Subject: Re: [OT] Re: turn list of letters into an array of integers
On 25/10/2012 09:25, Peter Otten wrote:

I just hope you get the technicalities correct.  "That parrot wouldn't
move if you put 4 million volts through it".  What rubbish.  It should
either have been 4 million amps through it or 4 million volts across it.
  I'm +1 for the former, although possibly biased by history.

--
Cheers.

Mark Lawrence.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Prasad, Ramit  
View profile  
 More options Oct 25 2012, 5:27 pm
Newsgroups: comp.lang.python
From: "Prasad, Ramit" <ramit.pra...@jpmorgan.com>
Date: Thu, 25 Oct 2012 21:27:16 +0000
Local: Thurs, Oct 25 2012 5:27 pm
Subject: RE: turn list of letters into an array of integers

Wow, that looks complicated. Why hardcode to 3 instead of where ever
the newline is?

>>> [ int(x.strip()) for subseq in seq.split('\n') for x in subseq.split() ]
[1, 2, 1, 2, 2, 1]
>>> lst = []
# OR
>>> for subseq in seq.split('\n'):

...     for x in subseq.split():
...         lst.append( int(x.strip()))
...    


Ramit Prasad

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »