![]() |
|
[Tut] How to Average a List of Lists in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Average a List of Lists in Python? (/thread-94911.html) |
[Tut] How to Average a List of Lists in Python? - xSicKxBot - 05-07-2020 How to Average a List of Lists in Python? Problem: You have a list of lists and you want to calculate the average of the different columns. Example: Given the following list of lists with four rows and three columns. Code: data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]]You want to have the average values of the three columns: Code: [average_col_1, average_col_2, average_col_3]There are three methods that solve this problem. You can play with them in the interactive shell and read more details below: https://repl.it/@finxter/listoflistsaverage?lite=true Method 1: Average in Python (No Library) A simple one-liner with list comprehension in combination with the Code: zip()Code: data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 1: Pure Python
res = [sum(x) / len(x) for x in zip(*data)]
print(res)
# [0.5, 0.75, 0.25]Do you love Python one-liners? I do for sure—I’ve even written a whole book about it with San Francisco Publisher NoStarch. Click to check out the book in a new tab: ![]() You can visualize the code execution and memory objects of this code in the following tool (just click “Next” to see how one step of the code unfolds). https://pythontutor.com/iframe-embed.html#code=data%20%3D%20%5B%5B0,%201,%200%5D,%0A%20%20%20%20%20%20%20%20%5B1,%201,%201%5D,%0A%20%20%20%20%20%20%20%20%5B0,%200,%200%5D,%0A%20%20%20%20%20%20%20%20%5B1,%201,%200%5D%5D%0A%0A%0Ares%20%3D%20%5Bsum%28x%29%20/%20len%28x%29%20for%20x%20in%20zip%28*data%29%5D%0Aprint%28res%29&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false Method 2: Average with NumPy Library You create a NumPy array out of the data and pass it to the np.average() function. Code: data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 2: NumPy
import numpy as np
a = np.array(data)
res = np.average(a, axis=0)
print(res)
# [0.5 0.75 0.25]The Code: axisCode: axis=0Code: axis=1Method 3: Mean Statistics Library + Map() Just to show you another alternative, here’s one using the Code: map()Code: zip(*data)Code: dataCode: data = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0]] # Method 3: Statistics + Map()
import statistics
res = map(statistics.mean, zip(*data))
print(list(res))
# [0.5, 0.75, 0.25]The Code: map(function, iterable)Code: functionCode: iterableCode: map()Where to Go From Here? Enough theory, let’s get some practice! To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? Practice projects is how you sharpen your saw in coding! Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people? Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner. Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |