Mar-22-2018, 06:41 PM
Ok, so you're starting with
That, right there, could be solved with itertools.product. But that's not the end of the story. You also want to rotate the base list each time you iterate over it. I would be surprised if itertools had something that could handle that, since it seems like a non-standard need.
But that's fine, because it isn't all that complicated anyhow :p
The key to solving this is indexing/slicing. The rest of it you can probably cobble together in any number of different ways, heck you can even skip the slicing (
4, 7, 8
, and you're multiplying each item by range(1, 5, 2)
, which is 1, 3
.That, right there, could be solved with itertools.product. But that's not the end of the story. You also want to rotate the base list each time you iterate over it. I would be surprised if itertools had something that could handle that, since it seems like a non-standard need.
But that's fine, because it isn't all that complicated anyhow :p
The key to solving this is indexing/slicing. The rest of it you can probably cobble together in any number of different ways, heck you can even skip the slicing (
list.pop(0)
followed by .append()
).>>> items = [4, 7, 8] >>> for ndx in range(1, 5, 2): ... print(list(map(lambda x: x*ndx, items))) ... first = items[0] ... rest = items[1:] ... items = rest + [first] ... [4, 7, 8] [21, 24, 12]