looking for a neat solution to a nested loop problem
Tom P
werotizy at freent.dd
Mon Aug 6 13:16:45 EDT 2012
On 08/06/2012 06:03 PM, John Gordon wrote:
> In <a8a7hvF8crU1 at mid.individual.net> Tom P <werotizy at freent.dd> writes:
>
>> consider a nested loop algorithm -
>
>> for i in range(100):
>> for j in range(100):
>> do_something(i,j)
>
>> Now, suppose I don't want to use i = 0 and j = 0 as initial values, but
>> some other values i = N and j = M, and I want to iterate through all
>> 10,000 values in sequence - is there a neat python-like way to this? I
>> realize I can do things like use a variable for k in range(10000): and
>> then derive values for i and j from k, but I'm wondering if there's
>> something less clunky.
>
> You could define your own generator function that yields values
> in whatever order you want:
>
> def my_generator():
> yield 9
> yield 100
> for i in range(200, 250):
> yield i
> yield 5
>
>
Thanks, I'll look at that but I think it just moves the clunkiness from
one place in the code to another.
More information about the Python-list
mailing list