[Tutor] help with comparing list of tuples in python 2

Lulu J lulu.jama2016 at gmail.com
Mon Jun 20 08:42:06 EDT 2016


Many thanks to those of you who responded to my question. I really
appreciate.

Just to answer some questions you guys raised.

1.The list only contains the key words that was matched as a result of a
search. So words in this list aren't necessarily next to each other. They
could be anywhere in the text and the words can appear multiple times
2. The list is already sorted by the position
3.The main goal is to count the number of matches but to count adjacent
words only once. ie. two words or more next to each other counts as one
match.

Again, thank you for your time.
L

On Mon, Jun 20, 2016 at 8:34 AM, Muhubo Yusuf <muhuboyusuf at gmail.com> wrote:

>
>
> From: Alan Gauld via Tutor <tutor at python.org>
> Date: Fri, Jun 17, 2016 at 6:36 PM
> Subject: Re: [Tutor] help with comparing list of tuples in python 2
> To: tutor at python.org
>
>
> On 17/06/16 20:18, Lulu J wrote:
>
> > I have a list of dictionaries. Each dictionary has a word and its
> position
> > in the text  the positions are in the form of a tuple.
> > Here is an example:
> > [
> > {'position': (5, 4), 'term': u'happy',},
> >  {'position': (5, 5), 'term': u'something'}
> > ]
> >
> > for the potions, the first element is the paragraph number and the second
> > is the word number in that paragraph(sequence from 1...n)
> >
> > What I would like to is find which words are next to each other. Meaning,
> > they will be in the same paragraph and the difference of their word
> numbers
> > is 1.
>
> You can sort them by providing a key function, for example,
> a simplified version::
>
> >>> L = [{'k':1,'v':0},{'k':5,'v':9},{'k':2,'v':6},{'k':0,'v':12}]
> >>> sorted(L,key=lambda d: d['v'])
> [{'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}, {'k': 0, 'v': 12}]
> >>> sorted(L,key=lambda d: d['k'])
> [{'k': 0, 'v': 12}, {'k': 1, 'v': 0}, {'k': 2, 'v': 6}, {'k': 5, 'v': 9}]
> >>>
>
> Then filter your results to find an adjacent pair that have matching
> positions. (This may not be necessary if you have all of the words since
> they should naturally be placed adjacent to each other, but
> if you only have key words it will be needed)
>
> Something like (untested)
>
> neighbours = [item for index,item in enumerate(data)
>               if item['position'][0] == data[index+1][position'][0] and
>               item['position'][1] == data[index+1][position'][1]-1]
>
> But there are lots of possible gotchas here.
>
> - look out for the last item which will give you an index error!
> - what happens to words that appear more than once? Are they
>   in your list more than once?
> - probably more :-(
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list