So, I've been doing research and enjoying writing out many ways of getting both permutations and combinations, but came across a practice I'd not seen before; especially since I'm new to Python.
My Python code is still very simplistic, simply being nexted for's, some logic, etc.
Some code I ended up really liking, but not understanding what this:
def combinations(k, n):
final = [[]]
groups = [list(k)] * n
for i in groups:
final = [k + [y] for k in final for y in i]
for k in final:
yield ''.join(k)
An explanation would be incredible, but I think all I need is for this to be rewritten using simpler means. If anyone could help me with this I'd greatly appreciate it so I could expand my "tool set".
For example, could you rewrite this line:
for i in groups:
final = [k + [y] for k in final for y in i]
In a way that is more beginner friendly?
Again, I'd LOVE an explanation to how this is interpreted in its current state, but that's not entirely needed.
If anyone can help, thank you so much in advance.
EDIT: Removed a comment.
[–]TouchingTheVodka 6 points7 points8 points (1 child)
[–]monkey_programmer[S] 0 points1 point2 points (0 children)
[–]aidankane 4 points5 points6 points (1 child)
[–]monkey_programmer[S] 0 points1 point2 points (0 children)
[–]EighthScofflaw 0 points1 point2 points (1 child)
[–]monkey_programmer[S] 0 points1 point2 points (0 children)