Python Forum
Sum only some values from a two-dimensional list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum only some values from a two-dimensional list
#1
Hello,
I'm a newbie. I have a two-dimensional list like this
DATA = 0
# quality control flag
QC = 1
samples = [[0.1, 33.3, 4.4], [1, 0, 2]]
where 3 samples and their 3 quality flags are collected. To calculate the average of all the data, I usually do:
mean = sum (map (float, samples[DATA])) / float (len (samples[DATA]))
But now I want to calculate the average using only the data that have flag > 0

I appreciate any help.
Reply
#2
zero_data = [data for data, flag in zip(*samples) if flag > 0]
average = sum(zero_data) / len(zero_data)
print(average)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thank you very much, just what I needed!
I did not know "zip" in Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,164 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,246 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,161 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,666 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,073 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  How to quantize a 4 dimensional array? PythonNPC 2 1,624 Apr-23-2022, 04:34 PM
Last Post: Gribouillis
  How to create 2 dimensional variables in Python? plumberpy 5 1,859 Mar-31-2022, 03:15 AM
Last Post: plumberpy
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,001 Jan-19-2022, 08:33 AM
Last Post: menator01
  Slicing a 2 dimensional array Scott 2 1,663 Jan-12-2022, 07:18 AM
Last Post: paul18fr
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,121 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020