Function to prune dictionary keys not working
Steve Holden
steve at holdenweb.com
Wed Jun 28 04:05:36 EDT 2006
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.
>
Besides all the good advice you've been given about not expecting
string/float comparisons to be meaningful, remember too that both your
function's result and the dict's .keys() method return *lists*.
>>> [1, 2] == [2, 1]
False
>>>
Easiest would be to convert both lists to sets and use
if set(prune(d,cp)) == set(d.keys()):
You could, instead, sort them before comparison, but that's only a win
if you need them sorted elsewhere.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
More information about the Python-list
mailing list