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


Groups > comp.lang.python > #63117

Re: Strange behaviour with a for loop.

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mhysnm1964@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'parameters': 0.04; 'string.': 0.05; 'element': 0.07; 'modify': 0.07; 'sys': 0.07; 'string': 0.09; 'character,': 0.09; 'chunk': 0.09; 'filename': 0.09; 'filenames': 0.09; 'filenames:': 0.09; 'lawrence': 0.09; 'newly': 0.09; 'spelled': 0.09; 'strings.': 0.09; 'typed': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; 'language.': 0.14; 'question.': 0.14; '%s\\n"': 0.16; '(list)': 0.16; '3.3,': 0.16; 'element.': 0.16; 'filename)': 0.16; 'invalid.': 0.16; 'referencing': 0.16; 'sliced': 0.16; 'unlikely': 0.16; 'used:': 0.16; 'index': 0.16; ':-)': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'result.': 0.19; 'slightly': 0.19; 'starts': 0.20; '>>>': 0.22; 'memory': 0.22; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'load': 0.23; 'sean': 0.24; "shouldn't": 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'script': 0.25; 'mention': 0.26; 'shown': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'testing': 0.29; 'feature': 0.29; 'array': 0.29; 'character': 0.29; "doesn't": 0.30; 'characters': 0.30; 'errors': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; 'context.': 0.31; 'os,': 0.31; 'file': 0.32; 'languages': 0.32; 'option': 0.32; 'regular': 0.32; 'another': 0.32; 'text': 0.33; 'worked': 0.33; 'url:python': 0.33; 'beginning': 0.33; 'position.': 0.33; 'core': 0.34; "i'd": 0.34; 'subject:with': 0.35; 'basic': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'everyone.': 0.36; 'in.': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; "didn't": 0.36; 'method': 0.36; 'useful': 0.36; 'charset:us-ascii': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'positive': 0.37; 'wrong': 0.37; 'list': 0.37; 'starting': 0.37; 'being': 0.38; 'message- id:@gmail.com': 0.38; 'ends': 0.38; 'work?': 0.38; 'planning': 0.38; 'pm,': 0.38; 'rather': 0.38; 'anything': 0.39; 'does': 0.39; 'either': 0.39; 'url:mail': 0.40; '2nd': 0.60; 'blank': 0.60; 'expression': 0.60; 'negative': 0.60; 'number,': 0.60; 'results.': 0.60; 'lower': 0.61; 'new': 0.61; 'providing': 0.61; 'first': 0.61; 'back': 0.62; "you've": 0.63; 'header:Message-Id:1': 0.63; 'our': 0.64; 'provide': 0.64; 'more': 0.64; 'different': 0.65; 'forward': 0.65; 'within': 0.65; 'anything.': 0.68; 'results': 0.69; 'wish': 0.70; 'cut': 0.74; 'ending': 0.78; 'counts': 0.83; '"".': 0.84; 'does?': 0.84; 'end.': 0.84; 'to:addr:yahoo.co.uk': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=nhPORZ784sJ3pSjAK8DVM/qeZ4wDisSuKmLgEQPntmg=; b=dmQL3q76NNCzIFN9nbOHOwfe7+jF6G/fnrbompksDlY864gKbsyNSv7ehz6BbkCGJk ++jGSOR/7FIYnSKo5GubQz1exePelYd5xMotwCA3svE6VpRK/A1KC8LLZU2FwpvUmksB UtJhDOiVtDWJLYW7oivI2TGL99PNRZSmB5ceNvfhmnHhZt02UJuuyE/fKzRw6d2n2CMK UD1ok/6WLiYzEPEkX7qbUFSrjOkc4QlrQJUdSgS4fG4bNw3db2IPHv9I/TnuI+pb33Cn W+TvFeSfrEdsy3vU7fC/d+eyEywpiweovljh1zMY2egQWY/JGE5d7ReXGy5Jw3mwm1wu jHBA==
X-Received by 10.68.66.1 with SMTP id b1mr101169629pbt.43.1388817169557; Fri, 03 Jan 2014 22:32:49 -0800 (PST)
Content-Type text/plain; charset=us-ascii
Mime-Version 1.0 (Mac OS X Mail 6.6 \(1510\))
Subject Re: Strange behaviour with a for loop.
From Sean Murphy <mhysnm1964@gmail.com>
In-Reply-To <3EA05A2F-8F5D-43D3-AB9A-B3C50C4B74A6@gmail.com>
Date Sat, 4 Jan 2014 17:32:44 +1100
Content-Transfer-Encoding quoted-printable
References <4DC5A4FC-CCAF-446B-B41C-23E52C2389B6@icloud.com> <la86n0$sdk$1@ger.gmane.org> <3EA05A2F-8F5D-43D3-AB9A-B3C50C4B74A6@gmail.com>
To Mark Lawrence <breamoreboy@yahoo.co.uk>
X-Mailer Apple Mail (2.1510)
X-Mailman-Approved-At Sat, 04 Jan 2014 08:31:07 +0100
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4897.1388820668.18130.python-list@python.org> (permalink)
Lines 161
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388820668 news.xs4all.nl 2944 [2001:888:2000:d::a6]:60573
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63117

Show key headers only | View raw


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
> 

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web