Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32170

RE: turn list of letters into an array of integers

From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Subject RE: turn list of letters into an array of integers
Date 2012-10-25 21:27 +0000
References <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com> <CA+vVgJXYET2xDT-zkkK1qj-qBsJDq56=K7jHzmhfr1Cb4-p29w@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2870.1351200457.27098.python-list@python.org> (permalink)

Show all headers | View raw


David Hutto wrote:
> On Wed, Oct 24, 2012 at 1:23 AM, seektime <michael.j.krause@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:
> >
> >   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/2008/09/27/search-replace-multiple-words-or-characters-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 = [ ]
> 

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.  

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

turn list of letters into an array of integers seektime <michael.j.krause@gmail.com> - 2012-10-23 22:23 -0700
  Re: turn list of letters into an array of integers Demian Brecht <demianbrecht@gmail.com> - 2012-10-23 22:45 -0700
  Re: turn list of letters into an array of integers David Hutto <dwightdhutto@gmail.com> - 2012-10-24 01:50 -0400
  Re: turn list of letters into an array of integers Demian Brecht <demianbrecht@gmail.com> - 2012-10-23 22:51 -0700
  Re: turn list of letters into an array of integers Demian Brecht <demianbrecht@gmail.com> - 2012-10-23 22:54 -0700
  Re: turn list of letters into an array of integers Chris Rebert <clp2@rebertia.com> - 2012-10-23 23:07 -0700
    Re: turn list of letters into an array of integers 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-24 05:03 -0700
      Re: turn list of letters into an array of integers Robert Kern <robert.kern@gmail.com> - 2012-10-24 13:22 +0100
    Re: turn list of letters into an array of integers 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-24 05:03 -0700
    Re: turn list of letters into an array of integers seektime <michael.j.krause@gmail.com> - 2012-10-24 21:27 -0700
      Re: turn list of letters into an array of integers Chris Rebert <clp2@rebertia.com> - 2012-10-24 21:52 -0700
    Re: turn list of letters into an array of integers seektime <michael.j.krause@gmail.com> - 2012-10-24 21:27 -0700
  Re: turn list of letters into an array of integers Peter Otten <__peter__@web.de> - 2012-10-24 09:47 +0200
  Re: turn list of letters into an array of integers Peter Otten <__peter__@web.de> - 2012-10-24 11:04 +0200
  Re: turn list of letters into an array of integers Terry Reedy <tjreedy@udel.edu> - 2012-10-24 11:56 -0400
  Re: turn list of letters into an array of integers MRAB <python@mrabarnett.plus.com> - 2012-10-24 18:05 +0100
  Re: turn list of letters into an array of integers wxjmfauth@gmail.com - 2012-10-24 10:27 -0700
    Re: turn list of letters into an array of integers Demian Brecht <demianbrecht@gmail.com> - 2012-10-24 10:36 -0700
  Re: turn list of letters into an array of integers Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-24 15:57 -0400
  [OT] Re: turn list of letters into an array of integers Peter Otten <__peter__@web.de> - 2012-10-25 07:47 +0200
    Re: [OT] Re: turn list of letters into an array of integers Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-25 07:49 +0000
      Re: [OT] Re: turn list of letters into an array of integers Peter Otten <__peter__@web.de> - 2012-10-25 10:25 +0200
      Re: [OT] Re: turn list of letters into an array of integers Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-25 09:55 +0100
  RE: turn list of letters into an array of integers "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-25 21:27 +0000

csiph-web