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

Michael Selik michael.selik at gmail.com
Fri Jun 17 18:39:40 EDT 2016


On Fri, Jun 17, 2016, 6:12 PM Lulu J <lulu.jama2016 at gmail.com> wrote:

> Hi there,
>
> My apologies if this is a trivial question but I am sort of new to python.
> Here is my problem:
> 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.
>

Put the words in a dictionary, key is the location, value is the word.

Sort the location-word pairs by location. Loop over the result pairwise.

>


More information about the Tutor mailing list