Re: [Tutor] Counting help

2005-09-02 Thread Jacob S.
I'll do this again, just because I like sending email.

Very similar to Alan G.'s -- but with the do first, ask forgiveness later

a = [Joe Smith, Joe Smith, Jack Smith, Sam Love, Joe Smith]
b = {}
for x in a:
try: b[x] += 1
except KeyError: b[x] = 1

Access count like this, of course,

 b[Joe Smith]
3


Jacob Schmidt

- Original Message - 
From: Scott Oertel [EMAIL PROTECTED]
To: Tutor@python.org
Sent: Tuesday, August 23, 2005 1:01 PM
Subject: [Tutor] Counting help


I have extracted a list of names, i.e.
 
 Joe Smith
 Joe Smith
 Jack Smith
 Sam Love
 Joe Smith
 
 I need to be able to count the occurances of these names and I really 
 don't have any idea where to begin.
 
 Any ideas?  excuse me this is my first post to this list, I hope I 
 included enough information.
 
 
 -Scott Oertel
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-24 Thread Scott Oertel




Kent Johnson wrote:

  Scott Oertel wrote:
  
  
The next problem I have though is creating the dict,

i have a loop, but i can't figure out how to compile the dict,  it is 
returning this: ('Joey Gale', ('Scott Joe', 'This is lame' )))


listofnames = []
while (cnt  number[1][0]):
if (date[2] == today[2]):
test = regex.findall(M.fetch(int(number[1][0]) - cnt, 
'(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
cnt += 1
if (nameofsender != []):
print nameofsender[0]
listofnames = nameofsender[0], listofnames

  
  
I think you want 
  listofnames.append(nameofsender[0])
which will add nameofsender[0] to the list. What you have -
  listofnames = nameofsender[0], listofnames
is making a tuple (a pair) out of the new name and the old list, and assigning it to listofnames. Kind of like CONS in LISP - but Python lists are more like arrays than like LISP lists.

Kent

  
  
else:
no_name += 1
else: break





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  
  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  

Thank you everyone, this is exactly it, I'm going to take that link
from byron and read up on dicts/lists.

-Scott Oertel


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Counting help

2005-08-23 Thread Scott Oertel
I have extracted a list of names, i.e.

Joe Smith
Joe Smith
Jack Smith
Sam Love
Joe Smith

I need to be able to count the occurances of these names and I really 
don't have any idea where to begin.

Any ideas?  excuse me this is my first post to this list, I hope I 
included enough information.


-Scott Oertel
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Luis N
On 8/23/05, Scott Oertel [EMAIL PROTECTED] wrote:
 I have extracted a list of names, i.e.
 
 Joe Smith
 Joe Smith
 Jack Smith
 Sam Love
 Joe Smith
 
 I need to be able to count the occurances of these names and I really
 don't have any idea where to begin.
 
 Any ideas?  excuse me this is my first post to this list, I hope I
 included enough information.
 
 
 -Scott Oertel

Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
if d.has_key(name):
x = d.get(name)
d[name] = x + 1
else:
d[name] = 1  

Luis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Byron
Luis N wrote:

Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
if d.has_key(name):
x = d.get(name)
d[name] = x + 1
else:
d[name] = 1  


100% agreed.  I have used this approach before and it works great... 

Byron


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Scott Oertel




Byron wrote:

  Luis N wrote:

  
  
Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
   if d.has_key(name):
   x = d.get(name)
   d[name] = x + 1
   else:
   d[name] = 1  


  
  
100% agreed.  I have used this approach before and it works great... 

Byron

  

Thanks for the snipplet, it's perfect for what I'm doing, I wasn't
aware of the has_key() or get(), this is very usefull.


-Scott Oertel






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Scott Oertel




Scott Oertel wrote:

  
  
Byron wrote:
  
Luis N wrote:

  

  Ideally, you would put your names into a list or dictionary to make
working with them easier. If all you're trying to do is count them
(and your list of names is long), you might consider a dictionary
which you would use like so:

#This is just the first thing I considered.

l = ['a list of names']

d = {}

for name in namelist:
   if d.has_key(name):
   x = d.get(name)
   d[name] = x + 1
   else:
   d[name] = 1  




100% agreed.  I have used this approach before and it works great... 

Byron

  
  
Thanks for the snipplet, it's perfect for what I'm doing, I wasn't
aware of the has_key() or get(), this is very usefull.
  
  
-Scott Oertel
  
  
  
  
  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  

The next problem I have though is creating the dict, 

i have a loop, but i can't figure out how to compile the dict, it is
returning this: ('Joey Gale', ('Scott Joe', ('This is lame' )))


listofnames = []
while (cnt  number[1][0]):
 if (date[2] == today[2]):
 test = regex.findall(M.fetch(int(number[1][0]) - cnt,
'(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
 cnt += 1
 if (nameofsender != []):
 print nameofsender[0]
 listofnames = nameofsender[0], listofnames
 else:
 no_name += 1
 else: break




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Alan G

I have extracted a list of names, i.e.

 Joe Smith
 Joe Smith
 Jack Smith
 Sam Love
 Joe Smith

 I need to be able to count the occurances of these names and I 
 really don't have any idea where to begin.

The classic way to do this kind of thing is with a dictionary:

names = [
 Joe Smith,
 Joe Smith,
 Jack Smith,
 Sam Love,
 Joe Smith]

counts = {}

for name in names:
if name in counts:
counts[name] += 1
else: counts[name] = 1

print counts

You could also use a list comprehension combined with the list
count() method but I doubt if its much faster.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Kent Johnson
Scott Oertel wrote:
 The next problem I have though is creating the dict,
 
 i have a loop, but i can't figure out how to compile the dict,  it is 
 returning this: ('Joey Gale', ('Scott Joe', 'This is lame' )))
 
 
 listofnames = []
 while (cnt  number[1][0]):
 if (date[2] == today[2]):
 test = regex.findall(M.fetch(int(number[1][0]) - cnt, 
 '(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip())
 cnt += 1
 if (nameofsender != []):
 print nameofsender[0]
 listofnames = nameofsender[0], listofnames

I think you want 
  listofnames.append(nameofsender[0])
which will add nameofsender[0] to the list. What you have -
  listofnames = nameofsender[0], listofnames
is making a tuple (a pair) out of the new name and the old list, and assigning 
it to listofnames. Kind of like CONS in LISP - but Python lists are more like 
arrays than like LISP lists.

Kent

 else:
 no_name += 1
 else: break
 
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Kent Johnson
Luis N wrote:
 Ideally, you would put your names into a list or dictionary to make
 working with them easier. If all you're trying to do is count them
 (and your list of names is long), you might consider a dictionary
 which you would use like so:
 
 #This is just the first thing I considered.
 
 l = ['a list of names']
 
 d = {}
 
 for name in namelist:
 if d.has_key(name):
 x = d.get(name)
 d[name] = x + 1
 else:
 d[name] = 1 

dict.get() allows a default argument that will be used if the key doesn't 
exist, so this can be shortened to
for name in namelist:
  d[name] = d.get(name, 0) + 1

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Counting help

2005-08-23 Thread Byron
Hi Scott,

The site ( http://www.greenteapress.com ) has a wonderful tutorial on it 
for Python that quickly teaches one (within a few minutes) how to work 
with dictionaries.  I would highly recommend that you check it out.  
It's well worth it...

Byron


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Counting help

2005-08-23 Thread Python
listofnames = nameofsender[0], listofnames

does not add a name to a list.  Rather it creates a tuple of the new
name and the list and then binds the tuple to the list name.  That's why
you wind up with the lisp style list.

To add a name to the head of the list use
listofnames.insert(0, nameofsender[0])


If you are using a version of Python that supports sets, using sets
would be much simpler since the duplicates get discarded automatically.

import sets # python2.3
setofnames = sets.Set()
while.
setofnames.add(nameofsender[0])

len(setofnames) # count of distinct names

-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor