Re: [Tutor] iterating through a directory

2015-09-09 Thread Mark Lawrence
On 09/09/2015 14:32, richard kappler wrote: Yes, many questions today. I'm working on a data feed script that feeds 'events' into our test environment. In production, we monitor a camera that captures an image as product passes by, gathers information such as barcodes and package ID from the imag

Re: [Tutor] iterating through a directory

2015-09-09 Thread Alan Gauld
On 09/09/15 15:29, richard kappler wrote: Still not sure how to efficiently get the script to keep moving to the next file in the directory though, in other words, for each iteration in the loop, I want it to fetch, rename and send/save the next image in line. Hope that brings better understandi

Re: [Tutor] iterating through a directory

2015-09-09 Thread richard kappler
-0400 > > From: richkapp...@gmail.com > > To: tutor@python.org > > Subject: [Tutor] iterating through a directory > > > > > Yes, many questions today. I'm working on a data feed script that feeds > > 'events' into our test environment. In production,

Re: [Tutor] iterating through a directory

2015-09-09 Thread Albert-Jan Roskam
> Date: Wed, 9 Sep 2015 09:32:34 -0400 > From: richkapp...@gmail.com > To: tutor@python.org > Subject: [Tutor] iterating through a directory > > Yes, many questions today. I'm working on a data feed script that feeds > 'events' into our test environment. In

[Tutor] iterating through a directory

2015-09-09 Thread richard kappler
Yes, many questions today. I'm working on a data feed script that feeds 'events' into our test environment. In production, we monitor a camera that captures an image as product passes by, gathers information such as barcodes and package ID from the image, and then sends out the data as a line of xm

Re: [Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread John Doe
Alan, Peter, et al: Thank you all very much! Staring at this problem for hours was driving me crazy and I am very appreciative for your guys' time in looking into my silly error -- I have thoroughly reviewed both the responses and it makes perfect sense (*sigh of relief*). On Thu, Oct 2, 2014 a

Re: [Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread Peter Otten
John Doe wrote: > Hello List, > I am in need of your assistance. I have a text file with random words > in it. I want to write all the lines to a new file. Additionally, I am > using Python 2.7 on Ubuntu 12.04: > > Here is my code: > > def loop_extract(): > with open('words.txt', 'r') as f:

Re: [Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread Alan Gauld
On 02/10/14 16:47, John Doe wrote: def loop_extract(): with open('words.txt', 'r') as f: for lines in f: #print lines (I confirmed that each line is successfully printed) with open('export.txt', 'w') as outf: This opens and closes the file for each itera

[Tutor] Iterating Lines in File and Export Results

2014-10-02 Thread John Doe
Hello List, I am in need of your assistance. I have a text file with random words in it. I want to write all the lines to a new file. Additionally, I am using Python 2.7 on Ubuntu 12.04: Here is my code: def loop_extract(): with open('words.txt', 'r') as f: for lines in f:

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread Steven D'Aprano
On 05/02/13 22:27, Oscar Benjamin wrote: On 5 February 2013 03:56, eryksun wrote: On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: Nope, in both Python 2 and 3 iterating over a dict directly just provides the key. That's also how "if key in dict" works. A dict implements __contains__ for a

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread eryksun
On Tue, Feb 5, 2013 at 6:27 AM, Oscar Benjamin wrote: > > I almost wrote this response but then I realised that Dave probably > meant that "obj in dict" returns True if the dict has a key equal to > obj rather than if the dict has a (key, value) pair equal to obj. Thanks, that's probably what Ste

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-05 Thread Oscar Benjamin
On 5 February 2013 03:56, eryksun wrote: > On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: >>> Nope, in both Python 2 and 3 iterating over a dict directly just >>> provides the key. That's also how "if key in dict" works. > > A dict implements __contains__ for an efficient "in" test. In general

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread eryksun
On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel wrote: >> Nope, in both Python 2 and 3 iterating over a dict directly just >> provides the key. That's also how "if key in dict" works. A dict implements __contains__ for an efficient "in" test. In general, the interpreter falls back to using iteration i

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 06:18 PM, Steven D'Aprano wrote: On 05/02/13 09:26, Dave Angel wrote: Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator providing both key and value. If I'm right, then it could be simplif

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Steven D'Aprano
On 05/02/13 09:26, Dave Angel wrote: Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator providing both key and value. If I'm right, then it could be simplified further to: for i, (k, v) in enumerate(dat

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 12:58 PM, Modulok wrote: Hmm.. no kidding. Well, at least I knew I was over-complicating it. Cheers! -Modulok- Please don't top-post. Another point. I don't currently have Python 3.x installed, but I seem to remember that in Python 3 you can use the dict itself as an iterator

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Modulok
Hmm.. no kidding. Well, at least I knew I was over-complicating it. Cheers! -Modulok- On 2/4/13, Dave Angel wrote: > On 02/04/2013 12:13 PM, Modulok wrote: >> List, >> >> Simple question: Is there a common pattern for iterating a dict, but also >> providing access to an iteration counter? Here'

Re: [Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Dave Angel
On 02/04/2013 12:13 PM, Modulok wrote: List, Simple question: Is there a common pattern for iterating a dict, but also providing access to an iteration counter? Here's what I usually do (below). I'm just wondering if there are other, more clever ways:: data = {'a': "apple", 'b': "banana",

[Tutor] Iterating a dict with an iteration counter? How would *you* do it?

2013-02-04 Thread Modulok
List, Simple question: Is there a common pattern for iterating a dict, but also providing access to an iteration counter? Here's what I usually do (below). I'm just wondering if there are other, more clever ways:: data = {'a': "apple", 'b': "banana", 'c': "cherry"} i = 0 for k,v in da

Re: [Tutor] iterating over a changing list

2012-10-10 Thread Steven D'Aprano
On 11/10/12 08:49, eryksun wrote: Also, generally avoid mutating a list while iterating over it. listiterator is just incrementing an index, so modifying the size of the list can produce nonsense (e.g. if you remove the current item, the next item will be skipped). Instead, create an empty list

Re: [Tutor] iterating over a changing list

2012-10-10 Thread eryksun
On Wed, Oct 10, 2012 at 3:52 PM, Ed Owens wrote: > > import string Why are you importing "string"? Most string functions one would need are methods of str/unicode. Sometimes "string" is still required, however. > def add_element(items, point): > items = items[:point+1][:] + [['new']] + items

Re: [Tutor] iterating over a changing list

2012-10-10 Thread Dave Angel
On 10/10/2012 03:52 PM, Ed Owens wrote: > I'm trying to iterate over a list of elements, and make changes to the list > in front of the element I'm currently working with. I can update the list, > but the 'for' doesn't see the new element. Here's the code: > > import string > > def add_element(

Re: [Tutor] iterating over a changing list

2012-10-10 Thread Mark Lawrence
On 10/10/2012 20:52, Ed Owens wrote: I'm trying to iterate over a list of elements, and make changes to the list in front of the element I'm currently working with. I can update the list, but the 'for' doesn't see the new element. Here's the code: import string def add_element(items, point)

[Tutor] iterating over a changing list

2012-10-10 Thread Ed Owens
I'm trying to iterate over a list of elements, and make changes to the list in front of the element I'm currently working with. I can update the list, but the 'for' doesn't see the new element. Here's the code: import string def add_element(items, point): items = items[:point+1][:] + [['n

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-05 Thread David Hutto
On Sat, Sep 4, 2010 at 6:16 AM, Steven D'Aprano wrote: > On Sat, 4 Sep 2010 11:57:00 am David Hutto wrote: >> First of all, I'll respond more thoroughly tomorrow, when I can >> review what you said more clearly, but for now I'll clarify. >> >> Here is the whole code that I'm using: >> >> http://pa

Re: [Tutor] iterating over less than a full list

2010-09-05 Thread Dave Angel
Bill Allen wrote: Thanks to everyone who replied. Some of the methods presented where some I had thought of, others were new to me. Particularly, I did not realize I could apply a slice to a list. The for x in some_stuff[:value] form worked very well for my purposes. I can also see I need

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Bill Allen
Thanks to everyone who replied. Some of the methods presented where some I had thought of, others were new to me. Particularly, I did not realize I could apply a slice to a list. The for x in some_stuff[:value] form worked very well for my purposes. I can also see I need to look into the ite

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Dave Angel
Bill Allen wrote: Say I have and iterable called some_stuff which is thousands of items in length and I am looping thru it as such: for x in some_stuff etc... However, what if I want only to iterate through only the first ten items of some_stuff, for testing purposes. Is there a concise

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Steven D'Aprano
On Sun, 5 Sep 2010 03:14:24 am Bill Allen wrote: > Say I have and iterable called some_stuff which is thousands of items > in length and I am looping thru it as such: > > for x in some_stuff > etc... > > However, what if I want only to iterate through only the first ten > items of some_stuff,

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Mark Lawrence
On 04/09/2010 18:29, Sander Sweers wrote: On 4 September 2010 19:25, Sander Sweers wrote: for x in some_stuff: if x<= 10: print x else: break Oops, corrected version... count = 0 for x in some_stuff: if count< 10: print x count +=1 else:

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Sander Sweers
On 4 September 2010 19:25, Sander Sweers wrote: > for x in some_stuff: >    if x <= 10: >        print x >    else: >        break Oops, corrected version... count = 0 for x in some_stuff: if count < 10: print x count +=1 else: break Greets Sander ___

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Sander Sweers
On 4 September 2010 19:14, Bill Allen wrote: > Say I have and iterable called some_stuff which is thousands of items in > length and I am looping thru it as such: > > for x in some_stuff > etc... > > However, what if I want only to iterate through only the first ten items of > some_stuff, for

Re: [Tutor] iterating over less than a full list

2010-09-04 Thread Nitin Pawar
if its a dictionary, then I think you will need to use limit if its normal array you can use range(0,10) and access some_stuff[i] On Sat, Sep 4, 2010 at 10:44 PM, Bill Allen wrote: > Say I have and iterable called some_stuff which is thousands of items in > length and I am looping thru it as su

[Tutor] iterating over less than a full list

2010-09-04 Thread Bill Allen
Say I have and iterable called some_stuff which is thousands of items in length and I am looping thru it as such: for x in some_stuff etc... However, what if I want only to iterate through only the first ten items of some_stuff, for testing purposes. Is there a concise way of specifying tha

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-04 Thread Steven D'Aprano
On Sat, 4 Sep 2010 11:57:00 am David Hutto wrote: > First of all, I'll respond more thoroughly tomorrow, when I can > review what you said more clearly, but for now I'll clarify. > > Here is the whole code that I'm using: > > http://pastebin.com/Ak8DFjrb David, in genrandfiles() you say this:

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread David Hutto
snip I'll go further with this though, just to get the response. Hypothetically, if I wanted AI(artificial imagination), then I would want random thoughts that made sense, every once in a while. So, I hypothesize that the first step in Artificial Imagination, is random thoughts, and then they have

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread David Hutto
On Fri, Sep 3, 2010 at 9:57 PM, David Hutto wrote: > First of all, I'll respond more thoroughly tomorrow, when I can review > what you said more clearly, but for now I'll clarify. > > Here is the whole code that I'm using: > > http://pastebin.com/Ak8DFjrb > > On Fri, Sep 3, 2010 at 9:12 PM, Steven

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread David Hutto
First of all, I'll respond more thoroughly tomorrow, when I can review what you said more clearly, but for now I'll clarify. Here is the whole code that I'm using: http://pastebin.com/Ak8DFjrb On Fri, Sep 3, 2010 at 9:12 PM, Steven D'Aprano wrote: > On Fri, 3 Sep 2010 12:24:00 pm David Hutto wr

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread Steven D'Aprano
On Fri, 3 Sep 2010 12:24:00 pm David Hutto wrote: > In the below function I'm trying to iterate over the lines in a > textfile, and try to match with a regex pattern that iterates over > the lines in a dictionary(not {}, but turns a text list of > alphabetical words into a list using readlines()).

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread David Hutto
On Fri, Sep 3, 2010 at 2:51 PM, David Hutto wrote: > On Thu, Sep 2, 2010 at 11:05 PM, David Hutto wrote: >> I just added +'*' to select in re.search(select+'*', str(readfile[:])), >> and it now shows the same in both. >> >> But if you have any input on modifications let me know. >> >> Thanks, >>

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-03 Thread David Hutto
On Thu, Sep 2, 2010 at 11:05 PM, David Hutto wrote: > I just added +'*' to select in re.search(select+'*', str(readfile[:])), > and it now shows the same in both. > > But if you have any input on modifications let me know. > > Thanks, > David > Still a problem. When I use the re.search(select+'*'

Re: [Tutor] Iterating through a list of replacement regex patterns

2010-09-02 Thread David Hutto
I just added +'*' to select in re.search(select+'*', str(readfile[:])), and it now shows the same in both. But if you have any input on modifications let me know. Thanks, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscriptio

[Tutor] Iterating through a list of replacement regex patterns

2010-09-02 Thread David Hutto
In the below function I'm trying to iterate over the lines in a textfile, and try to match with a regex pattern that iterates over the lines in a dictionary(not {}, but turns a text list of alphabetical words into a list using readlines()). def regexfiles(filename): textfile = file(filenam

Re: [Tutor] Iterating through a list of strings

2010-05-05 Thread Walter Prins
On 03/05/10 06:16, Thomas C. Hicks wrote: I am using Python 2.6.4 in Ubuntu. Since I use Ubuntu (with its every 6 months updates) and want to learn Python I have been working on a post-install script that would get my Ubuntu system up and running with my favorite packages quickly. As an aside,

Re: [Tutor] Iterating through a list of strings

2010-05-04 Thread Thomas C. Hicks
Wow, this is great! I appreciate all the pointers, lots to keep learning here. thomas ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread python
> Agreed that > > line = line[:line.index('%')] > > is slightly more readable than > >line = line.split('%', 1)[0] How about: line = line.partition('%')[0] partition() works even if '%' isn't present. The index() and split() techniques raise exceptions if '%' isn't present. Malcolm __

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread spir ☣
On Mon, 03 May 2010 13:08:18 +0200 Stefan Behnel wrote: > > Why aren't strings mutable, or lists immutable? > > What would be the use case of an immutable list, as opposed to a tuple? How > would you use mutable strings in a dictionary? [I'm not totally sure of the following, take it with so

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Steven D'Aprano
On Mon, 3 May 2010 08:18:41 pm Luke Paireepinart wrote: > On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: > > Luke Paireepinart, 03.05.2010 10:27: > >> What's this bizarre syntax? > > > > Look it up in the docs, it's called "with statement". Its purpose > > here is to make sure the file is c

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
Luke Paireepinart, 03.05.2010 12:18: On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: Luke Paireepinart, 03.05.2010 10:27: I thought they changed for loop interations so that if you did for line in open('packages.txt'): etc... it would automatically close the file handle after th

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Luke Paireepinart
2010/5/3 spir ☣ : > On Mon, 03 May 2010 10:55:11 +0200 > Stefan Behnel wrote: > >> Luke Paireepinart, 03.05.2010 10:27: >> > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: >> >>                 line = line.split('%', 1)[0] >> > >> > lines = [line[:line.index('%')] for line in ... >> >> Agree

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Steven D'Aprano
On Mon, 3 May 2010 05:35:52 pm Andre Engels wrote: > Don't change the list that you are iterating over. As you have found, > it leads to (to most) unexpected results. Or if you absolutely have to change the list in place, iterate over it *backwards* (starting at the end, and moving towards the

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread spir ☣
On Mon, 03 May 2010 10:55:11 +0200 Stefan Behnel wrote: > Luke Paireepinart, 03.05.2010 10:27: > > On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: > >> line = line.split('%', 1)[0] > > > > lines = [line[:line.index('%')] for line in ... > > Agreed that > > line = line[

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Luke Paireepinart
On Mon, May 3, 2010 at 3:50 AM, Stefan Behnel wrote: > Luke Paireepinart, 03.05.2010 10:27: >> What's this bizarre syntax? > > Look it up in the docs, it's called "with statement". Its purpose here is to > make sure the file is closed after the execution of the statement's body, > regardless of an

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
Luke Paireepinart, 03.05.2010 10:27: On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: line = line.split('%', 1)[0] lines = [line[:line.index('%')] for line in ... Agreed that line = line[:line.index('%')] is slightly more readable than line = line.split('%', 1)

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Stefan Behnel
Luke Paireepinart, 03.05.2010 10:27: On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: You are modifying the list during iteration, so the size changes and the iterator gets diverted. Don't remove the line, just skip over it, e.g. def read_package_names(open_text_file): """Read

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Andre Engels
On Mon, May 3, 2010 at 10:19 AM, Luke Paireepinart wrote: > Hmm, why not > lines = [line for line in lines if not line.strip().startswith('%')] I knew I missed something -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Luke Paireepinart
On Mon, May 3, 2010 at 1:49 AM, Stefan Behnel wrote: > > You are modifying the list during iteration, so the size changes and the > iterator gets diverted. Don't remove the line, just skip over it, e.g. > >    def read_package_names(open_text_file): >        """Read lines, strip any comments and r

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Luke Paireepinart
> If that looks a bit clumsy, use a generator expression: > > linesToDelete = [x for x in lines if x.startswith('%')] > for x in linesToDelete: >   lines.remove(x) > > which idiomatically should probably become: > > for x in [y for y in lines if y.startswith('%')]: >   lines.remove(x) > > Hmm, why

Re: [Tutor] Iterating through a list of strings

2010-05-03 Thread Andre Engels
On Mon, May 3, 2010 at 7:16 AM, Thomas C. Hicks wrote: > I am using Python 2.6.4 in Ubuntu.  Since I use Ubuntu (with its every > 6 months updates) and want to learn Python I have been working on a > post-install script that would get my Ubuntu system up and running with > my favorite packages qui

Re: [Tutor] Iterating through a list of strings

2010-05-02 Thread Stefan Behnel
Thomas C. Hicks, 03.05.2010 07:16: %Comment introducing the next block of packages %Below are the packages for using Chinese on the system %Third line of comment because I am a verbose guy! ibus-pinyin ibus-table-wubi language-pack-zh-hans etc. I read the lines of the file into a list for proce

[Tutor] Iterating through a list of strings

2010-05-02 Thread Thomas C. Hicks
I am using Python 2.6.4 in Ubuntu. Since I use Ubuntu (with its every 6 months updates) and want to learn Python I have been working on a post-install script that would get my Ubuntu system up and running with my favorite packages quickly. Basically the script reads a text file, processes the lin

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Rich Lovely
2009/11/28 Wayne Werner : > > > On Sat, Nov 28, 2009 at 6:59 AM, Dave Angel wrote: >> >> And if the lists are large, use  itertools.izip() which works the same, >> but produces an iterator. >> >> Note that if the lists are not the same length, I think it stops when the >> shorter one ends. > > But

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Wayne Werner
On Sat, Nov 28, 2009 at 6:59 AM, Dave Angel wrote: > > And if the lists are large, use itertools.izip() which works the same, but > produces an iterator. > > Note that if the lists are not the same length, I think it stops when the > shorter one ends. > But you can use izip_longest: import ite

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Dave Angel
Jose Amoreira wrote: Hi! I want to process corresponding elements of two lists, sequentially. Call the lists list1 and list2, and assume they have equal lengths. I can do something like for index in range(len(list1)): process(list1[index], list2[index]) But I find it somehow rather ugly,

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Lie Ryan
On 11/28/2009 10:03 PM, Jose Amoreira wrote: Yes, Robert, that does it! Thanks a lot! Have a nice weekend! Jose don't forget zip() built-in function: for x, y in zip(list1, list2): print x, y ___ Tutor maillist - Tutor@python.org To unsubscrib

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Robert Johansson
Hi! I want to process corresponding elements of two lists, sequentially. Call the lists list1 and list2, and assume they have equal lengths. I can do something like for index in range(len(list1)): process(list1[index], list2[index]) But I find it somehow rather ugly, because we generate yet

Re: [Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Jose Amoreira
Yes, Robert, that does it! Thanks a lot! Have a nice weekend! Jose On Saturday 28 November 2009 10:54:56 am Robert Johansson wrote: > Hi! > I want to process corresponding elements of two lists, sequentially. Call > the > lists list1 and list2, and assume they have equal lengths. I can do > someth

[Tutor] Iterating over two sequences in "parallel"

2009-11-28 Thread Jose Amoreira
Hi! I want to process corresponding elements of two lists, sequentially. Call the lists list1 and list2, and assume they have equal lengths. I can do something like for index in range(len(list1)): process(list1[index], list2[index]) But I find it somehow rather ugly, because we generate yet

Re: [Tutor] Iterating through objects

2009-08-16 Thread Alan Gauld
"GoodPotatoes" wrote print page1.readlines() From this excercise, the first time I read page1 I get all of the lines. The best thing to do is store them in a variable then print them. That way you can access them at will: lines = page1.readlines() print lines # all of them as a list pr

Re: [Tutor] Iterating through objects

2009-08-16 Thread bob gailer
GoodPotatoes wrote: I'm not sure if I've been searching using the correct terms. After I've iterated through an object, such as a cursor or webpage, how do I get back to the "top"? e.g. tutorial from http://www.daniweb.com/code/snippet563.html# print page1.readlines() Capture the lines in

[Tutor] Iterating through objects

2009-08-16 Thread GoodPotatoes
I'm not sure if I've been searching using the correct terms. After I've iterated through an object, such as a cursor or webpage, how do I get back to the "top"? e.g. tutorial from http://www.daniweb.com/code/snippet563.html# print page1.readlines() >From this excercise, the first time I read

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Dave Angel
Robert Berman wrote: Thank you, Christian. This solution was one I was not expecting and am glad to receive it. It is one I will explore in greater detail later. Robert On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote: Robert Berman wrote: Hi, Given a list of options: optio

Re: [Tutor] Iterating through a function list

2009-05-20 Thread Dave Kuhlman
On Wed, May 20, 2009 at 10:02:22AM -0400, Robert Berman wrote: > >Hi, >Given a list of options: option_1...option_n. For each option I >have a corresponding function: func_1. func_n. I have all function >names defined in a list similar to flist = [func_1, >func_2,..

[Tutor] Iterating through a function list

2009-05-20 Thread Robert Berman
Hi, Given a list of options: option_1...option_n. For each option I have a corresponding function: func_1. func_n. I have all function names defined in a list similar to flist = [func_1, func_2,...func_n] which I know is a legitimate construct having found a similar construct discusse

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread ALAN GAULD
> understand exactly what you are saying and what you are advocating. > The 'dictionary of functions' is the 'best' approach because of simplicity > and because it minimizes chances or errors. Correct. Maintaining synch of indexes between two arrays of data items is always going to be a risky b

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread spir
Le Wed, 20 May 2009 10:25:21 -0400, Robert Berman s'exprima ainsi: > What I do not know how to do is to call the selected function. If you have options and functions "hard-coded" in lists (or if you get them from outside), you can still let python build a dict for you, using "zip": l1 = [1,2,3

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Alan, The emphasis of your reply certainly makes me look at the dictionary solution as the most 'correct' solution to utilize. Before I change the code I just implemented, let me make sure I understand exactly what you are saying and what you are advocating. The 'dictionary of functions' is the 'b

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Alan Gauld
"Robert Berman" wrote Thank you, Christian. This solution was one I was not expecting and am glad to receive it. It is one I will explore in greater detail later. A dictionary of functions is the most common way to tackle this fairly common requirement. It combines readability with ease of

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Thank you, Christian. This solution was one I was not expecting and am glad to receive it. It is one I will explore in greater detail later. Robert On Wed, 2009-05-20 at 16:44 +0200, Christian Witts wrote: > Robert Berman wrote: > > Hi, > > > > Given a list of options: option_1...option_n. F

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Thank you, Emile. That is the exact answer I needed. Robert On Wed, 2009-05-20 at 07:48 -0700, Emile van Sebille wrote: > On 5/20/2009 7:25 AM Robert Berman said... > > Hi, > > > > Given a list of options: option_1...option_n. For each option I have > > a corresponding function: func_1...

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Emile van Sebille
On 5/20/2009 7:25 AM Robert Berman said... Hi, Given a list of options: option_1...option_n. For each option I have a corresponding function: func_1. func_n. I have all function names defined in a list similar to flist = [func_1, func_2,...func_n] which I know is a legitimate constru

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Christian Witts
Robert Berman wrote: Hi, Given a list of options: option_1...option_n. For each option I have a corresponding function: func_1. func_n. I have all function names defined in a list similar to flist = [func_1, func_2,...func_n] which I know is a legitimate construct having found a simi

Re: [Tutor] Iterating over list of functions

2009-05-20 Thread Lie Ryan
Robert Berman wrote: Hi, Given a list of options: option_1...option_n. For each option I have a corresponding function: func_1. func_n. I have all function names defined in a list similar to flist = [func_1, func_2,...func_n] which I know is a legitimate construct having found a simi

[Tutor] Iterating over list of functions

2009-05-20 Thread Robert Berman
Hi, Given a list of options: option_1...option_n. For each option I have a corresponding function: func_1. func_n. I have all function names defined in a list similar to flist = [func_1, func_2,...func_n] which I know is a legitimate construct having found a similar construct discusse

Re: [Tutor] Iterating over a long list with regular expressions andchanging each item?

2009-05-04 Thread Alan Gauld
"Paul McGuire" wrote For much of my own code, I find lists of string literals to be tedious to enter, and easy to drop a ' character. This style is a little easier on the eyes, and harder to screw up. 'case_def_gen':['case_def gen null'.split()], 'nsuff_fem_pl':['nsuff null null'.split()],

Re: [Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-04 Thread Paul McGuire
Original: 'case_def_gen':['case_def','gen','null'], 'nsuff_fem_pl':['nsuff','null', 'null'], 'abbrev': ['abbrev, null, null'], 'adj': ['adj, null, null'], 'adv': ['adv, null, null'],} Note the values for 'abbrev', 'adj' and 'adv' are not lists, but strings containing comma-separated lists. S

Re: [Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-04 Thread spir
Le Mon, 4 May 2009 10:15:35 -0400, Dan Liang s'exprima ainsi: > Hi Spir and tutors, > > Thank you Spir for your response. I went ahead and tried your code after > adding a couple of dictionary entries, as below: > ---Code Begins--- > #!usr/bin/python > > tags = { > > > 'c

Re: [Tutor] Iterating over a long list with regular expressions andchanging each item?

2009-05-04 Thread Alan Gauld
"Dan Liang" wrote def replaceTagging(source_name, target_name): source_file = file(source_name, 'r') source = source_file.read() # not really necessary this reads the entire file as a string target_file = open(target_name, "w") # replacement loop for li

Re: [Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-04 Thread Dan Liang
ine 16, in newlyTaggedWord (word,tag) = line.split(TAB)# separate parts of line, keeping data only ValueError: unpack list of wrong size ---Error Ends--- Any ideas? Thank you! --dan From: Dan Liang Subject: [Tutor] Iterating over a long list with regular expressions

Re: [Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-04 Thread spir
Le Sun, 3 May 2009 21:59:23 -0400, Dan Liang s'exprima ainsi: > Hi tutors, > > I am working on a file and need to replace each occurrence of a certain > label (part of speech tag in this case) by a number of sub-labels. The file > has the following format: > > word1 \tTag1 > word2 \tT

[Tutor] Iterating over a long list with regular expressions and changing each item?

2009-05-03 Thread Dan Liang
Hi tutors, I am working on a file and need to replace each occurrence of a certain label (part of speech tag in this case) by a number of sub-labels. The file has the following format: word1 \tTag1 word2 \tTag2 word3 \tTag3 Now the tags are complex and I wanted to split them in a

Re: [Tutor] Iterating over letters or arbitrary symbols like they were numbers...

2009-03-18 Thread Kent Johnson
On Wed, Mar 18, 2009 at 5:26 PM, Alexander Daychilde (Gmail) wrote: > exp_list = [] > > exp_range = exp.split(":") > > min_padding = len(exp_range[0]) > > for i in range(int(exp_range[0]),(int(exp_range[1])+1)): > >    exp_list.append('%0*d' % (min_padding, i)) This could be a little cleaner usi

Re: [Tutor] Iterating over letters or arbitrary symbols like they were numbers...

2009-03-18 Thread John Fouhy
2009/3/19 Alexander Daychilde (Gmail) : > That creates a list of numbers. I also need to do letters. That is, treat > a-z as base 26, and do the same thing. The three examples I gave from before > would be: >        1:9 --> a:z >        1:99 --> a:zz >        01:99 -- no "zero" in alpha to worry ab

Re: [Tutor] Iterating over letters or arbitrary symbols like they were numbers...

2009-03-18 Thread Wayne Watson
Title: Signature.html It looks like you used the wrong thread to post this. It's connected to questions about linux and Win, and not iterations. Alexander Daychilde (Gmail) wrote: If there's an easy way to do this, I'd like to have a pointer to it (i.e. what functions would deal with this -

[Tutor] Iterating over letters or arbitrary symbols like they were numbers...

2009-03-18 Thread Alexander Daychilde (Gmail)
If there's an easy way to do this, I'd like to have a pointer to it (i.e. what functions would deal with this - not wanting my code written for me...) Right now, I have written code to generate a list of strings that happen to be a range of numbers. (The fact that they're strings is actually desir

Re: [Tutor] Iterating two one dimensional lists to create a multidemsional array

2008-10-11 Thread W W
On Fri, Oct 10, 2008 at 10:59 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, Oct 10, 2008 at 10:45 PM, S Potter <[EMAIL PROTECTED]> wrote: > >> poem ={ ['roses','are red'], >> ['violets','are blue'], >> ['sugar','are sweet'] >> ['so','are you'] } An

Re: [Tutor] Iterating two one dimensional lists to create a multidemsional array

2008-10-10 Thread Kent Johnson
On Fri, Oct 10, 2008 at 10:45 PM, S Potter <[EMAIL PROTECTED]> wrote: > OK I'm still new to the python list / array deal. I'm having problems with > something very simple that I could accomplish in C++ but > for some reason I'm not grasping it python. > > Here is my example psuedo code: > > I have

[Tutor] Iterating two one dimensional lists to create a multidemsional array

2008-10-10 Thread S Potter
OK I'm still new to the python list / array deal. I'm having problems with something very simple that I could accomplish in C++ but for some reason I'm not grasping it python. Here is my example psuedo code: I have two lists: items = ['roses','violets','sugar','so'] and attributes = ['red','b

Re: [Tutor] iterating data and populating a dictionary

2008-08-07 Thread W W
On Thu, Aug 7, 2008 at 12:42 AM, Shrutarshi Basu <[EMAIL PROTECTED]>wrote: > If you're just going to be using numbers as dictionary keys, it might > be simpler just to use a list structure. Dictionaries don't preserve > order, so you'd need to write extra code if you ever need to iterate > over it

Re: [Tutor] iterating data and populating a dictionary

2008-08-06 Thread Shrutarshi Basu
If you're just going to be using numbers as dictionary keys, it might be simpler just to use a list structure. Dictionaries don't preserve order, so you'd need to write extra code if you ever need to iterate over it in order. Since your code increments field everytime it gets a new record you can j

  1   2   >