[Tutor] how do (or do I) do a list of variable names?

2014-02-02 Thread rick
Hello Everyone,

I think my approach is all wrong, but here goes.

var1 = []; var2 = []; var3 = [];  . . .  ~50 lists


each variable would be a list of two digit integers, or two digit
integers stored as strings (I don't need to do any math, I just need to
know which integers are in which variable)

container = [var1,var2,var3 . . . ]

I'd like to be able to iterate over container or pick out one or more of
my lists to write out an input (text) file for an external program.

The thing is, assigning all those lists to a list gives me a list of
lists, not a list of my variable names.  Worse, I may or may not be able
to have changes to a list show up in my list of lists, depending on how
& when I do it.

Really kludgy, but it is a one off that will likely never be used again.

TIA,
Rick



signature.asc
Description: OpenPGP digital signature
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how do (or do I) do a list of variable names?

2014-02-02 Thread Peter Otten
rick wrote:

> Hello Everyone,
> 
> I think my approach is all wrong, but here goes.
> 
> var1 = []; var2 = []; var3 = [];  . . .  ~50 lists
> 
> 
> each variable would be a list of two digit integers, or two digit
> integers stored as strings (I don't need to do any math, I just need to
> know which integers are in which variable)
> 
> container = [var1,var2,var3 . . . ]
> 
> I'd like to be able to iterate over container or pick out one or more of
> my lists to write out an input (text) file for an external program.
> 
> The thing is, assigning all those lists to a list gives me a list of
> lists, not a list of my variable names.  Worse, I may or may not be able
> to have changes to a list show up in my list of lists, depending on how
> & when I do it.
> 
> Really kludgy, but it is a one off that will likely never be used again.

You typically don't make 50 variables in Python, you use a dict:

>>> container = {
... "foo": [10, 20],
... "bar": [30, 40],
... "baz": [40, 50],
... #...
... }

you can then look up the "foo" list with

>>> print(container["foo"])
[10, 20]

If you want to know all the keys that 40 is associated with you can either 
iterate through the items every time

>>> print([k for k, v in container.items() if 40 in v])
['bar', 'baz']

or build the reverse dict once

>>> for k, values in container.items():
... for v in values:
... value_to_key.setdefault(v, set()).add(k)
... 

and then profit from fast lookup

>>> print(value_to_key[40])
{'bar', 'baz'}
>>> print(value_to_key[10])
{'foo'}

until you make changes to the original `container` dict.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] most useful ide

2014-02-02 Thread Ian D
Hi
 
Are there any recommendations for python ide's
 
currently I am using idle, which seems pretty decent but am open to any 
suggestions
 
 
cheers
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] sorting and writing to data file

2014-02-02 Thread adrian

Hello community,

Newbie here.  I have a data (.dat) file with integers (2,9,1,5,7,3,9) in 
it just as shown.
My instructions are to sort the numbers and rewrite them back to the 
data file.


*here is my code:**
*
lab3int=[2,9,1,5,7,3,9]
lab3int.sort()
print(lab3int)
lab3int=open('lab3int.dat','w')
lab3int.write()
lab3int.close()

*here is my error message:*

[1, 2, 3, 5, 7, 9, 9]
Traceback (most recent call last):
  File "lab3int.py", line 5, in 
lab3int.write()
TypeError: function takes exactly 1 argument (0 given)


I know that it is telling me that my error is in line #5.  If I put 
anything in the () for lab3int.write function, then that appears in my 
data file.  however, I am looking to just put the re-sorted integers 
back into the data file without having to manually type each integer 
manually.  Is there something that i can put into the lab3int.write() to 
make that happen?


Hope my problem is clear, Thanks people
ATS

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best version for novice

2014-02-02 Thread Leam Hall

On 02/01/2014 03:35 PM, Alan Gauld wrote:

On 01/02/14 18:41, Ian D wrote:


Is it better to use python 3 as a newcomer who isn't really going to be
writing any software as such just using it for learning?


The more important question is which version does your
preferred tutorial use?


And are you targeting a specific platform or just for yourself? Some 
platforms, like Red Hat/CentOS, are still 2.x. The most current RHEL is 
2.6 and the upcoming RHEL 7 is still Python 2.7.


If you're doing this for yourself, or for a start-up that can use newer 
versions, use 3.x if your tutorial supports it. You might note in Alan's 
signature is a like to his site, which happens to have a nice tutorial.   :)


http://www.alan-g.me.uk/

Leam

--
http://31challenge.net
http://31challenge.net/insight
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] strange errors

2014-02-02 Thread Gabriele Brambilla
Hi,

sometimes when I try to run a program in Python I obtain some errors.
The strange thing is that sometimes when I run it a second time or when I
turn off the pc and I restart later to try to make it works it gives
different errors.

How could I avoid this problem? I think that it is because it "remember"
some variables that I have used...

Thanks

Gabriele
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help me

2014-02-02 Thread hind fathallah
thank you so much because I got it :) 



On Saturday, February 1, 2014 1:28 PM, Danny Yoo  wrote:
 
On Fri, Jan 31, 2014 at 8:55 PM, hind fathallah

 wrote:
> hi can you answer this question for me plz

[question omitted]

Many of us probably could answer this.

But this is not a homework-answering mailing list.  The problem itself
is not interesting to us.  What is interesting is why the problem is
giving you trouble.  We'd rather focus on where you are having
difficulty: we'd rather help *you*.

Tell us what you've tried, where you're getting stuck, what other
kinds of problems you've done that are similar to this one.  That is,
show us what general problem solving strategies you're using now.
Maybe some of those strategies are not working for you.___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting and writing to data file

2014-02-02 Thread Alan Gauld

On 02/02/14 03:08, adrian wrote:


Newbie here.  I have a data (.dat) file with integers (2,9,1,5,7,3,9) in
it just as shown.


In your code below you use a hard coded list not a data file?


lab3int=[2,9,1,5,7,3,9]
lab3int.sort()
print(lab3int)


So far so good.


lab3int=open('lab3int.dat','w')


But now you've reassigned lab3int to the file object
so you threw away all your data. You need a separate
variable to store the file pointer.
Lets call it intFile...


lab3int.write()


Would now become

intfile.write()

But your data is a sorted list of integers so you need
to  convert that to a string before you can write it
to a text file.

You need a loop such as

for item in lab3int:
intFile.write( str(item) )

If you had a lot of data you might want to think
about adding some newlines when you write too...

hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strange errors

2014-02-02 Thread Alan Gauld

On 02/02/14 02:11, Gabriele Brambilla wrote:


sometimes when I try to run a program in Python I obtain some errors.


How are you running the program?

doubly clicking in a file manager/explorer?
Running from an OS command line?
Importing from the Python >>> prompt?
Or using some kind of IDE? (Which?)

The answer to your question depends a lot on the way you
run the code.

A sample error and your OS and Python version will help too.


How could I avoid this problem? I think that it is because it "remember"
some variables that I have used...


Could be. If you import a module then change that module then
reimporting alone is not enough. But if you run the code from the OS 
that shouldn't be an issue. So how do you run the code?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting and writing to data file

2014-02-02 Thread Peter Otten
Alan Gauld wrote:

> You need a loop such as
> 
> for item in lab3int:
>intFile.write( str(item) )

You also need to separate the values, with a space, a newline or whatever. 
So:

for item in lab3int:
intFile.write(str(item))
intFile.write("\n")

This can be simplified to

for item in lab3int:
print(item, file=intFile)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Alan Gauld

On 02/02/14 08:25, Ian D wrote:


Are there any recommendations for python ide's


Lots depending who you ask...


currently I am using idle, which seems pretty decent but am open to any
suggestions


If it works for you then use it.

The thing about IDEs is that they ae very personal.
Some people swear by emacs, others swear at it.
Some love Netbeans, or Eclipse, others hate them both.
If you are a Mac user you may prefer XCode...

There are lots of specialized Pyhon IDS too.

And many Linux users prefer to let Linux be their IDE
and just run multiple Windows to do the same job.
(I'm in this camp FWIW...)

A lot will also depend on whether you have used IDEs in the
past, since that might sway your preferences.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Asokan Pichai
On Sun, Feb 2, 2014 at 1:55 PM, Ian D  wrote:

> Hi
>
> Are there any recommendations for python ide's
>

> currently I am using idle, which seems pretty decent but am open to any
> suggestions
> cheers
>

While not an IDE in the usual sense I have found IPython a great addition
to my toolkit.
YMMV

  Asokan Pichai
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread scurvy scott
Hi

Are there any recommendations for python ide's

currently I am using idle, which seems pretty decent but am open to any
suggestions


cheers


I personally prefer the Linux interpreter.  Since you're asking.
Scott


On Sun, Feb 2, 2014 at 10:43 AM, Asokan Pichai wrote:

>
>
>
> On Sun, Feb 2, 2014 at 1:55 PM, Ian D  wrote:
>
>> Hi
>>
>> Are there any recommendations for python ide's
>>
>
>> currently I am using idle, which seems pretty decent but am open to any
>> suggestions
>> cheers
>>
>
> While not an IDE in the usual sense I have found IPython a great addition
> to my toolkit.
> YMMV
>
>   Asokan Pichai
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strange errors

2014-02-02 Thread ALAN GAULD
forwarding to list...
Please use reply-all when responding to the list.
 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos



>
> From: Gabriele Brambilla 
>To: Alan Gauld  
>Sent: Sunday, 2 February 2014, 17:31
>Subject: Re: [Tutor] strange errors
> 
>
>
>I use windows 7 command prompt. doing
>
>
>python .py
>
>
>the errors are various...when I will get them again I will write them!
>
>
>thanks
>
>
>Gabriele
>
>
>
>2014-02-02 Alan Gauld :
>
>On 02/02/14 02:11, Gabriele Brambilla wrote:
>>
>>
>>sometimes when I try to run a program in Python I obtain some errors.
>>>
>>
How are you running the program?
>>
>>doubly clicking in a file manager/explorer?
>>Running from an OS command line?
>>Importing from the Python >>> prompt?
>>Or using some kind of IDE? (Which?)
>>
>>The answer to your question depends a lot on the way you
>>run the code.
>>
>>A sample error and your OS and Python version will help too.
>>
>>
>>
>>How could I avoid this problem? I think that it is because it "remember"
>>>some variables that I have used...
>>>
>>
Could be. If you import a module then change that module then
>>reimporting alone is not enough. But if you run the code from the OS that 
>>shouldn't be an issue. So how do you run the code?
>>
>>-- 
>>Alan G
>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>http://www.flickr.com/photos/alangauldphotos
>>
>>___
>>Tutor maillist  -  Tutor@python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Albert-Jan Roskam


>Subject: Re: [Tutor] most useful ide
>
>On 02/02/14 08:25, Ian D wrote:
>
>> Are there any recommendations for python ide's
>
>Lots depending who you ask...

If you ask me: Spyder (free) or PyCharm (free for open source projects) ;-)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Kodiak Firesmith
Pycharm is nice for bigger projects (since tou can collapse any section);
but it's crazy resource intensive.  For Linux Gedit can be made very nice
for python, and of course vim in the shell is very nice with the right
~/.vimrc.
On Feb 2, 2014 11:20 AM, "scurvy scott"  wrote:

> Hi
>
> Are there any recommendations for python ide's
>
> currently I am using idle, which seems pretty decent but am open to any
> suggestions
>
>
> cheers
>
>
> I personally prefer the Linux interpreter.  Since you're asking.
> Scott
>
>
> On Sun, Feb 2, 2014 at 10:43 AM, Asokan Pichai 
> wrote:
>
>>
>>
>>
>> On Sun, Feb 2, 2014 at 1:55 PM, Ian D  wrote:
>>
>>> Hi
>>>
>>> Are there any recommendations for python ide's
>>>
>>
>>> currently I am using idle, which seems pretty decent but am open to any
>>> suggestions
>>> cheers
>>>
>>
>> While not an IDE in the usual sense I have found IPython a great addition
>> to my toolkit.
>> YMMV
>>
>>   Asokan Pichai
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Pierre Dagenais


On 14-02-02 01:16 PM, Kodiak Firesmith wrote:
> Pycharm is nice for bigger projects (since tou can collapse any section);
> but it's crazy resource intensive.  For Linux Gedit can be made very nice

I prefer Geany as it will run my code with a click of the mouse.

> for python, and of course vim in the shell is very nice with the right
> ~/.vimrc.
> On Feb 2, 2014 11:20 AM, "scurvy scott"  wrote:
> 
>> Hi
>>
>> Are there any recommendations for python ide's
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Oscar Benjamin
On Feb 2, 2014 8:17 PM, "Pierre Dagenais"  wrote:
>
>
>
> On 14-02-02 01:16 PM, Kodiak Firesmith wrote:
> > Pycharm is nice for bigger projects (since tou can collapse any
section);
> > but it's crazy resource intensive.  For Linux Gedit can be made very
nice
>
> I prefer Geany as it will run my code with a click of the mouse.

I prefer vim so that I never have to use the mouse. :)

Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Best version for novice

2014-02-02 Thread Pierre Dagenais


On 14-02-01 08:20 PM, Steven D'Aprano wrote:
> On Sat, Feb 01, 2014 at 06:41:10PM +, Ian D wrote:
>> Hi 
>>
>> Is it better to use python 3 as a newcomer who isn't really going to 
>> be writing any software as such just using it for learning?
> 
> Yes, you should use Python 3, with one proviso: many tutorials, 
> especially the older ones, are based on Python 2. That means that you 

Steven is not a newbie, but I am. I suppose that makes me the expert on
tutorials, LOL. I find that most useful tut are now for Python 3, when
you find a Python 2 tutorial you need to convert the print statement,
the tkinter import statement, and ignore any references to cPickle. In
my experience everything else works as posted.

PierreD.

> either need to find another tutorial, or mentally adjust from Python 2 
> to 3 when you read it. That's easy for an experienced user, but perhaps 
> not for a beginner.
> 
> The differences aren't really that great, no more different than between 
> (say) British English and American English, but it may be disconcerting 
> for somebody who isn't confident with the language.
> 
> Python 3 is the future of Python. All improvements are going into 3, 2 
> is only getting bug fixes. If you aren't *required* to stick with Python 
> 2 for some reason, you should use 3.
> 
> 
>> Also in 2.7 I use no subprocess by giving my python exe a -n argument, 
>> otherwise my canvas program's freeze.
> 
> I'm afraid that I have no idea what you are talking about here, Python 
> doesn't accept a -n argument:
> 
> [steve@ando ~]$ python2.7 -n
> Unknown option: -n
> usage: python2.7 [option] ... [-c cmd | -m mod | file | -] [arg] ...
> Try `python -h' for more information.
> 
> 
> 
> Regards,
> 
> 
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Ken G.


On 02/02/2014 03:10 PM, Pierre Dagenais wrote:


On 14-02-02 01:16 PM, Kodiak Firesmith wrote:

Pycharm is nice for bigger projects (since tou can collapse any section);
but it's crazy resource intensive.  For Linux Gedit can be made very nice

I prefer Geany as it will run my code with a click of the mouse.


for python, and of course vim in the shell is very nice with the right
~/.vimrc.
On Feb 2, 2014 11:20 AM, "scurvy scott"  wrote:


Hi

Are there any recommendations for python ide's


I, too, use Geany to run my Python programs.

Ken
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread Wiktor Matuszewski

W dniu 2014-02-02 18:56, Albert-Jan Roskam pisze:
>> On 02/02/14 08:25, Ian D wrote:
>>
>>> Are there any recommendations for python ide's
>>
>> Lots depending who you ask...
>
> If you ask me: Spyder (free) or PyCharm (free for open source 
projects) ;-)


There is also PyCharm Community Edition. Free for all projects.
And if you'd ask me, it would be my answer. :)

--
Best regards,
Wiktor Matuszewski
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2014-02-02 Thread Kipton Moravec
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))].


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


void minimize(int max_elements)

{
minsum = 9.0; // big big number

for (i=2; ihttps://mail.python.org/mailman/listinfo/tutor


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

2014-02-02 Thread David Hutto
On Sun, Feb 2, 2014 at 3:46 PM, Kipton Moravec  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 = 9.0; // big big number
>
>
 minsum = float(9.0) # used float just to show as double



> for (i=2; 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;
>
>

int m

lin_estimate =

errorsq = float(0) #just to show double = float type again

errorsq = 0



> for (m=istart; 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@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 *
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2014-02-02 Thread David Hutto
On Sun, Feb 2, 2014 at 6:44 PM, David Hutto  wrote:

>
>
>
> On Sun, Feb 2, 2014 at 3:46 PM, Kipton Moravec  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 = 9.0; // big big number
>>
>>
>  minsum = float(9.0) # used float just to show as double
>
>
>
>> for (i=2; 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> {
>> 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@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
> *
>
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com *

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

2014-02-02 Thread David Hutto
Kind of sure this is the translation line for line:

#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


def minimize(max_elements):
minsum = float(9.0) # used float just to show as double

for i in range(2,max_elements-6):
for j in range(i+2,max_elements-4):
for k in range(j+2,max_elements-2):

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

def error(istart,iend):
m = 0
lin_estimate = 0
errorsq = float(0) #just to show double = float type again
errorsq = 0
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])
#print errorsq # insert print for quick in line debugging by
uncommenting
return errorsq


#Call function minimize
minimize(max_elements)



Don't forget the tabs , or spaces, and the forced indentation.


On Sun, Feb 2, 2014 at 6:44 PM, David Hutto  wrote:

>
>
>
> On Sun, Feb 2, 2014 at 3:46 PM, Kipton Moravec  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 = 9.0; // big big number
>>
>>
>  minsum = float(9.0) # used float just to show as double
>
>
>
>> for (i=2; 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;
>>

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

2014-02-02 Thread Dave Angel
 Kipton Moravec  Wrote in message:
> 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.
> 

Sounds to me that you've just got a y list; your x list has the
 implicit assumption of being range (165,660)

> 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. 
> 

You're making the unspoken assumption that each line starts and
 ends exactly on a data point.  That won't give you the minimum
 error in general,  but perhaps it is an extra oddball restriction
 in the assignment. 


> 
> 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))].
> 
> 
> 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. 

No it appears to be the sum of the squares. 

> 
> 
> 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.
> 

That may seem to simplify the c but it's not pythonic.  If a
 function calculates 4 values, it should return 4 values, 
 presumably as a tuple.

> 
> 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
> 
> 
> void minimize(int max_elements)
> 
> {
> minsum = 9.0; // big big number
> 
> for (i=2; i for (j=i+2; j< max_elements -4; j++)
>  for (k=j+2; k< max_elements-2;k++)
>  {
> 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;
> } 
>  }
> }
> 
> 
> double 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;
> 
> for (m=istart; 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);
> 
> }
> 
> 
> 
> 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?
> 
> 
 for (i=2; i___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2014-02-02 Thread Steven D'Aprano
On Sun, Feb 02, 2014 at 02:46:38PM -0600, Kipton Moravec wrote:
> I am new to Python, and I do not know how to traverse lists like I
> traverse arrays in C.

You can do this, but normally shouldn't:

data = [2, 4, 8, 16, 32]
for i in range(0, len(data)):
x = data[i]
print(x)


That is a more-or-less exact translation of what you might do in a 
language like C or Pascal. But in Python, it's not considered good form. 
(Although, if you are doing a direct translation of some C code, it 
might be acceptable.) Instead, you should iterate directly over the 
elements of the list:

data = [2, 4, 8, 16, 32]
for x in data:
print(x)


(Python's for-loop is what some other languages call "for-each".) If you 
need the index as well, you can do this:

data = [2, 4, 8, 16, 32]
for i, x in enumerate(data):
print(i, x)


The rest of your question seems pretty interesting, unfortunately I 
don't have time to get into it now. Good luck!

-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sorting and writing to data file

2014-02-02 Thread spir

On 02/02/2014 04:08 AM, adrian wrote:

Hello community,

Newbie here.  I have a data (.dat) file with integers (2,9,1,5,7,3,9) in it just
as shown.
My instructions are to sort the numbers and rewrite them back to the data file.

*here is my code:**
*
lab3int=[2,9,1,5,7,3,9]
lab3int.sort()
print(lab3int)
lab3int=open('lab3int.dat','w')
lab3int.write()
lab3int.close()

*here is my error message:*

[1, 2, 3, 5, 7, 9, 9]
Traceback (most recent call last):
   File "lab3int.py", line 5, in 
 lab3int.write()
TypeError: function takes exactly 1 argument (0 given)


I know that it is telling me that my error is in line #5.  If I put anything in
the () for lab3int.write function, then that appears in my data file.  however,
I am looking to just put the re-sorted integers back into the data file without
having to manually type each integer manually.  Is there something that i can
put into the lab3int.write() to make that happen?

Hope my problem is clear, Thanks people


Do you realise you are calling two different very objects, a list of nums and a 
file, with the same name 'lab3int'?


d

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] most useful ide

2014-02-02 Thread spir

On 02/02/2014 09:10 PM, Pierre Dagenais wrote:



On 14-02-02 01:16 PM, Kodiak Firesmith wrote:

Pycharm is nice for bigger projects (since tou can collapse any section);
but it's crazy resource intensive.  For Linux Gedit can be made very nice


I prefer Geany as it will run my code with a click of the mouse.


geany is my favorite editor as well

I'd use gedit for its simplicity, but cannot survive an editor without the 
'duplicate' command (ctrl-d).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor