Hi
I have a function for calculating the standard deviation of a list. I was wanting to create this as an object and inherit from pythons list object.
I'm pretty new to the whole OOP concept, can someone please have a look at my code to see why it prints an address rather than a value and where I'm messing this OOP thing up.
Thanks -
class StandardDev(list):
-
def __init__(self, theList):
-
self.theList = theList
-
-
def standardDev(self):
-
import math
-
sums = 0
-
n = len(self)
-
#Calculate the mean average
-
ma = (1/float(n))*sum(self)
-
for i, j in enumerate(self):
-
sums += ((self[i]-ma)**2)
-
return round(math.sqrt((1/float(n))*(sums)),2)
-
-
-
a = (5,6,8,9)
-
-
b = StandardDev(a)
-
print b.standardDev
-
-
6 1453 bvdet 2,851
Expert Mod 2GB
Hi
I have a function for calculating the standard deviation of a list. I was wanting to create this as an object and inherit from pythons list object.
I'm pretty new to the whole OOP concept, can someone please have a look at my code to see why it prints an address rather than a value and where I'm messing this OOP thing up.
Thanks -
class StandardDev(list):
-
def __init__(self, theList):
-
self.theList = theList
-
-
def standardDev(self):
-
import math
-
sums = 0
-
n = len(self)
-
#Calculate the mean average
-
ma = (1/float(n))*sum(self)
-
for i, j in enumerate(self):
-
sums += ((self[i]-ma)**2)
-
return round(math.sqrt((1/float(n))*(sums)),2)
-
-
-
a = (5,6,8,9)
-
-
b = StandardDev(a)
-
print b.standardDev
-
-
- class StandardDev(list):
-
def __init__(self, theList):
-
self.theList = theList
-
-
def standardDev(self):
-
import math
-
sums = 0
-
n = len(self.theList)
-
#Calculate the mean average
-
ma = (1/float(n))*sum(self.theList)
-
for i, j in enumerate(self.theList):
-
sums += ((self.theList[i]-ma)**2)
-
return round(math.sqrt((1/float(n))*(sums)),2)
-
-
-
a = (5,6,8,9)
-
-
b = StandardDev(a)
-
print b.standardDev()
-
-
'''
-
>>> 1.58
-
>>>
-
'''
cheers much again bvdet. your awesome!
sorry, last question. How best is it to catch integer division by zero errors on this code?
Thanks
bvdet 2,851
Expert Mod 2GB
sorry, last question. How best is it to catch integer division by zero errors on this code?
Thanks
It looks like the only way that could happen is when the list is empty. A simple if statement will take care of that: - n = len(self.theList)
-
if n:
-
....... do stuff......
cheers mate :). I was thinking of using try except, but this works fine.
It looks like the only way that could happen is when the list is empty. A simple if statement will take care of that: - n = len(self.theList)
-
if n:
-
....... do stuff......
Or simply: - if self.theList:
-
....... do stuff......
Sign in to post your reply or Sign up for a free account.
Similar topics
by: JustSomeGuy |
last post by:
this doesn't want to compile....
class image : public std::list<element>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element...
|
by: Noah Coad [MVP .NET/C#] |
last post by:
How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain...
|
by: Opa |
last post by:
I have tried for two days to solve this problem with no luck.
I have a singleton class which has some events declared. When I inherit
from this class the events don't seem to come along with it....
|
by: Andrew McLellan |
last post by:
I think I must be missing something about generics, perhaps just about the
syntax. I'd like to derive a class MyList from System.Collections.Generic so
that it can only contain instance of MyItem....
|
by: YYZ |
last post by:
I've got a form in a class library, form named frmPCGGen. I'm inheriting from
this form (for some base functionality) in a different project. When I inherit
from the form, I obviously give it a...
|
by: Kyle Novak |
last post by:
I have a question about strongly typed objects when looping through a
collection based on the CollectionBase object and using a For..Each loop.
I have 2 objects:
-Invoice: Holds all properties...
|
by: Adrian Hawryluk |
last post by:
Hi all,
What is everyone's opinion of const inheriting? Should the object that
a pointer is pointing at inherit the constness of the pointer? Such as
in the case of a class having a pointer...
|
by: Mangabasi |
last post by:
Howdy,
I would like to create a Point class that lets me use Point instances
like the following example.
3
4
1
3
4
|
by: AalaarDB |
last post by:
struct base
{
int x, y, z;
base() {x = 0; y = 0; z = 0;};
base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;};
};
struct intermediate1 : public virtual base {};
struct intermediate2 :...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |