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: 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" To: "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> In-Reply-To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 David Hutto wrote:=0D=0A> On Wed, Oct 24, 2012 at 1:23 AM, seektime wrote:=0D=0A> > Here's some example code=2E The= input is a list which is a "matrix" of letters:=0D=0A> > a b a=0D=0A>= > b b a=0D=0A> >=0D=0A> > and I'd like to turn this into a Python arr= ay:=0D=0A> >=0D=0A> > 1 2 1=0D=0A> > 2 2 1=0D=0A> >=0D=0A> > so 1 repla= ces a, and 2 replaces b=2E Here's the code I have so far:=0D=0A> >=0D=0A> >= >>> L=3D['a b a\n','b b a\n']=0D=0A> >>>> s=3D' '=2Ejoin(L)=0D=0A> >>>> seq= 1=3D('a','b')=0D=0A> >>>> seq2=3D('1','2')=0D=0A> >>>> d =3D dict(zip(seq1,= seq2))=0D=0A> >>>> # Define method to replace letters according to dictiona= ry (got this from=0D=0A> http://gommeitputor=2Ewordpress=2Ecom/2008/09/27/s= earch-replace-multiple-words-or-characters-with-python/)=2E=0D=0A> > =2E=2E= =2E def replace_all(text, dic):=0D=0A> > =2E=2E=2E for i, j in dic=2Eit= eritems():=0D=0A> > =2E=2E=2E text =3D text=2Ereplace(i, j)=0D=0A> = > =2E=2E=2E return text=0D=0A> > =2E=2E=2E=0D=0A> >=0D=0A> >>>> seq =3D= replace_all(s,d)=0D=0A> >>>> print seq=0D=0A> > 1 2 1=0D=0A> > 2 2 1=0D= =0A> >=0D=0A> >>>> seq=0D=0A> > '1 2 1\n 2 2 1\n'=0D=0A> >=0D=0A> I'd sugge= st, if this is what you're referring to:=0D=0A> =0D=0A> x =3D seq=2Esplit('= \n ')=0D=0A> array_list =3D [ ]=0D=0A> next_3_d_array =3D []=0D=0A> range_= of_seq =3D len(seq)=0D=0A> for num in range(0,range_of_seq):=0D=0A> = if num % 3 !=3D 0:=0D=0A> next_3_d_array=2Eappend(num)=0D=0A= > if num % 3 =3D=3D 0:=0D=0A> array_list=2Eappend= (next_3_d_array)=0D=0A> next_3_d_array =3D [ ]=0D=0A> = =0D=0A=0D=0AWow, that looks complicated=2E Why hardcode to 3 instead of whe= re ever=0D=0Athe newline is?=0D=0A=0D=0A>>> [ int(x=2Estrip()) for subseq i= n seq=2Esplit('\n') for x in subseq=2Esplit() ]=0D=0A[1, 2, 1, 2, 2, 1]=0D= =0A>>> lst =3D []=0D=0A# OR=0D=0A>>> for subseq in seq=2Esplit('\n'):=0D=0A= =2E=2E=2E for x in subseq=2Esplit():=0D=0A=2E=2E=2E lst=2Eappen= d( int(x=2Estrip()))=0D=0A=2E=2E=2E =0D=0A>>>=0D=0A=0D=0ARamit Prasad= =0D=0A=0D=0A=0D=0AThis email is confidential and subject to important discl= aimers and=0D=0Aconditions including on offers for the purchase or sale of= =0D=0Asecurities, accuracy and completeness of information, viruses,=0D=0Ac= onfidentiality, legal privilege, and legal entity disclaimers,=0D=0Aavailab= le at http://www=2Ejpmorgan=2Ecom/pages/disclosures/email=2E