[Python-ideas] Set starting point for itertools.product()

Stephen J. Turnbull turnbull.stephen.fw at u.tsukuba.ac.jp
Thu Oct 25 02:59:21 EDT 2018


Ronie Martinez writes:

 > def main():
 >     datetime_odometer = itertools.product(
 >         range(2018, 10_000),  # year
 >         range(1, 13),  # month
 >         range(1, 31),  # days
 >         range(0, 24),  # hours
 >         range(0, 60),  # minutes
 >         range(0, 60)  # seconds
 >     )
 > 
 >     datetime_of_interest = (2050, 6, 15, 10, 5, 0)
 > 
 >     for i in datetime_odometer:
 >         if i == datetime_of_interest: # target start time
 >             break

I don't understand the issue.  Doesn't

    def make_odometer(year, month, day, hour, minute, second):
        return itertools.product(
            range(year, 10_000),
            range(month, 13),
            range(day, 31),
            range(hour, 24),
            range(minute, 60),
            range(second, 61)        # leap seconds!
        )

    def main():
        datetime_of_interest = (2050, 6, 15, 10, 5, 0)

        datetime_odometer = make_odometer(*datetime_of_interest)

do what you want?  If you have a task where that *doesn't* do what's
needed, eg, where the "odometer wheels" are an iterated function
system, I don't see any way to avoid the problem.  If you have some
range-like object that doesn't support starting values, you need to
fix that or there's nothing that could be done about it in itertools.

(Yet Another) Steve

-- 
Associate Professor              Division of Policy and Planning Science
http://turnbull.sk.tsukuba.ac.jp/     Faculty of Systems and Information
Email: turnbull at sk.tsukuba.ac.jp                   University of Tsukuba
Tel: 029-853-5175                 Tennodai 1-1-1, Tsukuba 305-8573 JAPAN


More information about the Python-ideas mailing list