[Tutor] Clean up loop and search in a list

Markus Rosenstihl mrosenstihl at macnews.de
Thu Oct 13 18:56:57 CEST 2005

I wrote a program (see below) to analyse my phone bill, which is shared 
by three others and I don't  know if there is a way to make lines like 
this nicer:
if len(filter(re_name.search, line)) > 0 and len(filter(re_misc.search, 
line)) == 0

Is there for example a way to search the whole list and give back all 
the line numbers containing a string?


phone={ "Markus":"1234561",\

for name in phone.keys():	# loop through the list for all names
	euro=0 # set at each run to 0
	i=0 # Line number

	# compile REs
	re_name = re.compile(phone[name])
	for line in rechnung:
		if len(filter(re_month.search, line)) == 0:   	# skip the monthly 
#		if "Monatliche" in line:
			if len(filter(re_name.search, line)) > 0 and 
len(filter(re_misc.search, line)) == 0:
#			if phone[name] in line:
				euro += float(rechnung[i][10].replace( ',' , '.'))
			if len(filter(re_misc.search, line)) > 0:	# misc services
				if i not in misc_list: # add misc fees only once
					misc += float(rechnung[i][10].replace( ',' , '.'))
		elif len(filter(re_month.search, line)) > 0:
			if i not in monthly_list: # add monthly occuring fees only once
				monthly += float(rechnung[i][10].replace( ',' , '.')) # replace 
commata with dots then add to the sum
	per_user = ( euro + ( monthly + misc )/ divider )*1.16
	total += per_user
	print name,": ", per_user

If everything seems to be going well,
you  obviously don't know what the hell is going on

More information about the Tutor mailing list