简体   繁体   中英

count the occurrences of elements in a nested list, and perform calculations

The title is kind of misleading because I don't really know how to describe this

Let say that I have a nested list that looks like this:

a = [[1234,'1/8/2014'],[4123,'1/3/2014'],[5754,'1/12/2014'],[8548,'11/8/2014'],[9469,'11/9/2013'],[3564,'1/8/2013']]

In this nested list, there are 4 lists with year 2014, and 2 lists with year 2013.

I want to get an average of each year's value. So for year 2014, I want to do,

(1234 + 4123 + 5754 + 8548) / 4 

and for year 2013,

(9469 + 3564) / 2

I need to get the occurrences of each year because I need to average out the sums for each year. At the end, I want something like,

new = [[4914.75, '2014'],[6516.5, '2013']]

Please note that dates are not in '01/03/2014', but just '1/3/2014'

How can this be done?

You can use Pandas to do this.

import pandas as pd

a = [[1234,'1/8/2014'],[4123,'1/3/2014'],[5754,'1/12/2014'],[8548,'11/8/2014'],[9469,'11/9/2013'],[3564,'1/8/2013']]

df = pd.DataFrame(a)

df[1] = pd.to_datetime(df[1])

df = df.set_index(1)

df.groupby(df.index.year.astype(str)).mean()\
  .reset_index().values.tolist()

Output:

[['2013', 6516.5], ['2014', 4914.75]]

The above answer works and if you are not comfortable using pandas, you can refer this one.

a = [[1234,'1/8/2014'],[4123,'1/3/2014'],[5754,'1/12/2014'],[8548,'11/8/2014'],[9469,'11/9/2013'],[3564,'1/8/2013']]

data = {}
result = []
for item in a:
    year = item[1].split('/')[-1]
    data[year] = data.get(year, []) + [item[0]]

for key in data.keys():
    items = data.get(key)
    avg = sum(items)/len(items)
    result.extend([key, avg])

print(result)

Try this (it assumes the inner lists are always of length 2 and that the 2nd one is a date):

from collections import defaultdict

cumulatives = defaultdict(int)
counts = defaultdict(int)
for (amount, dt) in a:
    key = dt[-4:]
    cumulatives[key] += amount
    counts[key] += 1.0

output = [[cumulatives[key]/counts[key], key] for key in cumulatives.keys()]
print(output)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM