Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average of elements in a list in the second column

I have a 2 dimensional list of file names and file sizes and I'm trying to find the average of all of the elements in the second column and after searching for about 2 hours can't find any help.

Here is my list:

input: print fileArr

output: [[['david.ppt'], [56437456]], [['terry.dmg'], [54485656]], [['mike.doc'], [6593543]], [['randy.docx'], [5968434]], [['rick.exe'], [4538565]], [['chris.txt'], [2569437]], [['sarah.txt'], [458667]], [['fred.png'], [54966]], [['terry.dat'], [4596]], [['flyer.jpg'], [4305]]]

I have tried multiple possible solutions with no avail.

like image 320
Edsel Norwood Avatar asked Dec 23 '22 10:12

Edsel Norwood


1 Answers

This might do it:

fileArr = [[['david.ppt'], [56437456]], [['terry.dmg'], [54485656]], [['mike.doc'], [6593543]], [['randy.docx'], [5968434]], [['rick.exe'], [4538565]], [['chris.txt'], [2569437]], [['sarah.txt'], [458667]], [['fred.png'], [54966]], [['terry.dat'], [4596]], [['flyer.jpg'], [4305]]]

average = sum(second[0] for first, second in fileArr) // len(fileArr)
print(average)

The key observation, as Willem commented, is that each of your items are wrapped in an extra, extraneous, list. So the first pair isn't what one might expect:

['david.ppt', 56437456],

But instead is a pair of single-element lists:

[['david.ppt'], [56437456]],
like image 163
Robᵩ Avatar answered Dec 26 '22 00:12

Robᵩ