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


Groups > comp.lang.python > #32004

Re: turn list of letters into an array of integers

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'skip:[ 20': 0.03; 'used.': 0.07; 'python': 0.09; '"r")': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'subject:into': 0.09; 'cc:addr:python- list': 0.10; 'assume': 0.11; 'although,': 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; 'received:209.85.212.46': 0.18; 'code.': 0.20; 'question.': 0.20; 'parse': 0.22; "i'd": 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'example': 0.23; "python's": 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'implemented': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; '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; 'asking': 0.32; 'url:python': 0.32; 'structure': 0.32; 'received:google.com': 0.34; 'wrong': 0.34; 'list': 0.35; 'lists.': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'url:org': 0.36; 'url:library': 0.36; "wasn't": 0.36; 'method': 0.36; 'turn': 0.36; '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; 'header:Received:5': 0.40; 'most': 0.61; 'further': 0.61; 'first': 0.61; 'letters': 0.62; 'relatively': 0.62; 'between': 0.63; 'safe': 0.63; 'worth': 0.63; 'different': 0.63; 'more': 0.63; 'improvements': 0.65; 'obtained': 0.71; 'suited': 0.71; 'p.s.:': 0.84; 'sender:addr:chris': 0.84; 'url:tutorial': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=2iTu9jcusm2fXp33KuyLF0vsHNO8/kZQxPoA9FDiYK0=; b=Knd1Rg65ZdWLl5mhPx0LnIiHxnqSzb3JlGp3XF9KBgJn+twfV+wRfr9SQPSC1psi3D hpX0YxiAHjEW37AKRpJGxpoW5l0ZXqzsQesSKK8CSvD+LSE7546/hr4S8sYqXGEVhXcY 8fHUz9hBV7AosoLEEjM49R5p3NmC5loBg7keU=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=2iTu9jcusm2fXp33KuyLF0vsHNO8/kZQxPoA9FDiYK0=; b=UAJLkps6RuuPoGO/6fODgYVrEhzN32/CzV4/Hxxu+EIlnZJFGIQMKBmY/wuCAfPEbC 9gxfkAAR014hIivW5GFLhhBPiEIym3LPhntX2Id6IORdjRzLVaShNVkR5+pph8WC8p8T Ya5qGeayoztxc15UkfKWbuLTu58IB7WthsR8Dc+sh/Zk9Xw7ytZdiSap4pcm6C7X1iia +CSXJaZn+Jlrx8csNMH8OotQjpq7/hAgpodW9ZyCDUo5xfLo+JA7jPl/zs8idHMpV+q4 /Z9aRhsgBuZhDNpeXwauBoe+/QnYpKot5GuMM5ntTMKt34K3IOcYn+4vlwLpxlTXavFA QVqQ==
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com>
References <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com>
Date Tue, 23 Oct 2012 23:07:26 -0700
X-Google-Sender-Auth aX9kpO6QTEZCSCGXYvisTIXFwVs
Subject Re: turn list of letters into an array of integers
From Chris Rebert <clp2@rebertia.com>
To "Michael J. Krause" <michael.j.krause@gmail.com>
Content-Type text/plain; charset=UTF-8
X-Gm-Message-State ALoCoQktCFp7jcxY3/iBwUMRgYa3ZCJ4IYn1P/rqnLzhvhM9SM2TXN9VFfRzz1T3WzQnThxysz2v
Cc python-list@python.org
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.2727.1351058849.27098.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351058849 news.xs4all.nl 6969 [2001:888:2000:d::a6]:60334
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32004

Show key headers only | View raw


On Tue, Oct 23, 2012 at 10:23 PM, 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:

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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