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


Groups > comp.lang.python > #62297

Re: Struggling for inspiration with lists

Return-Path <prvs=057eee947=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.044
X-Spam-Evidence '*H*': 0.91; '*S*': 0.00; 'algorithm': 0.04; 'output': 0.05; '(b)': 0.07; 'string': 0.09; 'repeated': 0.09; 'cc:addr:python-list': 0.11; '"x"': 0.16; '(starting': 0.16; 'dict': 0.16; 'keyed': 0.16; 'ordereddict': 0.16; 'timestamp': 0.16; 'timestamps': 0.16; 'tuples,': 0.16; 'elements': 0.16; 'bit': 0.19; 'seems': 0.21; 'cc:addr:python.org': 0.22; 'print': 0.22; 'this?': 0.23; '(a)': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'values': 0.27; 'header:In- Reply-To:1': 0.27; '(c)': 0.29; 'list:': 0.30; "i'm": 0.30; 'code': 0.31; 'timestamp:': 0.31; 'tuples': 0.31; '-----': 0.33; 'subject:with': 0.35; 'problem.': 0.35; 'something': 0.35; 'point.': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'there': 0.35; 'sequence': 0.36; "didn't": 0.36; 'hi,': 0.36; 'example,': 0.37; 'wrong': 0.37; 'list': 0.37; 'list.': 0.37; 'starting': 0.37; 'thank': 0.38; 'depends': 0.38; 'little': 0.38; 'sure': 0.39; 'how': 0.40; "you're": 0.61; 'you.': 0.62; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'information': 0.63; 'such': 0.63; 'received:194': 0.64; 'to:addr:gmail.com': 0.65; 'determine': 0.67; 'notice:': 0.67; 'person,': 0.68; 'started.': 0.68; 'privileged.': 0.69; 'disclose': 0.74; 'as:': 0.81; 'longest': 0.84; 'working,': 0.84; 'medium.': 0.91; 'task,': 0.91
X-IronPort-AV E=Sophos;i="4.95,507,1384297200"; d="scan'208";a="2255628"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Wed, 18 Dec 2013 13:38:23 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Denis McMahon <denismfmcmahon@gmail.com>
In-Reply-To <l8r63h$fid$4@dont-email.me>
Subject Re: Struggling for inspiration with lists
MIME-Version 1.0
X-Mailer Zimbra 7.2.4_GA_2900 (ZimbraWebClient - GC31 (Win)/7.2.4_GA_2900)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4351.1387370305.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387370305 news.xs4all.nl 2850 [2001:888:2000:d::a6]:46403
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!Xbb.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:62297

Show key headers only | View raw


----- Original Message -----
> Hi
> 
> I have a list of data that presents as:
> 
> timestamp: value
> 
> Timestamps are used solely to determine the sequence of items in the
> list.
> 
> I want to find the longest repeated sequence of values in the list.
> Example, in the following list:
> 
> data = { 0: "d", 1: "x", 2: "y", 3: "t", 4: "d", 5: "y", 77: "g"' 78:
> "h", 79: "x", 80: "y", 206: "t", 210: "d", 211: "x" }
> 
> I would pull out the sequence x-y-t-d (starting at 1 and 79)
> 
> I need to keep the timestamp / data association because I need to
> generate output that identifies (a) the longest repeated sequence (b)
> how
> many elements in the longest repeated sequence (c) at what timestamps
> each occurrence started.
> 
> I'm not sure of the best way, programatically, to aproach this task,
> which means I'm unsure whether eg a list of tuples ( time, data ) or
> an
> OrderedDict keyed on the timestamp is the best starting point.
> 
> I can make a list of tuples using:
> 
> d = [ (k,v) for k,v in data ]
> 
> and with the list of tuples, I can do something like:
> 
> d.sort( key=lambda tup: tup[0] )
> 
> max_start_a = 0
> max_start_b = 0
> max_len = 0
> i = 0
> 
> while i < len( d ):
> 
>   j = i + 1
> 
>   while j < len( d ):
> 
>     o = 0
> 
>     while j+o < len( d ) and d[i+o][1] == d[j+o][1]:
> 
>       o += 1
> 
>       if o > max_len:
> 
>         max_len = 0
>         max_start_a = i
>         max_start_b = j
> 
>     j += 1
> 
>   i += 1
> 
> print d[max_start_a][0], d[max_start_b][0], max_len
> 
> Is there a better way to do this?
> 
> --
> Denis McMahon, denismfmcmahon@gmail.com

Hi,

Depends on what you're meaning by better.
If you mean quicker, it seems there is an algorithm in O(n). http://stackoverflow.com/questions/11090289/find-longest-repetitive-sequence-in-a-string
You'll need a little bit of work to move from dict / to string / to dict but that shouldn't be a problem.

If you didn't mean quicker, I have no idea. If your current code is working, I don't see how it would such wrong that you'd need to change it.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Struggling for inspiration with lists Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-12-18 13:38 +0100

csiph-web