Re: [Tutor] Splitting a string

2011-02-09 Thread Alan Gauld
"tee chwee liong" wrote t> hanks for catching the bug. the code should be: s='11100101' found = False for i,c in enumerate(reversed(s)): if c == '0': print 'Lane fail',i found = True if not found: print 'All lanes PASS' the if not found needs to be indented. No, e

Re: [Tutor] Splitting a string

2011-02-09 Thread tee chwee liong
> No, it doesn't work. You haven't sufficiently tested it. It tells lies: > > > >>> s='11100101' > >>> found = False > >>> for i,c in enumerate(s): > ... if c == '0': > ... print 'Lane fail',i > ... found = True > ... if not found: print 'All lanes PASS' > ... > All lanes PASS > All lanes PASS >

Re: [Tutor] Splitting a string

2011-02-09 Thread Steven D'Aprano
tee chwee liong wrote: hi all, the code works: s='00101' found = False for i,c in enumerate(s): if c == '0': print 'Lane fail',i found = True if not found: print 'All lanes PASS No, it doesn't work. You haven't sufficiently tested it. It tells lies: >>> s='111

Re: [Tutor] Splitting a string

2011-02-09 Thread ALAN GAULD
>s='00101' >found = False >for i,c in enumerate(s): >if c == '0': >print 'Lane fail',i >found = True >if not found: print 'All lanes PASS ># > >the enumerate is checking from left to right. is it possible check from right >to >left? > >for eg:s='00101' >The ea

Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong
hi all, the code works: s='00101' found = False for i,c in enumerate(s): if c == '0': print 'Lane fail',i found = True if not found: print 'All lanes PASS # Result: >>> Lane fail 0 Lane fail 1 Lane fail 3 >>> the enumerate is checking from left to right. is

Re: [Tutor] Splitting a string

2011-02-08 Thread Alan Gauld
"tee chwee liong" wrote ### c=('01101') i=-1 try: while i print "Lane fail",i except ValueError: print "All Lanes PASS" pass # The first thing is that you never increment i so the while condition is never going to terminate, so its probably just

Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong
> `while i < len(c)` instead of `while 1` > hi, i modified codes to be i>> Lane fail 0 Lane fail 3 All Lanes PASS >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options

Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong
> I'm not sure what you mean by highlight the location. > Is it a GUI? Are you colour coding the characters? > hi, no it is not GUI. i just want to know where is the location of the 0 in the returned string. For eg: 10111, i want to say 0 is at lane 3 (calculating location from right to le

Re: [Tutor] Splitting a string

2011-02-08 Thread Alan Gauld
"tee chwee liong" wrote i have a function which returns a string. for eg: X='101110'. i want to search for 0 and highlight the location. I'm not sure what you mean by highlight the location. Is it a GUI? Are you colour coding the characters? i am thinking of defining X as a list. You

Re: [Tutor] Splitting a string

2011-02-08 Thread Hugo Arts
On Tue, Feb 8, 2011 at 2:47 PM, Christian Witts >>> `while i<  len(c)` instead of `while 1` >>> >> >> You remind me of me...like it was yesterday. Explanations with no >> thought. Unimpressive isn't it? >> > > While the index is smaller than the length of the string do your processing. >  It reads

Re: [Tutor] Splitting a string

2011-02-08 Thread Peter Otten
Christian Witts wrote: > On 08/02/2011 15:04, tee chwee liong wrote: >> hi all, >> >> thanks for the advice. i modified my code to be: >> >> c=('01101') >> i=-1 >> try: >> while 1: >> i=c.index('0',i+1) >> print "Lane fail",i >> except ValueError: >> print "All Lanes PASS"

Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts
On 08/02/2011 15:38, David Hutto wrote: On Tue, Feb 8, 2011 at 8:36 AM, Christian Witts wrote: On 08/02/2011 15:04, tee chwee liong wrote: hi all, thanks for the advice. i modified my code to be: c=('01101') i=-1 try: while 1: i=c.index('0',i+1) print "Lane fail

Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
On Tue, Feb 8, 2011 at 8:36 AM, Christian Witts wrote: > On 08/02/2011 15:04, tee chwee liong wrote: >> >> hi all, >> >> thanks for the advice. i modified my code to be: >> >> c=('01101') >> i=-1 >> try: >>    while 1: >>        i=c.index('0',i+1) >>        print "Lane fail",i >> except ValueError

Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts
On 08/02/2011 15:04, tee chwee liong wrote: hi all, thanks for the advice. i modified my code to be: c=('01101') i=-1 try: while 1: i=c.index('0',i+1) print "Lane fail",i except ValueError: print "All Lanes PASS" pass when i run, the result is: >>> Lane fail 0 Lane

Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
Don't use the try and except, use a if else. I'm not as good as an explainer as the pros, but I can make enough sense if you respond back with a useful set of examples you've tried. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscrip

Re: [Tutor] Splitting a string

2011-02-08 Thread tee chwee liong
hi all, thanks for the advice. i modified my code to be: c=('01101') i=-1 try: while 1: i=c.index('0',i+1) print "Lane fail",i except ValueError: print "All Lanes PASS" pass when i run, the result is: >>> Lane fail 0 Lane fail 3 All Lanes PASS Qu

Re: [Tutor] Splitting a string

2011-02-08 Thread David Hutto
> To get a list, just ask for one: > list("abcd") > ['a', 'b', 'c', 'd'] > or., and this isn't to argue with anyone;), you could: >>> x = 'abcd' >>> y = [] >>> for letter in x: ... y.append(letter) ... >>> print y ['a', 'b', 'c', 'd'] which explains that the list is derived from the st

Re: [Tutor] Splitting a string

2011-02-08 Thread Steven D'Aprano
tee chwee liong wrote: hi all, i have a function which returns a string. for eg: X='101110'. i want to search for 0 and highlight the location. i am thinking of defining X as a list. but how can i split 101110 as there are no spaces in between? i'm thinking if i can put it into a list: X=['1','0

Re: [Tutor] Splitting a string

2011-02-08 Thread Christian Witts
On 08/02/2011 11:20, tee chwee liong wrote: hi all, i have a function which returns a string. for eg: X='101110'. i want to search for 0 and highlight the location. i am thinking of defining X as a list. but how can i split 101110 as there are no spaces in between? i'm thinking if i can put it

Re: [Tutor] Splitting a string

2011-02-08 Thread col speed
On 8 February 2011 16:20, tee chwee liong wrote: > hi all, > > >i have a function which returns a string. for eg: X='101110'. i want to > search for 0 and highlight the location. > i> am thinking of defining X as a list. but how can i split 101110 as there > are no spaces in between? i'm thinkin

[Tutor] Splitting a string

2011-02-08 Thread tee chwee liong
hi all, i have a function which returns a string. for eg: X='101110'. i want to search for 0 and highlight the location. i am thinking of defining X as a list. but how can i split 101110 as there are no spaces in between? i'm thinking if i can put it into a list: X=['1','0','1','1','1','0'],

Re: [Tutor] Splitting a string into n-sized bytes

2006-03-15 Thread Kent Johnson
Steve Nelson wrote: > Indeed - as I now have a function: > > def nsplit(s, n): > return [s[i:i+n] for i in range(0, len(s), n)] > > Incidentally I am currently going with: > > def nsplit(s, n): > while s: >yield s[:n] >s = s[n:] You can write the generator function to use the same m

Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
On 3/14/06, Adam <[EMAIL PROTECTED]> wrote: > Hopefully that should point you in the right direction to do n-sized > words as well. Indeed - as I now have a function: def nsplit(s, n): return [s[i:i+n] for i in range(0, len(s), n)] Incidentally I am currently going with: def nsplit(s, n):

Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Smith
| From: "Steve Nelson" | | Further to my previous puzzling, I've been working out the best way to | chop a string up into n-sized words: | I think the follow use of groupby is from Raymond Hettinger from ASPN recipes. The batch() function will return an iterable to you in user-definable sized

Re: [Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Adam
On 14/03/06, Steve Nelson <[EMAIL PROTECTED]> wrote: > Hello all, > > Further to my previous puzzling, I've been working out the best way to > chop a string up into n-sized words: > > I'm aware that I can use a slice of the string, with, eg, myWord[0:4]. > > I am also aware that I can do blob = myW

[Tutor] Splitting a string into n-sized bytes

2006-03-14 Thread Steve Nelson
Hello all, Further to my previous puzzling, I've been working out the best way to chop a string up into n-sized words: I'm aware that I can use a slice of the string, with, eg, myWord[0:4]. I am also aware that I can do blob = myWord.pop(0) to take (and remove) the first character. I can botch