Dictionary sorting problem

2005-09-16 Thread JerryB
Hi,
I have a dictionary for counting ocurrences of strings in a document.
The dictionary looks like this:

'hello':135
'goodbye':30
'lucy':4
'sky':55
'diamonds':239843
'yesterday':4

I want to print the dictionary so I see most common words first:

'diamonds':239843
'hello':135
'sky':55
'goodbye':30
'lucy':4
'yesterday':4

How do I do this? Notice I can't 'swap' the dictionary (making keys
values and values keys) and sort because I have values like lucy &
yesterday which have the same number of occurrences.

Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


locals() and dictionaries

2006-02-01 Thread JerryB
Hi,
I have a dictionary, a string, and I'm creating another string, like
this:

dict = {}
dict[beatles] = "need"
str = "love"

mystr = """All you %(dict[beatles])s is %(str)s""" % locals()

Why do I get
keyerror: 'dict[one]'?

Is there a way to reference the elements in a dictionary with locals()
or do I need to create a temp variable, like

need = dict[one]
mystr = """All you %(need)s is %(str)s"""

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: locals() and dictionaries

2006-02-01 Thread JerryB
Rocco:
thanks for your response. The examples were just made up. I don't
normally use 'dict' and 'str'.
I know I can create a dictionary with the variables I want, etc. My
question is not how to solve the problem, or how to come up with a
work-around (I'm getting pretty good at this one :), so my question
stands:

is it possible to access the individual members of a dictionary using %
locals() when creating a string?

Thank you again for your suggestions.
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list


re.match versus re.findall

2006-02-09 Thread JerryB
Hi,
I have a string like this:

invalidStr = "192.168.*.1"

I want to be sure I don't get a * followed by a number, i.e. I want
invalidStr to be invalid. So I do:

numberAfterStar = re.compile(r'\*.*\d')

Now, here's the fun:

If I run:
if numberAfterStar.findall(invalidStr):
print "Found it with findall!"

it prints, if I run:

if numberAfterStar.match(invalidStr):
print "Found it with match!"

it doesn't.
Why does findall finds a match it but match doesn't?

Thanks!
Jerry

-- 
http://mail.python.org/mailman/listinfo/python-list