Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'skip:[ 20': 0.03; 'deprecated': 0.07; 'used.': 0.07; 'python': 0.09; '"r")': 0.09; 'if,': 0.09; 'subject:into': 0.09; 'suggestions.': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'assume': 0.11; "(it's": 0.16; '2.7.3': 0.16; 'although,': 0.16; 'command,': 0.16; 'deprecated.': 0.16; 'guessing': 0.16; 'oct': 0.16; 'replaces': 0.16; 'subject:array': 0.16; "{'a':": 0.16; 'wrote:': 0.17; 'input': 0.18; 'code.': 0.20; 'question.': 0.20; 'bit': 0.21; 'converted': 0.22; 'parse': 0.22; "i'd": 0.22; 'cheers,': 0.23; 'example': 0.23; "python's": 0.23; 'task': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'linux': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'cc:addr:gmail.com': 0.27; 'implemented': 0.27; 'question': 0.27; '2.6': 0.27; 'subject:list': 0.28; 'chris': 0.28; '>>>>': 0.29; 'arrays': 0.29; 'strings,': 0.29; "i'm": 0.29; 'lists': 0.31; 'code': 0.31; 'point': 0.31; 'asking': 0.32; 'url:python': 0.32; 'file': 0.32; 'structure': 0.32; 'comments': 0.33; 'loading': 0.33; 'everyone': 0.33; 'tutorial': 0.33; 'agree': 0.34; 'received:google.com': 0.34; 'wrong': 0.34; 'thanks': 0.34; 'list': 0.35; 'lists.': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'list.': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; "wasn't": 0.36; 'method': 0.36; 'should': 0.36; 'turn': 0.36; 'october': 0.37; 'why': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'possible.': 0.38; 'url:docs': 0.38; 'most': 0.61; 'further': 0.61; 'first': 0.61; 'letters': 0.62; 'relatively': 0.62; 'solve': 0.62; 'between': 0.63; 'safe': 0.63; 'worth': 0.63; 'different': 0.63; 'more': 0.63; 'great': 0.64; 'improvements': 0.65; 'obtained': 0.71; 'suited': 0.71; 'manual,': 0.84; 'p.s.:': 0.84; 'hand.': 0.91; 'url:tutorial': 0.93 Newsgroups: comp.lang.python Date: Wed, 24 Oct 2012 21:27:28 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=67.170.243.75; posting-account=onjV8goAAABhFDzsRRtZ6vrubLrhpq0C References: <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 67.170.243.75 MIME-Version: 1.0 Subject: Re: turn list of letters into an array of integers From: seektime To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, "Michael J. Krause" 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: , Message-ID: Lines: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351139258 news.xs4all.nl 6932 [2001:888:2000:d::a6]:40811 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32088 On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote: > On Tue, Oct 23, 2012 at 10:23 PM, seektime w= rote: >=20 > > Here's some example code. The input is a list which is a "matrix" of le= tters: >=20 > > a b a >=20 > > b b a >=20 > > >=20 > > and I'd like to turn this into a Python array: >=20 >=20 >=20 > You mean a Python list. The datatype Python calls an `array` is very >=20 > different and relatively uncommonly used. >=20 > Although, confusingly, Python's lists are implemented using C arrays >=20 > rather than linked lists. >=20 >=20 >=20 > > 1 2 1 >=20 > > 2 2 1 >=20 > > >=20 > > so 1 replaces a, and 2 replaces b. Here's the code I have so far: >=20 > > >=20 > >>>> L=3D['a b a\n','b b a\n'] >=20 > >=20 > >>>> seq >=20 > > '1 2 1\n 2 2 1\n' >=20 > > >=20 > > My question is how can I turn "seq" into a python array? >=20 >=20 >=20 > I'd say you're asking the wrong question. The better question is "Why >=20 > wasn't the result a list in the first place?". Many transformations >=20 > are cumbersome to express over just strings, which is why the first >=20 > job of most programs is to parse their input into a more convenient >=20 > structure that is suited to their main task(s). >=20 >=20 >=20 > This (along with some other improvements) leads to a better, somewhat >=20 > different program/algorithm: >=20 >=20 >=20 > letter2number =3D {'a': 1, 'b': 2} >=20 > with open("path/to/file.txt", "r") as f: >=20 > result =3D [[letter2number[letter] for letter in >=20 > line.strip().split()] for line in f] >=20 >=20 >=20 > If it's safe to assume that the correspondence between the letters and >=20 > numbers isn't completely arbitrary, some further improvements are also >=20 > possible. >=20 >=20 >=20 > Some relevant docs: >=20 > http://docs.python.org/library/stdtypes.html#string-methods >=20 > http://docs.python.org/tutorial/datastructures.html#list-comprehensions >=20 >=20 >=20 > Cheers, >=20 > Chris >=20 >=20 >=20 > P.S.: I'm guessing you obtained `L` from file.readlines() or similar; >=20 > it is worth noting for future reference that the readlines() method is >=20 > considered somewhat deprecated. Thanks to everyone lots of great comments are actionable suggestions.=20 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 n= eeds to be converted right after loading. But I'm new to Python the f.readl= ine(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 deprecate= d then what should I use instead? I'm using ver. 2.6 on Linux (it's a bit d= ated, I know).