糯词笔记(1 年会员 ¥66 → ¥46.2,2 年会员 ¥112 → ¥78.4,11.1 - 11.11):Kindle 标注管理器。 阅读记录(永久会员:¥168 - ¥158,11.1 - 11.11):帮你养成良好的阅读习惯。 AdGuard(40% off):跨平台广告拦截及隐私保护工具。 Permute(¥69 → ¥47.4):媒体文件格式转换. Dark Mode – Permute now works 100% with the dark mode, even adjusting its Dock icon based on your macOS theme. Image Stitching – Stitch images together into a PDF. HEVC (H.265) Hardware Acceleration – HEVC (H.265) video encoding now supports hardware acceleration, resulting in conversions more than twice as fast. Permute 2.4.1 was added to DownloadKeeper this week and last updated on 12-Nov-2020.New downloads are added to the member section daily and we now have 419,969 downloads for our members, including: TV, Movies, Software, Games, Music and More. It's best if you avoid using common keywords when searching for Permute 2.4.1. Essentially this can be referred to as r-permutations of n or partial permutations, denoted as n P r, n P r, P (n,r), or P(n,r) among others. In the case of permutations without replacement, all possible ways that elements in a set can be listed in a particular order are considered, but the number of choices reduces each time an element is.
Question or problem about Python programming:
I know about itertools, but it seems it can only generate permutations without repetitions.
For example, I’d like to generate all possible dice rolls for 2 dice. So I need all permutations of size 2 of [1, 2, 3, 4, 5, 6] including repetitions: (1, 1), (1, 2), (2, 1)… etc
If possible I don’t want to implement this from scratch
How to solve the problem:
Solution 1:
You are looking for the Cartesian Product.
In mathematics, a Cartesian product (or product set) is the direct product of two sets.
In your case, this would be {1, 2, 3, 4, 5, 6}
x {1, 2, 3, 4, 5, 6}
.itertools
can help you there:
To get a random dice roll (in a totally inefficient way):
Solution 2:
You’re not looking for permutations – you want the Cartesian Product. For this use product from itertools:
Solution 3:
In python 2.7 and 3.1 there is a itertools.combinations_with_replacement
function:
Permute 3
Solution 4:
In this case, a list comprehension is not particularly needed.
Given
Code
Details
Unobviously, Cartesian product can generate subsets of permutations. However, it follows that:
- with replacement: produce all permutations nr via
product
- without replacement: filter from the latter
Permutations with replacement, nr
Permutations without replacement, n!
Consequently, all combinatoric functions could be implemented from product
:
combinations_with_replacement
implemented fromproduct
combinations
implemented frompermutations
, which can be implemented withproduct
(see above)
Permute 2.2 For Macos Catalina
Solution 5:
I think I found a solution using only lambdas
, map
and reduce
.
Essentially I’m mapping a first lambda function that given a row, iterates the columnns
then this is used as the output of a new lambda function
which is mapped across all the possible rows
and then we reduce all the resulting lists into one.
even better
Can also use two different numbers.
Solution 6:
First, you’ll want to turn the generator returned by itertools.permutations(list) into a list first. Then secondly, you can use set() to remove duplicates
Something like below: