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

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jun 17 18:36:05 EDT 2016


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




More information about the Tutor mailing list