Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertools and amazing speed
#3
perm_set is a permutations object, a generator like object that creates results on demand. To see how long this takes you could ask for a list. I doubt you have enough memory.
perm_set = list(itertools.permutations(images,9))
That an itertools.permutations object is a generator is mentioned in the documentation when they say permutations is "roughly equivalent to"
def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = list(range(n))
    cycles = list(range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])  # <------ The first tuple.
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])   # <------ More tuples
                break
        else:
            return
Pedroski55 likes this post
Reply


Messages In This Thread
itertools and amazing speed - by Pedroski55 - Nov-07-2022, 04:59 AM
RE: itertools and amazing speed - by ndc85430 - Nov-07-2022, 05:03 AM
RE: itertools and amazing speed - by deanhystad - Nov-07-2022, 05:41 AM
RE: itertools and amazing speed - by Pedroski55 - Nov-07-2022, 11:07 PM
RE: itertools and amazing speed - by bowlofred - Nov-08-2022, 01:04 AM
RE: itertools and amazing speed - by Pedroski55 - Nov-10-2022, 12:49 AM
RE: itertools and amazing speed - by Gribouillis - Nov-10-2022, 03:06 PM
RE: itertools and amazing speed - by Pedroski55 - Nov-10-2022, 11:15 PM
RE: itertools and amazing speed - by Gribouillis - Nov-11-2022, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? Pedroski55 3 2,508 Nov-29-2020, 08:35 AM
Last Post: DeaD_EyE
  Making lists using itertools and eliminating duplicates. mike3891 2 2,318 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,146 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,902 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,995 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  can itertools compact a list removing all of some value? Skaperen 6 3,299 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  Help with itertools jarrod0987 1 1,879 Jun-10-2019, 02:41 AM
Last Post: Larz60+
  ImportError: No module named jaraco.itertools in Python manhnt 0 3,050 Nov-08-2018, 11:41 AM
Last Post: manhnt
  Need help with itertools.islice() Charles1 2 2,896 Sep-19-2018, 10:32 AM
Last Post: Charles1
  itertools jarrod0987 1 2,535 Jul-27-2018, 06:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020