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.
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]],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With