I need to output these deposits in order based on the club number. take a look at the first item in deposits (0434512). The first two digits represent the club number(in this case it is club number 04). The remaining digits represent the deposit made to the account(34512). This needs to be rounded so it should be outputted as 4 $345.12
deposits = ["0434512", "03145234", "012341347", "0511112345", "0475746","03654534", "02112"]
deposits.sort()
for i in deposits:
print i[1],
print int(i[2:])/100.
1 23413.47
2 1.12
3 1452.34
3 6545.34
4 345.12
4 757.46
5 111123.45
I thought i had this completed but turns out that i need to add the deposits made to the same club. so i would need to add 1452.34 and 6545.34 together and same with club 4.
Thanks in advance!
Never use a list to do a dictionary's job:
dictionary_repr = {}
for item in deposits:
if item in dictionary_repr:
# clump the clubs earnings to gain total earnings
dictionary_repr[int(item[1])] += int(item[2:]/100.00)
else:
# create the first instance of earnings
dictionary_repr[int(item[1])] = int(item[2:]/100.00)
Now you can just iterate through the dictionary and print your results:
for club, amount in dictionary_repr.items(): # or iteritems in Py 2.7
print(club, amount) # or print club, amount in Py 2.7
I think you should convert your club numbers to int, but solution based on your code can be like this
from collections import defaultdict
deposits = ["0434512", "03145234", "012341347", "0511112345", "0475746", "03654534", "02112"]
summary = defaultdict(float)
for i in deposits:
summary[i[:2]] += int(i[2:])/100.
#sort by club number
sorted_summary = sorted(summary.items(), key=lambda item: item[0])
for key, value in sorted_summary:
print key, value
#1 23413.47
#2 1.12
#3 7997.68
#4 1102.58
#5 111123.45
You can use a dictionary:
deposits = ["0434512", "03145234", "012341347", "0511112345", "0475746","03654534", "02112"]
deposits.sort()
from collections import OrderedDict
mydict = OrderedDict()
for i in deposits:
mydict[i[:2]] = 0
for i in deposits:
mydict[i[:2]] += float(i[2:])/100
Output:
>>> for i,j in mydict.items():
print(i,j)
01 23413.47
02 1.12
03 7997.68
04 1102.58
05 111123.45
Python 2:
>>> for i,j in mydict.iteritems():
print i,j
01 23413.47
02 1.12
03 7997.68
04 1102.58
05 111123.45
OrderedDict
is used to keep the clubs in number order.
Here are some links you might be interested in: Dictionaries , OrderedDict
mydict = OrderedDict
creates an ordered dictionary called mydict
.
for i in deposits:
mydict[i[:2]] = 0
This for
loop then creates a key using the first 2 characters in each entry in deposits
. Ie The club number. It gives every club a value of 0
to start with.
for i in deposits:
mydict[i[:2]] += float(i[2:])/100
This second for
loop goes through deposits
and adds everything after the 2nd character to the corresponding key's value in mydict
. This only works because a dictionary can't have 2 keys with the same name.
I hope this helps you understand. It is relatively simple once you understand how dictionaries work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.