[Tutor] finding duplicates within a tuple of tuples
Norman Khine
norman at khine.net
Fri Jul 30 05:28:34 CEST 2010
Hello,
Thanks for the replies.
On Thu, Jul 29, 2010 at 7:10 PM, Gregory, Matthew
<matt.gregory at oregonstate.edu> wrote:
> 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.
Here is the latest version http://pastie.org/1066582 can this be
further improved?
>
> matt
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
%>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] )
More information about the Tutor
mailing list