Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6205415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:16:11+00:00 2026-05-24T05:16:11+00:00

I am familiar with the built-in sum() function for lists and have used it

  • 0

I am familiar with the built-in sum() function for lists and have used it before, eg:

sum(list1[0:41])

when the list contains integers, but I’m in a situation where I have instances from a Class and I need them summed.

I have this Class:

class DataPoint:
    def __init__(self, low, high, freq):
        self.low = low
        self.high = high
        self.freq = freq

They all refer to floats from an XML file and these instances later go into a list in my code.

So for instance, I want to be able to do something like:

sum(list[0:41].freq)

where the list contains the Class instances.

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:

for i in range(len(list)):
    sum(list[0:i+1].freq)

Anyone know how I can get around this or if there is another way to do it?

Thanks!

UPDATE:

Thanks for all the responses, I’ll try to provide something more concrete than the conceptual stuff I put up there first:

# 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 <= sum(l.freq for l in diceList[0:i+1]):
            diceRoll = random.uniform(diceList[i].low, diceList[i].high)
            diceDictionary[thisKey].append(diceRoll)

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.

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.

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.

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.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:16:13+00:00Added an answer on May 24, 2026 at 5:16 am

    Have you tried:

    sum(i.freq for i in items[0:41])
    

    If you need the cumulative sum of the last “i” elements, the following is the most efficient approach:

    sums = [items[0].freq]
    for i in items[1:]:
        sums.append(sums[-1] + i.freq)
    

    As other posters already have anticipated, it is a bad programming style to use name of builtins for your variables; I have replace list with items in the code above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not too familiar with networking in JAVA but what i'm basically trying to
I'm totally familiar in making static and dynamic websites. However, I have never dealt
I'm not familiar with ADO.NET Data Services but it looks usable. All I need
I'm familiar with JAD , and haven't found anything newer or better, but I
I have client that built a website that is part static html and part
We are very familiar with the built-in Maps application on Android and I wanted
We have built a webservice for a client that uses AutoCAD. They have a
I have built a webservice into my companies self developed CRM system that we
I am a student developer and I have built several installers for the company
I'm kind of familiar with both jQuery and JSON but I don't know if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.