[Tutor] finding duplicates within a tuple of tuples

Gregory, Matthew matt.gregory at oregonstate.edu
Thu Jul 29 19:10:35 CEST 2010


Norman Khine wrote:
> basically i have two tables:
> 
> id, url
> 24715L, 'http://aqoon.local/muesli/2-muesli-tropical-500g.html'
> 24719L, 'http://aqoon.local/muesli/2-muesli-tropical-500g.html'
> 
> id, tid,
> 1, 24715L
> 2, 24719L
> 
> so i want to first update t(2)'s tid to t(1)'s id for each duplicate
> and then delete the row id = 24719L

Assuming your first table is 'd' and your second table is 't', could you do this?

for url in sorted(d):
    # Get the ID of the first element for updating t's records
    update_id = d[url][0]

    # For all duplicate URLs, remove the dict item from d and
    # update t's records
    while len(d[url]) > 1:
        pop_id = d[url].pop()
        for (k, v) in t.iteritems():
            if v == pop_id:
                t[k] = update_id

There may be a more terse way of updating the values in t that I'm not seeing right now.

matt


More information about the Tutor mailing list