[Tutor] Traversing lists or getting the element you want.

David Hutto dwightdhutto at gmail.com
Mon Feb 3 00:50:27 CET 2014


On Sun, Feb 2, 2014 at 6:44 PM, David Hutto <dwightdhutto at gmail.com> wrote:

>
>
>
> On Sun, Feb 2, 2014 at 3:46 PM, Kipton Moravec <kip at kdream.com> wrote:
>
>> I am new to Python, and I do not know how to traverse lists like I
>> traverse arrays in C. This is my first program other than "Hello World".
>> I have a Raspberry Pi and they say Python is the language of choice for
>> that little machine. So I am going to try to learn it.
>>
>>
>> I have data in the form of x, y pairs where y = f(x) and is non linear.
>> It comes from a .csv file.
>>
>> In this case x is an integer from 165 to 660 so I have 495 data sets.
>>
>> I need to find the optimal locations of three values of x to piecewise
>> linear estimate the function.
>>
>>
>> So I need to find i, j, k so 165 < i < j < k < 660 and the 4 line
>> segments [(165, f(165)), (i, f(i))], [(i, f(i)), (j, f(j))], [(j, f(j),
>> (k, f(k))], [(k, f(k)), (660, f(660))].
>>
>> So as you iterate through the minimize function below, you want something
> like this, I believe:
>
>     if (165 < i) and (i < j) and (j < k) and (k < 660):
>         print 'true'
>     else:
>         print 'false'
>
>
>> The value I need to minimize is the square of the difference between the
>> line estimate and the real value at each of the 495 points.
>>
>>
>> I can do this in C. To keep it simple to understand I will assume the
>> arrays x[] and y[] and minsum, mini, minj, and mink are global.
>>
>>
>> I have not tested this but this is what I came up with in C. Assuming
>> x[] and y[] are already filled with the right values.
>>
>>
>> int x[495];
>> double y[495];
>> max_elements = 495;
>> double minsum;
>> int mini, minj, mink
>>
>> #We define some vars before utilizing them x,and y are list comprehension
> # You said int for x, so it's created in ints, and y is in floats
> x = [num for num in range(0,496)]
> # You said double for y, so the it's created in floats, and x is an int
> list
> y = [float(num) for num in range(0,496)]
> i = 0
> j = 0
> k = 0
> max_elements = 495
> mini = 0
> minj = 0
> mink = 0
>
>
>> void minimize(int max_elements)
>>
>>
>  def minimize(max_elements):
>
> {
>>     minsum = 9999999999999.0; // big big number
>>
>>
>  minsum = float(9999999999999.0) # used float just to show as double
>
>
>
>>     for (i=2; i<max_elements-6;i++)
>>         for (j=i+2; j< max_elements -4; j++)
>>              for (k=j+2; k< max_elements-2;k++)
>>
>
> for i in range(2,max_elements-6):
>     print i
>     for j in range(i+2,max_elements-4):
>         print j
>         for k in range(j+2,max_elements-2):
>             print k
>
>
>>              {
>>                 sum = error(0,i);
>>                 sum += error(i,j);
>>                 sum += error(j,k);
>>                 sum += error(k,495)
>>
>>
>                 sum = error(0,i)
>                 sum += error(i,j)
>                 sum += error(j,k)
>                 sum += error(k,495)
>
>
>>                 if (sum < minsum)
>>                 {
>>                     minsum = sum;
>>                     mini = i;
>>                     minj = j;
>>                     mink = k;
>>                 }
>>              }
>> }
>>
>>
>
>                 if sum < minsum:
>
>                     minsum = sum
>                     mini = i
>                     minj = j
>                     mink = k
>
>
>>
>> double error(int istart, int iend)
>> {
>>
>>
> error(int istart, int iend):
>
>
>
>> // linear interpolation I can optimize but left
>> // it not optimized for clarity of the function
>>
>>     int m;
>>
>>     double lin_estimate;
>>
>>     double errorsq;
>>
>>     errorsq = 0;
>>
>>
> Forgot this part:
>     m = 0
>
>     lin_estimate = 0
>
>     errorsq = float(0) #just to show double = float type again
>
>     errorsq = 0
>
>
>
>>     for (m=istart; m<iend; m++)
>>     {
>>         lin_estimate = y[istart] + ((y[iend] - y[istart]) *
>>                        ((x[m] - x[istart]) / (x[iend] - x[istart])));
>>
>>         errorsq += (lin_estimate - y[m]) * (lin_estimate - y[m]);
>>     }
>>
>>     return (errorsq);
>>
>> }
>>
>>
>>
>>
>
> for iterator in range(istart; iend):
>
>         lin_estimate = y[istart] + ((y[iend] - y[istart]) * ((x[m] -
> x[istart]) / (x[iend] - x[istart])))
>
>         errorsq += (lin_estimate - y[m]) * (lin_estimate - y[m])
>
>     return errorsq
>
>
>
>
>> At the end the three values I want are mini, minj, mink;
>> or x[mini], x[minj], x[mink]
>>
>>
>> So how do I do this (or approach this) in Python?
>>
>> Loose Translation
>
>>
>> Kip
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
> I'll look at it a little more later on, unless someone' is less rusty in C
> and Python than I am.
>
> --
> Best Regards,
> David Hutto
> *CEO:* *http://www.hitwebdevelopment.com
> <http://www.hitwebdevelopment.com>*
>
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com <http://www.hitwebdevelopment.com>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140202/0c1b9b27/attachment.html>


More information about the Tutor mailing list