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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'completeness': 0.07; 'newline': 0.07; 'referring': 0.07; 'python': 0.09; '[1,': 0.09; 'lst': 0.09; 'received:155': 0.09; 'subject:into': 0.09; 'def': 0.10; 'url:)': 0.13; '24,': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'oct': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:169.70': 0.16; 'received:169.70.184': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'replaces': 0.16; 'securities,': 0.16; 'subject:array': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wow,': 0.16; 'wed,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'input': 0.18; 'code.': 0.20; 'define': 0.20; 'to:name:python-list@python.org': 0.20; "i'd": 0.22; 'example': 0.23; 'received:169.254': 0.24; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'am,': 0.27; 'accuracy': 0.27; 'is?': 0.27; 'replace': 0.27; 'subject:list': 0.28; '>>>>': 0.29; 'dictionary': 0.29; 'url:2008': 0.29; 'received:169': 0.29; 'header:Received:8': 0.30; 'code': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'text': 0.34; 'list': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'turn': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'skip:a 30': 0.60; 'letters': 0.62; 'ever': 0.63; 'skip:n 10': 0.63; 'information,': 0.63; 'url:email': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'url:wordpress': 0.75; 'sale': 0.76; 'received:169.254.8': 0.84; 'url:27': 0.84
X-DKIM OpenDKIM Filter v2.1.3 sf1.jpmchase.com q9PLRTC3026565
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1351200449; bh=ge5PAWhKkMOAv32j8RlneOvx055nnKNyNHwwxueXLxY=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=l9+g6uTHFjNnVJraQn5Hq7x7K8g/4VRS5vluI+tMQyBN99LPypuYijlG/pNrra44b Z9FyIpgE1LMAtDlfuklbxschyhEOF3LAwIJsoAb4jC86HVan5qvCJVp+yHnlj8jS4/ wVocLgPwTok8Lc1KsB+3ug48XH8PSD8WTK8dZ2G0=
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: turn list of letters into an array of integers
Thread-Topic turn list of letters into an array of integers
Thread-Index AQHNsakLO5FlxDzvY0299UEJeVNzNZfINrgAgAJSQJA=
Date Thu, 25 Oct 2012 21:27:16 +0000
References <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com> <CA+vVgJXYET2xDT-zkkK1qj-qBsJDq56=K7jHzmhfr1Cb4-p29w@mail.gmail.com>
In-Reply-To <CA+vVgJXYET2xDT-zkkK1qj-qBsJDq56=K7jHzmhfr1Cb4-p29w@mail.gmail.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
Content-Type text/plain; charset="us-ascii"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2870.1351200457.27098.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351200457 news.xs4all.nl 6848 [2001:888:2000:d::a6]:60242
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32170

Show key headers only | 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