Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'referring': 0.07; 'python': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'url:)': 0.13; '24,': 0.16; 'oct': 0.16; 'replaces': 0.16; 'subject:array': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'input': 0.18; 'code.': 0.20; 'define': 0.20; 'received:209.85.216.46': 0.21; "i'd": 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; '>>>>': 0.29; 'dictionary': 0.29; 'url:2008': 0.29; 'code': 0.31; 'print': 0.32; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'text': 0.34; 'list': 0.35; 'received:209.85': 0.35; 'method': 0.36; 'turn': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'header:Received:5': 0.40; 'skip:a 30': 0.60; 'letters': 0.62; 'skip:n 10': 0.63; 'url:wordpress': 0.75; 'url:27': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=LRPQ75rg1WrZiUZ3LoZObqFcnsKLUBfJHqVvLNpYlfg=; b=jEe/KwRWs5wPoiwINFdk3UmXnWb3rWokdhzsoEzZcs3v6dLyjBKmA9JrO57cpgPpHR aVRXLAStrmooW89oX665pnE6jbGSMPfxpWex6oCUFp2l/dt0WqjCyeBnjPDGpCJ00wVN ms7SUoc6/ZqJTNkm6IQaC9jPJhqF/clDMJBnLgVAXt6t4PhmG78C5L6Bsdn9LLKFue/l Pw+XSzPd2eSZcvsEloYfJydWy3yeDxtWo7Cs3zQSlcSZdOv2rVgs3SIeCwJ1tDlC+7vs kTfQd4N+qQelPiqX4Gc4sry2jF9IjCLzrfmzRAPj2XKxJZ4xmINF6Fsz9j+hv8f3DtNX TH+Q== MIME-Version: 1.0 In-Reply-To: <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com> References: <07073002-d79f-46f6-83fc-8d20c51b39c3@googlegroups.com> Date: Wed, 24 Oct 2012 01:50:38 -0400 Subject: Re: turn list of letters into an array of integers From: David Hutto To: seektime Content-Type: text/plain; charset=ISO-8859-1 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351057840 news.xs4all.nl 6949 [2001:888:2000:d::a6]:42936 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31999 On Wed, Oct 24, 2012 at 1:23 AM, seektime 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 = [ ] -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com