I have a list:
data=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]
This list (data) consists of lists (data[x]), which consists of lists (data[x][x]) containing the same word together, each with a number with it. I want to find the average of the number for each word (any floats rounded to integer).
For example, there are 2 cakes in the list (data[1]) and in the same list are the numbers 5 and 8. I want the average out of those two numbers, which would be 7, and so data[1] would be changed to ["cake",7]
If this is done for each element in the list, the result should be:
data=[["apple",2],["cake",7],["chocolate",7],["grapes",6]]
How could this be done? Thanks :3
You can use zip
within a list comprehension:
>>> from __future__ import division
>>> [[set(i).pop(),round(sum(j)/len(j),0)] for i,j in [zip(*i) for i in data]]
[['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]
The zip
function here would separate your values from names in nested lists:
>>> [zip(*i) for i in data]
[[('apple',), (2,)], [('cake', 'cake'), (5, 8)], [('chocolate', 'chocolate', 'chocolate'), (3, 9, 10)], [('grapes',), (6,)]]
Then you can loop over them and calculate the avg and use set
to select the set of names!also note that is have used round
function as you wanted the avg for the cake
to be 7
as its 6.5 .the round function will do the job for you
You can try
x=[ [["apple",2]], [["cake",5],["cake",8]], [["chocolate",3],["chocolate",9],["chocolate",10]],[["grapes",6]] ]
y=[]
for i in x:
avg=0
c=0
for k in i:
avg=k[1]+avg
c=c+1
avg=avg/c
y.append([k[0],avg])
avg=0
c=0
print y
Output:
[['apple', 2], ['cake', 6], ['chocolate', 7], ['grapes', 6]]
You can do it with 2 list comprehensions and numpy.mean
function or statistics.mean
function if you use python 3:
In [1]: [[x[0][0], round(np.mean([y[1] for y in x]))] for x in data]
Out[1]: [['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]
If you don't have mean function, you can calculate it like this
In [2]: tmp = ([x[0][0], [y[1] for y in x]] for x in data)
In [3]: [[x[0], round(sum(x[1])*1.0/len(x[1]))] for x in tmp]
Out[3]: [['apple', 2.0], ['cake', 7.0], ['chocolate', 7.0], ['grapes', 6.0]]
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.