[Tutor] how to delete some quasi-duplicated keys

lina lina.lastname at gmail.com
Fri Nov 25 10:29:45 CET 2011


On Fri, Nov 25, 2011 at 5:19 PM, Christian Witts <cwitts at compuscan.co.za> wrote:
> On 2011/11/25 11:15 AM, lina wrote:
>
> On Fri, Nov 25, 2011 at 5:05 PM, Christian Witts <cwitts at compuscan.co.za>
> wrote:
>
> On 2011/11/25 10:41 AM, lina wrote:
>
> pairs
>
> {('66', '69'): 217, ('69', '66'): 75, ('64', '71'): 25}
>
>
> such as here ('66', '69') and ('69', '66') is one key,
>
> I wanna keep only one and add the value of those two keys, above is a
> very simple example:
>
> here is the (failed) code:
>
>         for k, v in pairs.items():
>             if str(k)[1]+str(k)[0] in pairs.keys():
>                 print(pairs[str(k)[1]+str(k)[0]])
>                 pairs[k]+=pairs[str(k)[1]+str(
> k)[0]]
>                 del pairs[str(k)[1]+str(k)[0]]
>             print(v,k)
>
>
> Thanks for any advice,
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> pairs.items() is fine if it's a small dictionary but for larger ones it's
> going to slow things down as it generates the entire list of items before
> you continue, rather use .iteritems() as it creates an iterator which only
> yields one item at a time making it more efficient.
>
> str(k)[1]+str(k)[0] is string concatenation so your first key tested is
> '6669' and not ('66', '69') as you intended, you would have to create a new
> tuple to get the key you wanted like `if (k[1], k[0]) in pairs.keys():`.
> Also, your check to "in pairs.keys()" is wasteful as you generate a new list
> of keys for every key and you will be better off using `in pairs:` directly
> as it performs a dictionary lookup to test if the key exists.
>
> With that in mind, this is how I would re-write that code segment. YMMV
>
> for key in pairs.iterkeys():
>     # The [::-1] creates a reverse of the iterable so ('66', '69') will be
> ('69', '66')
>
> $ python3 dehydron_data_stastic.py | sort -nr
> Traceback (most recent call last):
>   File "dehydron_data_stastic.py", line 44, in <module>
>     for k in pairs.iterkeys():
> AttributeError: 'dict' object has no attribute 'iterkeys'
>
>     if key[::-1] in pairs:
>         try:
>             # The first instance of the pair gets kept and the reversed gets
> added
>             pairs[key] += pairs[key[::-1]]
>             del pairs[::-1]
>         except KeyError:
>             print "Key ('%s', '%s') already accumulated)" % key
>
> Hope that helps.
>
> --
>
> Christian Witts
> Python Developer
>
>
> Ah, should have mentioned .iterkeys() / .itervalues() are in Python 2.x and
> .keys() and .values() have been changed in Py3 to match .iter* from Py2.x.
>
> Just change that line to `for key in pairs.keys():` then.

        for key in pairs.keys():
            if key[::-1] in pairs:
                pairs[key] += pairs[key[::-1]]
                del pairs[key[::-1]]
            print(pairs)

Traceback (most recent call last):
  File "dehydron_data_stastic.py", line 47, in <module>
    for key in pairs.keys():
RuntimeError: dictionary changed size during iteration


> --
>
> Christian Witts
> Python Developer
>


More information about the Tutor mailing list