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


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

Resetting the Range start in the middle of a loop

Started bybigred04bd3@gmail.com
First post2015-10-23 08:42 -0700
Last post2015-10-23 19:31 -0700
Articles 6 — 5 participants

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


Contents

  Resetting the Range start in the middle of a loop bigred04bd3@gmail.com - 2015-10-23 08:42 -0700
    Re: Resetting the Range start in the middle of a loop John Gordon <gordon@panix.com> - 2015-10-23 17:15 +0000
    Re: Resetting the Range start in the middle of a loop Peter Pearson <pkpearson@nowhere.invalid> - 2015-10-23 17:23 +0000
    Re: Resetting the Range start in the middle of a loop Steven D'Aprano <steve@pearwood.info> - 2015-10-24 11:05 +1100
    Re: Resetting the Range start in the middle of a loop Nobody <nobody@nowhere.invalid> - 2015-10-24 03:13 +0100
    Re: Resetting the Range start in the middle of a loop bigred04bd3@gmail.com - 2015-10-23 19:31 -0700

#97921 — Resetting the Range start in the middle of a loop

Frombigred04bd3@gmail.com
Date2015-10-23 08:42 -0700
SubjectResetting the Range start in the middle of a loop
Message-ID<ea58da60-3fb1-46e1-91db-ed48beebfd5c@googlegroups.com>
I am wanting to reset a Range start while a loop is running.  here is what I have:


s = 1
for i in range(s, 1000):
    # do stuff
        for r in range(i, 1000):
            # do stuff
            s = s + 100  #example
            break


I've also tried to update the i as well, but it just continues on to the next number as if it was never updated.

any help would be great

Thanks

[toc] | [next] | [standalone]


#97922

FromJohn Gordon <gordon@panix.com>
Date2015-10-23 17:15 +0000
Message-ID<n0dq07$2tm$1@reader1.panix.com>
In reply to#97921
In <ea58da60-3fb1-46e1-91db-ed48beebfd5c@googlegroups.com> bigred04bd3@gmail.com writes:

> I am wanting to reset a Range start while a loop is running.
> here is what I have:

> s = 1
> for i in range(s, 1000):
>     # do stuff
>         for r in range(i, 1000):
>             # do stuff
>             s = s + 100  #example
>             break

> I've also tried to update the i as well, but it just continues on to
> the next number as if it was never updated.

You can't do this.  The range is only calculated once, and changing s or i
afterwards is too late to have any effect.

You could use a while loop instead, like this:

    i = 1
    while i <= 1000:

        # do stuff...

        if some_condition:
            # skip ahead in the loop
            i = i + 100

        else:
            # move i ahead normally
            i = i + 1

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#97923

FromPeter Pearson <pkpearson@nowhere.invalid>
Date2015-10-23 17:23 +0000
Message-ID<d8v8p7Fem43U1@mid.individual.net>
In reply to#97921
On Fri, 23 Oct 2015 08:42:53 -0700 (PDT), bigred04bd3@gmail.com wrote:
> I am wanting to reset a Range start while a loop is running.  here is
> what I have:
>
> s = 1
> for i in range(s, 1000):
>     # do stuff
>         for r in range(i, 1000):
>             # do stuff
>             s = s + 100  #example
>             break
>
> I've also tried to update the i as well, but it just continues on to
> the next number as if it was never updated.
>
> any help would be great

The "s = s + 100" cannot affect the outer for loop because by the time
it is executed, the expression "range(s, 1000)" has already been
evaluated (i.e., it has been turned into an object) and is in the grips
of the outer "for" machinery.

Beyond that, though, this is a peculiar thing to want to do.  If you
want to take such liberties with i, then a "for" probably isn't the
clearest construction to use.  If you share a *slightly* broader view
of your goals, the unusually helpful experts in this group might treat
us to an informative discussion of elegant ways to get there.

-- 
To email me, substitute nowhere->runbox, invalid->com.

[toc] | [prev] | [next] | [standalone]


#97924

FromSteven D'Aprano <steve@pearwood.info>
Date2015-10-24 11:05 +1100
Message-ID<562acb3f$0$1614$c3e8da3$5496439d@news.astraweb.com>
In reply to#97921
On Sat, 24 Oct 2015 02:42 am, bigred04bd3@gmail.com wrote:

> I am wanting to reset a Range start while a loop is running.  here is what
> I have:
> 
> s = 1
> for i in range(s, 1000):
>     # do stuff
>         for r in range(i, 1000):
>             # do stuff
>             s = s + 100  #example
>             break

The above code is perfectly legal and "works", for some definition of works.
But your example is unclear, and I cannot tell what you mean by "resetting
the range start". One problem with the example is that the inner loop
*unconditionally* exits with a break after just one loop, so it is
pointless and can be replaced by this:

s = 1
for i in range(s, 1000):
    # do stuff
    r = i
    # do stuff
    s = s + 100

My *guess* is that this will solve your problem:

for i in range(1, 1000, 100):
    # do stuff


but I really can't tell.

It would help if you show us the actual values you expect to see in the
loop. For example, perhaps you mean something like this:

# outer loop variable, i
# inner loop variable, r
i=1, r=1
i=1, r=2
i=1, r=3
...
i=1, r=999
i=100, r=100
i=100, r=101
i=100, r=102
...
i=100, r=999
i=200, r=200
i=200, r=201
i=200, r=202
...
i=200, r=999
i=300, r=300
...
i=300, r=999
...
i=900, r=900
...
i=900, r=999



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#97925

FromNobody <nobody@nowhere.invalid>
Date2015-10-24 03:13 +0100
Message-ID<pan.2015.10.24.02.13.54.891000@nowhere.invalid>
In reply to#97921
On Fri, 23 Oct 2015 08:42:53 -0700, bigred04bd3 wrote:

> I am wanting to reset a Range start while a loop is running.  here is what
> I have:
> 
> s = 1
> for i in range(s, 1000):
>     # do stuff
>         for r in range(i, 1000):
>             # do stuff
>             s = s + 100  #example
>             break
> 
> 
> I've also tried to update the i as well, but it just continues on to the
> next number as if it was never updated.

You either need a while loop or a custom iterator. E.g.:

class Iterator(object):
    def __init__(self, start, end):
        self.cur = start
        self.end = end

    def set(self, cur):
        self.cur = cur

    def __iter__(self):
        return self

    def next(self):
        if self.cur >= self.end:
            raise StopIteration
        i = self.cur
        self.cur += 1
        return i

So then you can do e.g.:

it = Iterator(s, 1000)
for i in it:
    # do stuff
    for r in range(i, 1000):
        # do stuff
        s = s + 100  #example
        it.set(s)
        break

Unless you need to do this sort of thing a lot, use a while loop.

A "for" loop is used when the set of values is known in advance.
C's "for" loops (since copied by other C-like languages) are "while" loops
in disguise. Python's "for" loops are actually "for" loops.

[toc] | [prev] | [next] | [standalone]


#97926

Frombigred04bd3@gmail.com
Date2015-10-23 19:31 -0700
Message-ID<e31ca6b3-c752-4142-b6d2-0b05f7d56443@googlegroups.com>
In reply to#97921
thanks for all the replies.  I took the while advice and have been trying to get it work correctly since this afternoon.

WHILE (no pun inteneded lol) it did solve my initial problem, it opened up another...the main reason i try to stay away from while loops...i created an infinate loop ha. so I'm trying to get it to do what I want, and still be able to exit.

Ill hack away at it tonight and try to post back tomorrow if i cant get it.

thanks again.

[toc] | [prev] | [standalone]


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


csiph-web