| | |
multiplication of dimensional analysis
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
:mrgreen: hi im am tryingto create a dimensional analysis thing going on and so far i have this
and my output is so far like this:
>>> t = dimensions([('j',2),('km',1), ('f',5)])
>>> w = dimensions([('j',2),('f',1)])
>>> w.mult(t)
[('j', 4), ('f', 6)] need to append km1.... what am i doing wrong anyone? thanks for your help
Python Syntax (Toggle Plain Text)
def mult(self,other): tempTuple1=list() tempTuple2=list() i=0 for unit,value in other.expr: for unit2,value2 in self.expr: if unit2 == unit: tempTuple1.append((unit, value+value2)) #self.expr[i]=tempTuple1 ## for unit2,value2 in self.expr: ## for unit,value in other.expr: ## if unit2 != unit: ## tempTuple1.append((unit2,value2)) print tempTuple1
and my output is so far like this:
>>> t = dimensions([('j',2),('km',1), ('f',5)])
>>> w = dimensions([('j',2),('f',1)])
>>> w.mult(t)
[('j', 4), ('f', 6)] need to append km1.... what am i doing wrong anyone? thanks for your help
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
class dimensions: def __init__(self, listofTuples): self.expr = listofTuples def sub(self,other): for value in self.expr: if not value in other.expr: return "incompatible" return self.expr def mult(self,other): tempTuple1=list() tempTuple2=list() i=0 for unit,value in other.expr: for unit2,value2 in self.expr: if unit2 == unit: tempTuple1.append((unit, value+value2)) tempTuple2.append(()) ## for unit2,value2 in self.expr: ## for unit,value in other.expr: ## if unit2 != unit: ## tempTuple1.append((unit2,value2)) #print tempTuple1
here is my code now, i hope this is better
I don't know what editor you are using, but for some odd reason you lost all the indentations (critical for Python to work). I doctored up your code and added a number of temporary print statements, always good for troubleshooting. I noticed that your mult() has no return statement, so result = None is the returned.
Here is the doctored code:
How new are you to Python?
Here is the doctored code:
Python Syntax (Toggle Plain Text)
class dimensions: def __init__(self, listofTuples): self.expr = listofTuples def sub(self, other): for value in self.expr: if not value in other.expr: return "incompatible" return self.expr def mult(self, other): tempTuple1=list() tempTuple2=list() print tempTuple1 # [] print tempTuple2 # [] print other.expr # [('j', 2), ('km', 1), ('f', 5)] print self.expr # [('j', 2), ('f', 1)] i=0 for unit,value in other.expr: for unit2,value2 in self.expr: if unit2 == unit: tempTuple1.append((unit, value+value2)) tempTuple2.append(()) ## for unit2,value2 in self.expr: ## for unit,value in other.expr: ## if unit2 != unit: ## tempTuple1.append((unit2,value2)) print tempTuple1 # [('j', 4), ('f', 6)] print tempTuple2 # [(), ()] # what does mult() return????? t = dimensions([('j',2),('km',1), ('f',5)]) print t # <__main__.dimensions instance at 0x00AE6BE8> w = dimensions([('j',2),('f',1)]) print w # <__main__.dimensions instance at 0x00AE6EE0> print w.mult(t) # gives None, correct since there is no return
drink her pretty
•
•
Join Date: Jul 2006
Posts: 34
Reputation:
Solved Threads: 0
sorry i just copied it and pasted it from my textedit. i was using the command line in gentoo.... i was working mostly with biopython and zope, never really persay did simple things in python. i just kinda skipped that and learned biopython syntax and the necessary python syntax to do my internship.
what i would like it to return is this: for example:
t = dimensions([('j',2),('km',1), ('f',5)])
w = dimensions([('j',2),('f',1)])
print w.mult(t) # gives None, correct since there is no return
return tempTuple1.append(the value that does not have a pair to be added with ('km',1)]
([('j',4),('f',6),('km',1)])
thanks for your help. what book would you recommend to learn python? i read some of the tutorials and they help but a book would be good.
what i would like it to return is this: for example:
t = dimensions([('j',2),('km',1), ('f',5)])
w = dimensions([('j',2),('f',1)])
print w.mult(t) # gives None, correct since there is no return
return tempTuple1.append(the value that does not have a pair to be added with ('km',1)]
([('j',4),('f',6),('km',1)])
thanks for your help. what book would you recommend to learn python? i read some of the tutorials and they help but a book would be good.
Last edited by msaenz; Sep 22nd, 2006 at 7:10 pm.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- How to implement multi-dimensional arrays in Python?? (Python)
- multiplication tutorial (C++)
Other Threads in the Python Forum
- Previous Thread: Click event >> window appears?
- Next Thread: Hep finding C++ Projects
| Thread Tools | Search this Thread |
address aliased anydbm bash beginner bits calling casino changecolor class clear conversion convert corners count cturtle cursor curves definedlines dictionary digital dynamic dynamically events examples excel external file float format frange function gui handling hints homework i/o iframe import info input java line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton string strings subprocess table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode urllib urllib2 valueerror variable web-scrape whileloop word wxpython






