Needing python experts to help with a problem

John Nagle nagle at animats.com
Sat Jun 7 02:15:02 EDT 2008


CM wrote:
> On Jun 7, 12:51 am, gms <esug... at gmail.com> wrote:
>> Hello,
>> I have the following list:
>>
>> [{'count': u'2', 'manu': <Manufacturer: Manu1>}, {'count': u'4',
>> 'manu': <Manufacturer: Manu2>}, {'count': u'2', 'manu': <Manufacturer:
>> Manu3>}, {'count': u'2', 'manu': <Manufacturer: Manu2>}]
...

This sounds like a homework assignment.  If you're having trouble
with this, sending mail from Python is really going to be a headache
for you.

You want something like

inlist = [{'count': u'2', 'manu': <Manufacturer: Manu1>}, {'count': u'4',
 > 'manu': <Manufacturer: Manu2>}, {'count': u'2', 'manu': <Manufacturer:
 > Manu3>}, {'count': u'2', 'manu': <Manufacturer: Manu2>}]

outdict = {}
for mandict in inlist :
	if not outdict.has_key(mandict[manu])	# if first for this manu
		outdict[mandict[manu]] = []	# make empty list
	outdict[mandict[manu]].append(mandict)	# append entry to list	

You now have the desired result in a dictionary, which you can convert to a list
if you like.

If it's a production job, and the number of manufacturers is large,
you're probably better off using a database like MySQL, or some
mail merge program.

				John Nagle



More information about the Python-list mailing list