#🔒 How can I write a generator function from N to N^3?
43 messages · Page 1 of 1 (latest)
@hybrid lantern
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
I tried making it go like
(1,2,3)
(1,2,4)
(1,3,4)
(2,3,4)
(1,2,5)
(1,3,5)
(1,4,5)
(2,3,5)
(3,4,5)
(1,2,6)
...
So increasing the highest number only when neccesary
But I don't really see how
I don't really get it, you want a cartesian product?
Ye
!d itertools.product
itertools.product(*iterables, repeat=1)```
Cartesian product of input iterables.
Roughly equivalent to nested for-loops in a generator expression. For example, `product(A, B)` returns the same as `((x,y) for x in A for y in B)`.
The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted in sorted order.
To compute the product of an iterable with itself, specify the number of repetitions with the optional *repeat* keyword argument. For example, `product(A, repeat=4)` means the same as `product(A, A, A, A)`.
Can do this and filter them
Or write one yourself to already incorporate the constraint
Well, I want it a bit faster than having to get rid of them all, is there a way I get the original function and edit it to remove the extra ones?
!e
for x in range(1, 7):
for y in range(x, 7):
for z in range(y, 7):
print(x, y, z)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1 1 1
002 | 1 1 2
003 | 1 1 3
004 | 1 1 4
005 | 1 1 5
006 | 1 1 6
007 | 1 2 2
008 | 1 2 3
009 | 1 2 4
010 | 1 2 5
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/RUNZU2XPXLZDRXFQ7GY4MDXC2U
Well that'll stop after 7^3 numbers
You wanted it to stop after N^3 right?
No, I want it to go indefinitely
The bijection is from N to N^3, or as you said, the cartesian product of N^3
N is just the natural numbers, not an actual number like 7
This
The usual
But in 3D
import itertools.count
for x in itertools.count(start=3):
for y in range(2, x):
for z in range(1, y):
print(z, y, x)
Something like this?
Edited*
Yes, that works
How does it work?
Well, I can find that out myself
Thanks you so much!
!close
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.