PDA

View Full Version : Looping in Python



Mike
23-06-2006, 04:52 PM
I'm hoping there is someone here who knows a little more Python than I do :)

I have created a script that repeats itself, and I want to put a loop in so that I don't have the script repeated 13 times ;)

The only difference each time the script runs is a couple of variables (which could possibly be reduced to one)

I know you can use something like the "for" command, but I'm not sure quite how to use it (if that is the right command?)

Anyone able to help? I know I haven't explained myself very well, but perhaps you could ask the right questions so I know where I can explain it better :D

Cheers,
Mike.

Graham L
23-06-2006, 05:03 PM
I'm not much of a fan for snakes, or any other interpreters, but a for loop is generally quite simple to use.

you might have a flexible for like
for j = 1,4 ,6, 82, 87, 90
blah = j;
blah;
next j;which is an easy way to pass in one variable.

if you only have the conventional
for i=1 to 13
blah
blah=variables[i] + othervariables[i]
next i is one way to pass in variables. Just fill as many arrays as you need, and use the for loop control variable as the index.

Mike
23-06-2006, 05:34 PM
Let me see if I understand you :)

If my variables are Bob, Fred, Jack, Mary and I want the script to run based on each of those, I go something like:


for group = Bob, Fred, Jack, Mary
people = group

run.script(people)

next people

Would that be right?

And can I run a loop within a loop? :D Or is that getting a bit carried away?

Cheers,
Mike.

andrew93
23-06-2006, 10:58 PM
The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this :


MyArray = ['bird', 'cat', 'dog']
for MyVariable in MyArray:
print MyVariable

There is some good online help available for Python here :

http://www.faqs.org/docs/diveintopython/fileinfo_for.html

HTH, Andrew

P.S. Yes you can nest loops inside loops.

Mike
24-06-2006, 07:52 AM
The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this :


MyArray = ['bird', 'cat', 'dog']
for MyVariable in MyArray:
print MyVariable

There is some good online help available for Python here :

http://www.faqs.org/docs/diveintopython/fileinfo_for.html

HTH, Andrew

P.S. Yes you can nest loops inside loops.Fantastic, thanks Andrew. I'll see if I can get it to work when I'm at work on Monday (unless I log in from home before then ;)) and hopefully it'll all work out... it seems straightforward to me, just wasn't too sure on the syntax.

Cheers,
Mike.

Graham L
24-06-2006, 02:09 PM
I'd imagine that you can put a for loop inside the script if you want to.

It's the syntax which will always bite you. That's why I gave one for loop with ";" termninators and one without.

It seems that Python uses a ":" to open the body. Isn't a terminator required for the loop body? Or does it allow only one statement in the body?

I'm doing one project in C at the moment. Since I use Pascal mostly, I'm not happy with it. It requires terminators in places which look wrong , and (unlike Pascal) gets upset if you put them where they aren't needed.

andrew93
24-06-2006, 02:15 PM
Graham
Python doesn't use loop terminators, it uses indentation and the full colon to work out if something is inside or outside the loop.
Andrew

Graham L
24-06-2006, 02:29 PM
WOOHOO. ;) Lots of scope for typographical errors in that. I like begin ... end myself.

Mike
24-06-2006, 03:54 PM
I like the way Python uses indentation as you can just glance at a script and see which things go together.

Two weeks ago I knew nothing about Python - after a couple of weeks of teaching myself (mostly by initially disecting other scripts and then using the python.org documentation once I figured out mostly how things worked), I think I understand it fairly well, but I've still got a lot to learn.

Using 'None' instead of 'Null' threw me though - I think it'll take me a while to get used to that one :)

Mike.

andrew93
24-06-2006, 05:38 PM
Hey Mike

Python is ok. However, I have used quite a few languages (including Pascall Graham but that was eons ago!) and my preference is visual basic. Well it is at the moment. There is so much more online support / documentation for it given there a millions of users worldwide. Plus with the right version you can create stand-alone executable applications. In my opinion the Python GUI could hardly be described as feature rich and some of the syntax, as GL indicated, is counter-intuitive.

As you have probably found out support for Python is very limited. If you have Excel on your PC then you can dabble with the VBA version behind Excel (when in Excel press the Alt and F11 keys at the same time to open the vb editor). I used Python for some massive computational processes and it did the job ok. The claim to fame with Python is speed, but I can't envisage situations (for myself, other than that one project) where nanoseconds make a perceptible difference to the user. Have you considered trying visual basic Mike?

And a question for Graham : given you understand the importance of syntax, why oh why are you providing syntax examples (albeit incorrect) on a language that I don't think you have used??????? ;)

A

Mike
24-06-2006, 05:57 PM
Hi Andrew,

I would love to get into VBA, and am pushing my superiors to get me on courses etc. to get me started.

The reason why I'm using Python is because it has turned out easier to disect that VBA, but probably more so because the application I'm writing these python scripts for uses python internally, and has a set of commands that I can load that are easily called from Python. Apparently the same can be done using VBA within this application, but the majority of users in the world are using python for their scripting, and so finding help (or even examples to disect) for this program is a lot harder than finding help for the python version (if you follow what I mean).

The application (if you're wondering) is ESRI's ArcInfo - ArcGIS Desktop. Python is being used as a batch processor and a geoprocessor, to remove some of the very repetitive tasks involved in what I do.

Cheers,
Mike.

dolby digital
24-06-2006, 06:09 PM
VBA for Unix anyone :D

andrew93
24-06-2006, 06:44 PM
Fair call Mike. That makes sense. That link I gave earlier has plenty of information - not just on loops. These sites might also help :

http://docs.python.org/lib/genindex.html
http://www.microsoft.com/technet/scriptcenter/scripts/python/default.mspx
http://www.codesampler.com/python.htm

And the difference between the C and Python syntax can be seen here (for Grahams benefit) :

http://en.wikipedia.org/wiki/Python_syntax

A

Graham L
25-06-2006, 02:39 PM
Don't worry about it Andrew. I gave a couple of semantic examples of "generic" for loops: what they look like. I made no claim that they were syntactically correct for Python. (I think I have used it, but just to modify scripts.) Given the form of a construct I'm not familiar with, I can find out the syntactical details of a language; I assumed Mike is that smart.

I don't like interpreters: like spreadsheets, they make it far too easy to get wrong answers. They are just allowable for scripting.

I like strong block structure: I use indentation to show me the structure; I use begin/end (or even {/}) for the language processor.

Mike
27-06-2006, 08:55 AM
The for loop in Python doesn't follow the traditional for .... next syntax, it is more like this :


MyArray = ['bird', 'cat', 'dog']
for MyVariable in MyArray:
print MyVariableHi again Andrew,
Can Python handle a semicolon instead of a comma to identify the different variables in the array?
MyArray = ['bird'; 'cat'; 'dog'] - The reason being that my program can send variables in an array, but I'm pretty sure it splits them using semicolons... I haven't been able to check yet though.

Cheers,
Mike.

andrew93
27-06-2006, 09:03 PM
I've only ever used commas - there is more on the subject here :
http://www.astro.ufl.edu/~warner/prog/python.html

Can you replace the semi-colons with commas before the Python process?

There is a useful resource for ESRI users here:
http://www.esri.com/news/arcuser/0405/files/python.pdf

A