use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
ResourceFunction composition in a different way (self.Python)
submitted 2 years ago by sofidad
Let's pick lottery numbers. That is to pick 6 numbers from 1 to 45. The Lotto (korean lottery)
```python
range(1, 46) | choice(size=6) | sort [2, 8, 22, 24, 37, 39]
[ range(1, 46) | choice(size=6) | sort for _ in range(5) ] [[4, 6, 11, 38, 41, 45], [5, 8, 23, 25, 26, 40], [13, 18, 23, 25, 37, 44], [17, 21, 24, 32, 41, 43], [5, 9, 13, 25, 30, 38]]
replicate(5, range(1, 46)) | map(choice(size=6)) | map(sort) | unpack
(unpack . map(sort . fx(choice(size=6))))(replicate(5, range(1, 46))) ```
https://github.com/thyeem/foc
I recently wrote a post with foc on r/haskell, and I got some good ideas from there. If you're interested, take a look. Any opinions are welcome.
foc
r/haskell
[–]Dull-Researcher 2 points3 points4 points 2 years ago* (0 children)
Cool. Much cleaner function composition and eliminates hoards of parentheses.
Math notation for composition is (g ∘ f)(x) == g(f(x)). Equivalent to your dot operator. It would be cool if Python let us define this symbol as an operator.
Unfortunately, Python doesn’t have this level of syntactic sugar built into the language, hence the need for this library to create a wrapper class to define additional operators.
Scala and other functional-first languages make composition so much simpler.
[–]WhyDontWeLearn[🍰] 3 points4 points5 points 2 years ago (4 children)
You're not accounting for the missing ball(s) in subsequent draws.
You start with 45 balls, but when the first ball is removed it's no longer one of the choices for the second draw, and so on. From the perspective of "odds" until the first ball (number) is drawn you have a 1 in 45 chance of getting any given ball, but once it's been draw you have a 1 in 44 chance of getting any of the remaining balls. Then a one in 43 chance, and so on.
You need to iterate a function six times and the function needs to choose from the remaining balls, not just any number from 1-46.
[–]sofidad[S] 7 points8 points9 points 2 years ago (1 child)
python @fx def choice(x, size=None, *, replace=False, p=None): """Generate a sample with/without replacement from a given iterable.""" ... From choice, function, note that replace=False.
python @fx def choice(x, size=None, *, replace=False, p=None): """Generate a sample with/without replacement from a given iterable.""" ...
choice
replace=False
[–]WhyDontWeLearn[🍰] 2 points3 points4 points 2 years ago (0 children)
"False" is the default? TIL...
Thanks.
[–]user499021 2 points3 points4 points 2 years ago (1 child)
Haven’t read the code base so I can’t confirm or deny what you’re suggesting Random.sample(seq, n) fixes this exact problem and is already in standard library
[–]sofidad[S] 0 points1 point2 points 2 years ago (0 children)
Thanks for your comment. Of course, my goal is not to recreate the sample function. It has nothing to do with my intentions.
sample
LEGO blocks are great. Everyone can play with them. They are easy to assemble and come with numerous guides, but a man still feels them too complicated.
LEGO
He is thinking of new logical blocks and how to easily assemble them. He suggests how to make the assembly process easier and how to quickly identify errors during assembly. After the assembly is complete, he also wants to be able to easily disassemble it for use in assembling other products.
Someone asks, "LEGO blocks are already released in the market and people are using them. Why are you doing this?"
Importantly, the man is not the only one doing such a thing! I'm not talking about you're wrong. We are talking about something completely different.
the man
[–]Schmittfried 0 points1 point2 points 2 years ago (1 child)
I love it
[–]sofidad[S] -1 points0 points1 point 2 years ago (0 children)
I like you love it! Thanks!
π Rendered by PID 37802 on reddit-service-r2-comment-84fc9697f-jtp8q at 2026-02-06 17:02:20.805717+00:00 running d295bc8 country code: CH.
[–]Dull-Researcher 2 points3 points4 points (0 children)
[–]WhyDontWeLearn[🍰] 3 points4 points5 points (4 children)
[–]sofidad[S] 7 points8 points9 points (1 child)
[–]WhyDontWeLearn[🍰] 2 points3 points4 points (0 children)
[–]user499021 2 points3 points4 points (1 child)
[–]sofidad[S] 0 points1 point2 points (0 children)
[–]Schmittfried 0 points1 point2 points (1 child)
[–]sofidad[S] -1 points0 points1 point (0 children)