[Tutor] Clean up loop and search in a list
Markus Rosenstihl
mrosenstihl at macnews.de
Thu Oct 13 18:56:57 CEST 2005
Hi,
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?
Regards,
Markus
phone={ "Markus":"1234561",\
"Eckhard":"1234562",\
"Robert":"1234563",\
"Lei":"1234564"
}
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])
re_month=re.compile("Monatliche")
re_misc=re.compile("Sonstige")
for line in rechnung:
if len(filter(re_month.search, line)) == 0: # skip the monthly
costs
# 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_list.append(i)
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_list.append(i)
monthly += float(rechnung[i][10].replace( ',' , '.')) # replace
commata with dots then add to the sum
i=i+1
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