0
Find the date for which the earning was maximum from the dictionary. How to create a list with only values of a dictionary
{{'regno':1,'name':'zoe','date':1,'cost':500},{'regno':2,'name':'mark','date':1,'cost':1500},{'regno':3,'name':'antony','date':3,'cost':1000},{'regno':4,'name':'zolta','date':2,'cost':500},{'regno':5,'name':'marcus','date':1,'cost':1500},{'regno':6,'name':'antlope','date':3,'cost':1000}} how do I make a dictionary from a given dictionary where key is date(1,2,3)and value is the earnings.then find maximum date . also how to create a dictionary with only values and one with only keys?
5 Réponses
+ 1
Hey there! Could you please show us your attempt?
+ 1
Yes sure
d={{'regno':1,'name':'zoe','date':1,'cost':500},{'regno':2,'name':'mark','date':1,'cost':1500},{'regno':3,'name':'antony','date':3,'cost':1000},{'regno':4,'name':'zolta','date':2,'cost':500},{'regno':5,'name':'marcus','date':1,'cost':1500},{'regno':6,'name':'antlope','date':3,'cost':1000}}
m=dict()
for i in d:
for j in d[i]:
if j=='date' and d[i][j]==1:
if j=='cost':
m['tot of 1']=m['tot of 1']+d[i]['cost']
print(m)
I don't know how to implement idea .I thought of creating a new dictionary and doing it but it does not work. if there is any other way I am open to it
+ 1
Maybe you misunderstood your own question? You asked for the maximum earnings and not for the maximum costs. Costs are pretty much the opposite of earnings. Then you asked "how to create a dictionary with only values and one with only keys". I mean, it's all up there, I'm not making things up.
Anyway, here's what I would do:
d = [{'regno':1,'name':'zoe','date':1,'cost':500},
{'regno':2,'name':'mark','date':1,'cost':1500},
{'regno':3,'name':'antony','date':3,'cost':1000},
{'regno':4,'name':'zolta','date':2,'cost':500},
{'regno':5,'name':'marcus','date':1,'cost':1500},
{'regno':6,'name':'antlope','date':3,'cost':1000}] # list of dicts
# create a set of unique dates
dates = {item['date'] for item in d} # output: {1, 2, 3}
# create a dict that containts the sum of costs for each day *.)
costs_per_day = {}
for item in d:
if item['date'] in costs_per_day.keys():
costs_per_day[item['date']] += item['cost']
else:
costs_per_day[item['date']] = item['cost']
# result: {1: 3500, 3: 2000, 2: 500}
# calculate max costs per day
max_cost = max(costs_per_day.values())
# result: 3500
# find day(s) with max costs
max_cost_days = [item for item in costs_per_day.keys() if costs_per_day[item] == max_cost]
print(max_cost_days)
# output: [1]
~~~
# *.) Here's a more elegant (and efficient) way to get the same result using a defaultdict:
from collections import defaultdict
costs_per_day = defaultdict(int)
for item in d:
costs_per_day[item['date']] += item['cost']
# result: defaultdict(<class 'int'>, {1: 3500, 3: 2000, 2: 500})
0
There's a couple of issues.
First, what you try to implement is a set {} of dictionaries. The assignment asks for a list [] of values of a dictionary. Then you ask how to make another dictionary from a given dictionary. Please make sure you know what kind of data type you want/need.
Also, you ask for maximum earnings but there's only an item 'cost' in your dict. Maybe maximum earnings equals minimum costs, but that's difficult to tell. You usually need several positions to calculate the earnings.
A dictionary with only keys or only values isn't possible. It's the opposite of what a dictionary means. A dictionary is always about key - value pairs. So you're probably looking for a list, a set etc.
0
thank you for your time but you misunderstood my question I asked two questions the first one being - I wanted to print the date in which there was maximum amount of cost .which directly translates to the day 1 in the given Dictionary.
next I just suggested a way to implement it (by suggesting to create a new dictionary) .I have already mentioned that it was just an idea and I am open to other suggestions .
regarding the second part of my question - it was my mistake I made a typo . I wanted to create a list (by mistake I asked for a dictionary)which contains only values of the dictionary (a nested list ) and another list with only key of a given dictionary.