473,770 Members | 6,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function to prune dictionary keys not working

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 :((

Jun 28 '06 #1
3 1765
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
Jun 28 '06 #2
On 6/27/06, John Machin <sj******@lexic on.net> 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.
Jun 28 '06 #3
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...
Jun 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3590
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
57
3617
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
17
43955
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. */ Technet Script Center - http://www.microsoft.com/technet/scriptcenter/ WSH 5.6 Documentation -...
26
4070
by: Alan Silver | last post by:
Hello, I have a server running Windows Server 2003, on which two of the web sites use the MegaBBS ASP forum software. Both sites suddenly developed the same error, which seems to be connected to the dictionary object. After some tinkering, I whittled it down to the following (complete) ASP... <%@ CodePage=65001 Language="VBScript"%>
4
3357
by: Noah | last post by:
I have a dictionary that I would like to expand to satisfy a function's agument list. I can used the ** syntax to pass a dictionary, but this only works if each key in the dictionary matches an argument. I cannot pass a dictionary that has more keys than the function has arguments. # Example 1 - This works: # Prints "hello world!" def foo (arg1='greetings', arg2='planet', arg3='.'):
90
10820
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
14
3466
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a list? I know that sets were introduced a lot later and lists/dictionaries were used instead but I think "the only correct way" now is for the dictionary keys and values to be sets. Presently {1:0,2:0,3:0}.keys() will produce but it could also...
11
11421
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the keys are numeric, the keys themselves are ordered in the dictionary. part of my code is like this: radix={} for i in range(256):
5
9531
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that code for a few hours: #dics Encryption={',':'hy{;',' ':'h4x0r2','':'zomg','?':'bko','a':'ika','b':'d0v','c':'ino', 'd':'maw', 'e':'aon', 'f':'que', 'g':'kip', 'h':'an', 'n':'ko print lol except KeyError: print 'These...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.