Home  >  Article  >  Backend Development  >  Example of implementation of permutation and combination calculation operations in Python

Example of implementation of permutation and combination calculation operations in Python

黄舟
黄舟Original
2017-10-14 10:24:213552browse

This article mainly introduces the permutation and combination calculation operations implemented in Python, involving related functions and usage skills of Python mathematical operations. Friends in need can refer to the following

The examples in this article describe the permutation and combination calculations implemented in Python operate. Share it with everyone for your reference, the details are as follows:

1. Call scipy to calculate the specific values ​​of the permutation and combination


>> from scipy.special import comb, perm
>> perm(3, 2)
6.0
>> comb(3, 2)
3.0

2. Call itertools to obtain the number of all permutations and combinations


>> from itertools import combinations, permutations
>> permutations([1, 2, 3], 2)
<itertools.permutations at 0x7febfd880fc0>
        # 可迭代对象
>> list(permutations([1, 2, 3], 2))
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
>> list(combinations([1, 2, 3], 2))
[(1, 2), (1, 3), (2, 3)]

The above is the detailed content of Example of implementation of permutation and combination calculation operations in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn