Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Summing class instances inside a list
    text
    copied!<p>I am familiar with the built-in sum() function for lists and have used it before, eg:</p> <pre><code>sum(list1[0:41]) </code></pre> <p>when the list contains integers, but I'm in a situation where I have instances from a Class and I need them summed.</p> <p>I have this Class:</p> <pre><code>class DataPoint: def __init__(self, low, high, freq): self.low = low self.high = high self.freq = freq </code></pre> <p>They all refer to floats from an XML file and these instances later go into a list in my code.</p> <p>So for instance, I want to be able to do something like:</p> <pre><code>sum(list[0:41].freq) </code></pre> <p>where the list contains the Class instances. </p> <p>I'm also trying to get it in a loop so that the second number in the range of the sum() goes up each time, eg:</p> <pre><code>for i in range(len(list)): sum(list[0:i+1].freq) </code></pre> <p>Anyone know how I can get around this or if there is another way to do it? </p> <p>Thanks!</p> <p><strong>UPDATE:</strong></p> <p>Thanks for all the responses, I'll try to provide something more concrete than the conceptual stuff I put up there first:</p> <pre><code># Import XML Parser import xml.etree.ElementTree as ET # Parse XML directly from the file path tree = ET.parse('xml file') # Create iterable item list items = tree.findall('item') # Create class for historic variables class DataPoint: def __init__(self, low, high, freq): self.low = low self.high = high self.freq = freq # Create Master Dictionary and variable list for historic variables masterDictionary = {} # Loop to assign variables as dictionary keys and associate their values with them for item in items: thisKey = item.find('variable').text thisList = [] masterDictionary[thisKey] = thisList for item in items: thisKey = item.find('variable').text newDataPoint = DataPoint(float(item.find('low').text), float(item.find('high').text), float(item.find('freq').text)) masterDictionary[thisKey].append(newDataPoint) # Import random module for pseudo-random number generation import random diceDictionary = {} # Dice roll for historic variables for thisKey in masterDictionary.keys(): randomValue = random.random() diceList = [] diceList = masterDictionary[thisKey] for i in range(len(diceList)): if randomValue &lt;= sum(l.freq for l in diceList[0:i+1]): diceRoll = random.uniform(diceList[i].low, diceList[i].high) diceDictionary[thisKey].append(diceRoll) </code></pre> <p>I am basically trying to create a dictionary of dice rolls to match the keys of my master dictionary with the data. The freq instance of my Class refers to probabilities of certain bins being applied and are determined by a dice roll (random number). This is what the summing is for.</p> <p>Maybe this helps clear up my intention? The "i" in the summing example will be whatever the number of data points for a certain variable.</p> <p>Once I have the dictionaries of which rolls were selected in my out loop (not shown here), I'll apply it to code below to make something meaningful.</p> <p>Let me know if there is still any confusion about my intention. I will try a few of these suggestions out, but maybe someone can break it down to the simplest form considering what I've provided.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload