How to find mean of an array which has two elements in Python?
up vote
3
down vote
favorite
I need to find mean of an array which is like: [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
Result should be like; [('a', 4.5), ('b', 4)]
python numpy
add a comment |
up vote
3
down vote
favorite
I need to find mean of an array which is like: [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
Result should be like; [('a', 4.5), ('b', 4)]
python numpy
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I need to find mean of an array which is like: [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
Result should be like; [('a', 4.5), ('b', 4)]
python numpy
I need to find mean of an array which is like: [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
Result should be like; [('a', 4.5), ('b', 4)]
python numpy
python numpy
edited yesterday
Willem Van Onsem
138k16129220
138k16129220
asked yesterday
bukowski
108215
108215
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
up vote
2
down vote
accepted
Raw solution without additional libraries could look like this:
def mean(l):
result = {}
for key, value in l:
if key not in result:
result[key] =
result[key].append(value)
return [(k, sum(v)/len(v)) for k, v in result.items()]
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
m = mean(lst)
print(m)
# [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
3
down vote
You can put all your tuples in a defaultdict, using the first value to group them into a list and then calculate the mean:
from collections import defaultdict
d = defaultdict(list)
for key,value in [('a', 5), ('b', 2), ('a', 4), ('b', 6)]:
d[key].append(value)
mean =
for k,values in d.items():
# mean.append((k,sum(values)/float(len(values)))) #python 2
mean.append((k,sum(values)/len(values)))
print(mean) # [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
2
down vote
We can use pandas for this:
import pandas as pd
pd.DataFrame(data).groupby(0)[1].mean().to_dict()
this will give us:
>>> pd.DataFrame(data).groupby(0)[1].mean().to_dict()
{'a': 4.5, 'b': 4.0}
or we can convert this to a list of 2-tuples with:
list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
which gives:
>>> list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
[('a', 4.5), ('b', 4.0)]
The above is thus more a "declarative" approach: we specify what we want, not much how we want to do this.
add a comment |
up vote
2
down vote
You can collect the numbers with a collections.defaultdict()
, then apply statistics.mean()
on each group of numbers:
from statistics import mean
from collections import defaultdict
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
means = [(k, mean(v)) for k, v in d.items()]
print(means)
# [('a', 4.5), ('b', 4)]
You can also use itertools.groupby()
to group the tuples:
from statistics import mean
from itertools import groupby
from operator import itemgetter
lst = [("a", 5), ("b", 2), ("a", 4), ("b", 6)]
means = [
(k, mean(map(itemgetter(1), g)))
for k, g in groupby(sorted(lst, key=itemgetter(0)), key=itemgetter(0))
]
print(means)
[('a', 4.5), ('b', 4)]
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
@bukowski: no,defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.
– Willem Van Onsem
yesterday
add a comment |
up vote
2
down vote
If you wish, you can also try the below reusable code (without using any external libraries).
>>> def get_mean(l):
... d = {}
... for k, v in l:
... if k in d:
... d[k].append(v)
... else:
... d[k] = [v]
... result = [(k, sum(d[k])/len(d[k])) for k in d]
... return result
...
>>> l = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
>>> new_l = get_mean(l)
>>> new_l
[('a', 4.5), ('b', 4.0)]
>>>
Grouping withsetdefault()
would be cleaner IMO. You avoid the if/else that way.
– RoadRunner
yesterday
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Raw solution without additional libraries could look like this:
def mean(l):
result = {}
for key, value in l:
if key not in result:
result[key] =
result[key].append(value)
return [(k, sum(v)/len(v)) for k, v in result.items()]
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
m = mean(lst)
print(m)
# [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
2
down vote
accepted
Raw solution without additional libraries could look like this:
def mean(l):
result = {}
for key, value in l:
if key not in result:
result[key] =
result[key].append(value)
return [(k, sum(v)/len(v)) for k, v in result.items()]
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
m = mean(lst)
print(m)
# [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Raw solution without additional libraries could look like this:
def mean(l):
result = {}
for key, value in l:
if key not in result:
result[key] =
result[key].append(value)
return [(k, sum(v)/len(v)) for k, v in result.items()]
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
m = mean(lst)
print(m)
# [('a', 4.5), ('b', 4.0)]
Raw solution without additional libraries could look like this:
def mean(l):
result = {}
for key, value in l:
if key not in result:
result[key] =
result[key].append(value)
return [(k, sum(v)/len(v)) for k, v in result.items()]
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
m = mean(lst)
print(m)
# [('a', 4.5), ('b', 4.0)]
answered yesterday
Looioe
845
845
add a comment |
add a comment |
up vote
3
down vote
You can put all your tuples in a defaultdict, using the first value to group them into a list and then calculate the mean:
from collections import defaultdict
d = defaultdict(list)
for key,value in [('a', 5), ('b', 2), ('a', 4), ('b', 6)]:
d[key].append(value)
mean =
for k,values in d.items():
# mean.append((k,sum(values)/float(len(values)))) #python 2
mean.append((k,sum(values)/len(values)))
print(mean) # [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
3
down vote
You can put all your tuples in a defaultdict, using the first value to group them into a list and then calculate the mean:
from collections import defaultdict
d = defaultdict(list)
for key,value in [('a', 5), ('b', 2), ('a', 4), ('b', 6)]:
d[key].append(value)
mean =
for k,values in d.items():
# mean.append((k,sum(values)/float(len(values)))) #python 2
mean.append((k,sum(values)/len(values)))
print(mean) # [('a', 4.5), ('b', 4.0)]
add a comment |
up vote
3
down vote
up vote
3
down vote
You can put all your tuples in a defaultdict, using the first value to group them into a list and then calculate the mean:
from collections import defaultdict
d = defaultdict(list)
for key,value in [('a', 5), ('b', 2), ('a', 4), ('b', 6)]:
d[key].append(value)
mean =
for k,values in d.items():
# mean.append((k,sum(values)/float(len(values)))) #python 2
mean.append((k,sum(values)/len(values)))
print(mean) # [('a', 4.5), ('b', 4.0)]
You can put all your tuples in a defaultdict, using the first value to group them into a list and then calculate the mean:
from collections import defaultdict
d = defaultdict(list)
for key,value in [('a', 5), ('b', 2), ('a', 4), ('b', 6)]:
d[key].append(value)
mean =
for k,values in d.items():
# mean.append((k,sum(values)/float(len(values)))) #python 2
mean.append((k,sum(values)/len(values)))
print(mean) # [('a', 4.5), ('b', 4.0)]
answered yesterday
Patrick Artner
17.5k51739
17.5k51739
add a comment |
add a comment |
up vote
2
down vote
We can use pandas for this:
import pandas as pd
pd.DataFrame(data).groupby(0)[1].mean().to_dict()
this will give us:
>>> pd.DataFrame(data).groupby(0)[1].mean().to_dict()
{'a': 4.5, 'b': 4.0}
or we can convert this to a list of 2-tuples with:
list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
which gives:
>>> list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
[('a', 4.5), ('b', 4.0)]
The above is thus more a "declarative" approach: we specify what we want, not much how we want to do this.
add a comment |
up vote
2
down vote
We can use pandas for this:
import pandas as pd
pd.DataFrame(data).groupby(0)[1].mean().to_dict()
this will give us:
>>> pd.DataFrame(data).groupby(0)[1].mean().to_dict()
{'a': 4.5, 'b': 4.0}
or we can convert this to a list of 2-tuples with:
list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
which gives:
>>> list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
[('a', 4.5), ('b', 4.0)]
The above is thus more a "declarative" approach: we specify what we want, not much how we want to do this.
add a comment |
up vote
2
down vote
up vote
2
down vote
We can use pandas for this:
import pandas as pd
pd.DataFrame(data).groupby(0)[1].mean().to_dict()
this will give us:
>>> pd.DataFrame(data).groupby(0)[1].mean().to_dict()
{'a': 4.5, 'b': 4.0}
or we can convert this to a list of 2-tuples with:
list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
which gives:
>>> list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
[('a', 4.5), ('b', 4.0)]
The above is thus more a "declarative" approach: we specify what we want, not much how we want to do this.
We can use pandas for this:
import pandas as pd
pd.DataFrame(data).groupby(0)[1].mean().to_dict()
this will give us:
>>> pd.DataFrame(data).groupby(0)[1].mean().to_dict()
{'a': 4.5, 'b': 4.0}
or we can convert this to a list of 2-tuples with:
list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
which gives:
>>> list(pd.DataFrame(data).groupby(0)[1].mean().to_dict().items())
[('a', 4.5), ('b', 4.0)]
The above is thus more a "declarative" approach: we specify what we want, not much how we want to do this.
answered yesterday
Willem Van Onsem
138k16129220
138k16129220
add a comment |
add a comment |
up vote
2
down vote
You can collect the numbers with a collections.defaultdict()
, then apply statistics.mean()
on each group of numbers:
from statistics import mean
from collections import defaultdict
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
means = [(k, mean(v)) for k, v in d.items()]
print(means)
# [('a', 4.5), ('b', 4)]
You can also use itertools.groupby()
to group the tuples:
from statistics import mean
from itertools import groupby
from operator import itemgetter
lst = [("a", 5), ("b", 2), ("a", 4), ("b", 6)]
means = [
(k, mean(map(itemgetter(1), g)))
for k, g in groupby(sorted(lst, key=itemgetter(0)), key=itemgetter(0))
]
print(means)
[('a', 4.5), ('b', 4)]
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
@bukowski: no,defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.
– Willem Van Onsem
yesterday
add a comment |
up vote
2
down vote
You can collect the numbers with a collections.defaultdict()
, then apply statistics.mean()
on each group of numbers:
from statistics import mean
from collections import defaultdict
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
means = [(k, mean(v)) for k, v in d.items()]
print(means)
# [('a', 4.5), ('b', 4)]
You can also use itertools.groupby()
to group the tuples:
from statistics import mean
from itertools import groupby
from operator import itemgetter
lst = [("a", 5), ("b", 2), ("a", 4), ("b", 6)]
means = [
(k, mean(map(itemgetter(1), g)))
for k, g in groupby(sorted(lst, key=itemgetter(0)), key=itemgetter(0))
]
print(means)
[('a', 4.5), ('b', 4)]
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
@bukowski: no,defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.
– Willem Van Onsem
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
You can collect the numbers with a collections.defaultdict()
, then apply statistics.mean()
on each group of numbers:
from statistics import mean
from collections import defaultdict
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
means = [(k, mean(v)) for k, v in d.items()]
print(means)
# [('a', 4.5), ('b', 4)]
You can also use itertools.groupby()
to group the tuples:
from statistics import mean
from itertools import groupby
from operator import itemgetter
lst = [("a", 5), ("b", 2), ("a", 4), ("b", 6)]
means = [
(k, mean(map(itemgetter(1), g)))
for k, g in groupby(sorted(lst, key=itemgetter(0)), key=itemgetter(0))
]
print(means)
[('a', 4.5), ('b', 4)]
You can collect the numbers with a collections.defaultdict()
, then apply statistics.mean()
on each group of numbers:
from statistics import mean
from collections import defaultdict
lst = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
d = defaultdict(list)
for k, v in lst:
d[k].append(v)
means = [(k, mean(v)) for k, v in d.items()]
print(means)
# [('a', 4.5), ('b', 4)]
You can also use itertools.groupby()
to group the tuples:
from statistics import mean
from itertools import groupby
from operator import itemgetter
lst = [("a", 5), ("b", 2), ("a", 4), ("b", 6)]
means = [
(k, mean(map(itemgetter(1), g)))
for k, g in groupby(sorted(lst, key=itemgetter(0)), key=itemgetter(0))
]
print(means)
[('a', 4.5), ('b', 4)]
edited yesterday
answered yesterday
RoadRunner
8,49731137
8,49731137
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
@bukowski: no,defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.
– Willem Van Onsem
yesterday
add a comment |
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
@bukowski: no,defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.
– Willem Van Onsem
yesterday
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
Hi thanks for the answer I'm giving it a try but I wonder if d = deafultdict(list) shouldn't be d = deafultdict(lst) ?
– bukowski
yesterday
2
2
@bukowski: no,
defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.– Willem Van Onsem
yesterday
@bukowski: no,
defaultdict
takes a callable that constructs elements in case it is missing. We do not want to insert the data by default.– Willem Van Onsem
yesterday
add a comment |
up vote
2
down vote
If you wish, you can also try the below reusable code (without using any external libraries).
>>> def get_mean(l):
... d = {}
... for k, v in l:
... if k in d:
... d[k].append(v)
... else:
... d[k] = [v]
... result = [(k, sum(d[k])/len(d[k])) for k in d]
... return result
...
>>> l = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
>>> new_l = get_mean(l)
>>> new_l
[('a', 4.5), ('b', 4.0)]
>>>
Grouping withsetdefault()
would be cleaner IMO. You avoid the if/else that way.
– RoadRunner
yesterday
add a comment |
up vote
2
down vote
If you wish, you can also try the below reusable code (without using any external libraries).
>>> def get_mean(l):
... d = {}
... for k, v in l:
... if k in d:
... d[k].append(v)
... else:
... d[k] = [v]
... result = [(k, sum(d[k])/len(d[k])) for k in d]
... return result
...
>>> l = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
>>> new_l = get_mean(l)
>>> new_l
[('a', 4.5), ('b', 4.0)]
>>>
Grouping withsetdefault()
would be cleaner IMO. You avoid the if/else that way.
– RoadRunner
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
If you wish, you can also try the below reusable code (without using any external libraries).
>>> def get_mean(l):
... d = {}
... for k, v in l:
... if k in d:
... d[k].append(v)
... else:
... d[k] = [v]
... result = [(k, sum(d[k])/len(d[k])) for k in d]
... return result
...
>>> l = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
>>> new_l = get_mean(l)
>>> new_l
[('a', 4.5), ('b', 4.0)]
>>>
If you wish, you can also try the below reusable code (without using any external libraries).
>>> def get_mean(l):
... d = {}
... for k, v in l:
... if k in d:
... d[k].append(v)
... else:
... d[k] = [v]
... result = [(k, sum(d[k])/len(d[k])) for k in d]
... return result
...
>>> l = [('a', 5), ('b', 2), ('a', 4), ('b', 6)]
>>> new_l = get_mean(l)
>>> new_l
[('a', 4.5), ('b', 4.0)]
>>>
answered yesterday
hygull
2,67311126
2,67311126
Grouping withsetdefault()
would be cleaner IMO. You avoid the if/else that way.
– RoadRunner
yesterday
add a comment |
Grouping withsetdefault()
would be cleaner IMO. You avoid the if/else that way.
– RoadRunner
yesterday
Grouping with
setdefault()
would be cleaner IMO. You avoid the if/else that way.– RoadRunner
yesterday
Grouping with
setdefault()
would be cleaner IMO. You avoid the if/else that way.– RoadRunner
yesterday
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238083%2fhow-to-find-mean-of-an-array-which-has-two-elements-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password