Function to prune dictionary keys not working

Discussion in 'Python' started by Girish Sahani, Jun 28, 2006.

  1. hi ppl,
    Here is a simple function to remove those keys of a dictionary whose
    values are less than some specified value. But it isnt working. Please
    help.

    def prune(d,cp):
    l = []
    for rule,value in d.iteritems():
    #print value
    if value >= cp:
    l.append(rule)
    return l


    >>> d = {'be=>c': '1.00', 'c=>da': '0.50', 'ea=>b': '0.33', 'be=>d':

    '0.50', 'c=>ba': '0.33', 'bd=>a': '1.00', 'a=>cb': '0.33', 'ea=>c':
    '0.67', 'a=>cd': '0.50', 'e=>ac': '0.40', 'e=>ab': '0.20', 'c=>bd':
    '0.33', 'e=>cb': '0.40', 'ed=>b': '0.25', 'ed=>c': '0.50'}
    >>> cp = 0.5
    >>> if prune(d,cp) == d.keys():

    print "code not working :(("

    code not working :((
     
    Girish Sahani, Jun 28, 2006
    #1
    1. Advertisements

  2. Girish Sahani

    John Machin Guest

    On 28/06/2006 12:15 PM, Girish Sahani wrote:
    > hi ppl,
    > Here is a simple function to remove those keys of a dictionary whose
    > values are less than some specified value. But it isnt working. Please
    > help.
    >
    > def prune(d,cp):
    > l = []
    > for rule,value in d.iteritems():
    > #print value
    > if value >= cp:
    > l.append(rule)
    > return l
    >
    >
    >>>> d = {'be=>c': '1.00', 'c=>da': '0.50', 'ea=>b': '0.33', 'be=>d':

    > '0.50', 'c=>ba': '0.33', 'bd=>a': '1.00', 'a=>cb': '0.33', 'ea=>c':
    > '0.67', 'a=>cd': '0.50', 'e=>ac': '0.40', 'e=>ab': '0.20', 'c=>bd':
    > '0.33', 'e=>cb': '0.40', 'ed=>b': '0.25', 'ed=>c': '0.50'}
    >>>> cp = 0.5
    >>>> if prune(d,cp) == d.keys():

    > print "code not working :(("
    >
    > code not working :((
    >


    |>> '1.00' >= 0.5
    True
    |>> '0.33' >= 0.5
    True

    Python (correctly) does very little (guesswork-based) implicit type
    conversion.

    It looks like the values in your dictionary should stored as floats, not
    as strings.

    HTH,
    John
     
    John Machin, Jun 28, 2006
    #2
    1. Advertisements

  3. Girish Sahani

    Serge Orlov Guest

    On 6/27/06, John Machin <> wrote:
    > |>> '1.00' >= 0.5
    > True
    > |>> '0.33' >= 0.5
    > True
    >
    > Python (correctly) does very little (guesswork-based) implicit type
    > conversion.


    At the same time, Python (incorrectly :) compares incomparable objects.
     
    Serge Orlov, Jun 28, 2006
    #3
  4. Girish Sahani a écrit :
    > hi ppl,
    > Here is a simple function to remove those keys of a dictionary whose
    > values are less than some specified value.
    >
    > But it isnt working.


    "is not working" is the worst possible description of a problem.

    > def prune(d,cp):
    > l = []
    > for rule,value in d.iteritems():
    > #print value
    > if value >= cp:
    > l.append(rule)
    > return l


    This doesn't remove anything from the dict - it returns a list of keys
    for which values are >= cp value. FWIW, a simple expression is enough to
    do this:
    [k for k, v in d.items() if v >= cp]

    The algorithm for what you described would be:
    def prune(dic, cmp):
    for k, v in dic.items():
    if v < cmp:
    del dic[k]


    >>>>d = {'be=>c': '1.00', 'c=>da': '0.50', 'ea=>b': '0.33', 'be=>d':

    >
    > '0.50', 'c=>ba': '0.33', 'bd=>a': '1.00', 'a=>cb': '0.33', 'ea=>c':
    > '0.67', 'a=>cd': '0.50', 'e=>ac': '0.40', 'e=>ab': '0.20', 'c=>bd':
    > '0.33', 'e=>cb': '0.40', 'ed=>b': '0.25', 'ed=>c': '0.50'}
    >
    >>>>cp = 0.5


    You are comparing floats with strings...
     
    Bruno Desthuilliers, Jun 29, 2006
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sandeep Kanwal

    serial keys/validation keys

    sandeep Kanwal, Oct 29, 2004, in forum: C++
    Replies:
    1
    Views:
    724
    Mike Wahler
    Oct 29, 2004
  2. Rory Campbell-Lange

    newbie : prune os.walk

    Rory Campbell-Lange, Mar 10, 2005, in forum: Python
    Replies:
    4
    Views:
    587
    Tim Roberts
    Mar 12, 2005
  3. Tim N. van der Leeuw

    Removing dictionary-keys not in a set?

    Tim N. van der Leeuw, Apr 18, 2005, in forum: Python
    Replies:
    9
    Views:
    525
    Raymond Hettinger
    Apr 19, 2005
  4. Christoph Zwerschke

    Why is dictionary.keys() a list and not a set?

    Christoph Zwerschke, Nov 23, 2005, in forum: Python
    Replies:
    90
    Views:
    1,900
    Mike Meyer
    Nov 29, 2005
  5. Martin Elzen
    Replies:
    2
    Views:
    225
    Ken Bloom
    Apr 25, 2008
  6. afrinspray

    MIME::Parser and Purge / Prune

    afrinspray, May 31, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    240
    afrinspray
    Jun 9, 2006
  7. skip path prune

    , Jul 17, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    140
  8. skip path prune

    , Jul 17, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    166
    Mumia W.
    Jul 17, 2006
Loading...