Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63117 > unrolled thread

Re: Strange behaviour with a for loop.

Started bySean Murphy <mhysnm1964@gmail.com>
First post2014-01-04 17:32 +1100
Last post2014-01-04 14:20 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Strange behaviour with a for loop. Sean Murphy <mhysnm1964@gmail.com> - 2014-01-04 17:32 +1100
    Re: Strange behaviour with a for loop. Larry Hudson <orgnut@yahoo.com> - 2014-01-04 14:20 -0800

#63117 — Re: Strange behaviour with a for loop.

FromSean Murphy <mhysnm1964@gmail.com>
Date2014-01-04 17:32 +1100
SubjectRe: Strange behaviour with a for loop.
Message-ID<mailman.4897.1388820668.18130.python-list@python.org>
Hi everyone.

Worked out what I was doing wrong with the string splicing. The offset number was lower then the index number, so it was failing. E.G:




On 04/01/2014, at 4:54 PM, Sean Murphy <mhysnm1964@gmail.com> wrote:

> Thanks everyone.
> 
> Mark thanks for the correction on the ':'. Since I didn't cut and copy, rather typed it out. Errors crept in. :-)
> 
> another question in relation to slicing strings. If you want to get a single character, just using the index position will get it. If I use the following, shouldn't it also work? when I use Python 3.3, it didn't provide anything.
> 
> a = "test.txt"
> print a[3]
> 
print a[4:1] <--- index is 4 and offset is one. This is invalid.

So I suspect the offset number still starts at the beginning of the string and counts forward or another way to look at it you are slicing from element x to element y. If element y is less then element x, return nothing. Does this make sense?

I should have used:

print a[4:6])

to get:

t.t 

The 2nd part of my original question still stands. I will expand upon this a bit more to give more context. I want to print from the beginning of the paragraph to the end. Each paragraph ends with "\n\n\n". 

If I use "\n\n\n" in lines this does return true for the string. But I don't have a starting position and ending position. The list method which I mention before can be sliced by going back one element.

Any suggestion on this would be welcomed. I want to achieve this using standard core python objects/methods.

Sean 
> result is:
> 
> 't
> 
> 
> print a[3:1]
> 
> Nothing is printed. 
> 
> print a[3:2]
> 
> 
> Nothing is printed.
> 
> print a[3:-1]
> 
> t.tx is printed.
> 
> 
> Why doesn't the positive number of characters to be splice return anything while the negative value does?
> 
> sorry about these basic questions. I do like the splice feature within Python. Also what is the best method of testing for a blank string?
> 
> end of paragraph line 1 
> 
> 
> new paragraph of line 1.
> 
> 
> The above example text is what I want to test for. I am planning to either load the whole file in as a single chunk of memory using fp.read() or store it into an array by using fp.readlines(). The first option I see being useful because you can create a regular expression to test for multiple '\n'. While in an array (list) I would have to test for a blank line which I assume would be "".
> 
> Any suggestions on this would be welcomed.
> 
> Sean 
> 
> 
> 
> print a[
> 
> On 04/01/2014, at 4:38 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> 
>> On 04/01/2014 04:03, Sean Murphy wrote:
>>> Hello all.
>>> 
>>> This is a newly question. But I wish to understand why the below code is providing different results.
>>> 
>>> import os, sys
>>> 
>>> 
>>> if len(sys.argv) > 2:
>>>  filenames = sys.argv[1:]
>>> else
>>>  print ("no parameters provided\n")
>>>  sys.edit()
>>> 
>>> for filename in filenames:
>>>  print ("filename is: %s\n" %filename)
>>> 
>>> The above code will return results like:
>>> 
>>> filename is test.txt
>>> 
>>> If I modify the above script slightly as shown below, I get a completely different result.
>>> 
>>> if len(sys.argv) > 2:
>>>  filenames = sys.argv[1]
>>> else
>>>  print ("no parameters provided\n")
>>>  sys.exit()
>>> 
>>> for filename in filenames:
>>>  print ("filename is:  %s\n" % filename)
>>> 
>>> The result is the filename is spelled out a character at a time. The bit I am missing is something to do with splicing or referencing in Python.
>>> 
>>> Why am I getting different results? In other languages I would have got the whole content of the element when using the index of the array (list).
>>> 
>>> 
>>> Sean
>>> filename is: t
>>> filename
>>> 
>> 
>> As you've already had answers I'd like to point out that your test for len(sys.argv) is wrong, else is missing a colon and sys.edit() is very unlikely to work :)
>> 
>> -- 
>> My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language.
>> 
>> Mark Lawrence
>> 
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> 

[toc] | [next] | [standalone]


#63149

FromLarry Hudson <orgnut@yahoo.com>
Date2014-01-04 14:20 -0800
Message-ID<s-mdnW5J4PuGFFXPnZ2dnUVZ_sadnZ2d@giganews.com>
In reply to#63117
On 01/03/2014 10:32 PM, Sean Murphy wrote:
> Hi everyone.
[snip]
> The 2nd part of my original question still stands. I will expand upon this a bit more to give more context. I want to print from the beginning of the paragraph to the end. Each paragraph ends with "\n\n\n".
>
> If I use "\n\n\n" in lines this does return true for the string. But I don't have a starting position and ending position. The list method which I mention before can be sliced by going back one element.
>
> Any suggestion on this would be welcomed. I want to achieve this using standard core python objects/methods.
>
Another useful string method is endswith().  With that you don't need to know the line length:

if line.endswith('\n\n\n'):
     ...

(Of course, there is a corresponding startswith() method also.)

If you are specifically looking for blank lines, someone already suggested isspace().  Another 
possibility is rstrip(), which will remove all trailing whitespace.  So you can check for blank 
lines with:

if line.rstrip() == '':
     ...

There are three of these:
lstrip() is left-strip, which removes leading whitespace,
rstrip() is right-strip, which removes trailing whitespace, and
strip() which removes whitespace from both ends.

All of these are very useful functions.

      -=- Larry -=-

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web