M
M
Mr_Pod2018-03-18 22:17:04
Python
Mr_Pod, 2018-03-18 22:17:04

How can you count the amount of something that has the same name?

For example, there is a list \ tuple of strings:

("2014-01-01 01:12:13 181",
 "2014-01-02 20:11:10 600",
 "2014-01-03 01:12:13 6009",
 "2014-01-03 12:13:55 200")

You need to calculate the sum of the values ​​​​that are separated by the last space, for each day (by the values ​​​​separated by the first space), but as short as possible .
I know how to do this, but it seems to me that you can use fewer lines for this. My option:
def total_cost(calls):
    cost = dict()
    for i in calls:
        i = i.split()
        cost[i[0]] = cost.get(i[0], 0) + int(i[2])
    return cost

print(total_cost(("2014-01-01 01:12:13 181",
                  "2014-01-02 20:11:10 600",
                  "2014-01-03 01:12:13 6009",
                  "2014-01-03 12:13:55 200")))

So, is it possible to make it shorter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-03-18
@dimonchik2013

https://stackoverflow.com/questions/43391333/list-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question