Using elements from a list in a formula

A forum for general discussion of the Python programming language.

Using elements from a list in a formula

Postby martin87 » Wed Nov 06, 2013 3:24 pm

Hi Guys,

I am new to python but not to programming. I have a very simple problem that I can't seem to workout in python. With other programming languages it is simple but python INSISTS on making it complicated.

The first section of the code defines an empty array (or in python, a list) called timestep. The code then opens a comma delimited text file with input acceleration time histories (first column is time and the following columns are accelerations). The code then appends the values in the first column of the .csv file line-by-line to the list 'timestep'. NOTE: I AM VERY NEW TO PYTHON SO ANY HELP OR ADVISE ON WHAT I CURRENTLY HAVE WOULD BE SUPER, I LOVE TO LEARN :D.

Code: Select all
timestep = []

import csv
accfile = open('Book1.csv', 'rb')
reader = csv.reader(accfile)

for line in reader:
    t = line[0]
    timestep.append(t)

accfile.close()


Later in the code i need to use elements within the timestep list in a formula (complex fourier transform). In other programming languages this is SIMPLE (for example in VBA....... timestep(n)*1000 would give me the value of the nth timestep element multiplied by 1000. However......python does not seem to think like this.

Consider timestep(n) = 0.005. Instead of giving me the value of timestep(n) multiplied by 1000 which I would expect to give me 5, it gives me 0.0050.005

I suspect this is something to do with python reading the list as a script? I'm really really trying to enjoy python but so far it seems to throw convention out the window and is beginning to grind me down :( please can someone help and shed light on this.......and please don't mention the word 'tuple'.......seriously.....'TUPLE'....what were they thinking? Haha

Thanks in advance guys :)

Kind Regards,

Martin
martin87
 
Posts: 2
Joined: Wed Nov 06, 2013 2:50 pm

Re: Using elements from a list in a formula

Postby stranac » Wed Nov 06, 2013 4:03 pm

Code: Select all
>>> '0.123' * 3
'0.1230.1230.123'
>>> 0.123 * 3
0.369

Just convert your strings to numbers(you can use float()), and you should be fine.

Btw, what's wrong with the word tuple? It's not something invented by python, it's a mathematical thingy.

martin87 wrote:I'm really really trying to enjoy python but so far it seems to throw convention out the window

Conventions set by VBA? LOL
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Using elements from a list in a formula

Postby martin87 » Wed Nov 06, 2013 4:18 pm

Ahaaaaa,

Thank you stranac. Yes, yes i know, everyone that does programming seems to look upon VBA as inferior but it has its place :D. I'm a civil engineer and almost all the data manipulation is carried out in excel because its so widely used so creating macros is very easy to do. Plus I find excel reads like a flowing story, I can return to a code from last year and read it like a book.

I do agree with you though....I might have jumped the gun using the word convention haha.

Thanks again for your reply :) a simple solution to a simple problem.

Kind regards,

Martin
martin87
 
Posts: 2
Joined: Wed Nov 06, 2013 2:50 pm

Re: Using elements from a list in a formula

Postby micseydel » Wed Nov 06, 2013 5:32 pm

I think Python might seem strange in terms of OOP in a few ways (duck typing, no nice way to specify interface without implementation) especially if you're used to Java (which encourages a "Main" object; wtf is a "Main"?) but otherwise I'm very curious what conventions you're referring to.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1390
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests