当前位置:网站首页>Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?

Example 001: the number combination has four numbers: 1, 2, 3, 4. How many three digits can be formed that are different from each other and have no duplicate numbers? How many are each?

2022-07-05 08:21:00 Lazy smile

#  example 001: Number combination 
#  There are four numbers :1、2、3、4, How many different and unrepeated three digit numbers can be formed ? How many are each ?

   Environmental Science :Pycharm2022 + Anaconda3

  First use mathematical permutation and combination analysis, and then Python Realization And summarize the methods

Catalog

         Analyze with mathematical permutation and combination

        Python Realization


         Analyze with mathematical permutation and combination

        The first thing I think of is arranging the array composite , Because the title requires no repeated numbers , Therefore, it is the number of permutations . Definition of permutation : from n Among the different elements , Take whatever you like m(m≤n,m And n All are natural numbers , The same below ) Different elements are arranged in a certain order , It's called from n Take out... Of the different elements m An arrangement of elements ; from n Take out... Of the different elements m(m≤n) The number of all permutations of elements , It's called from n Take out... Of the different elements m Number of permutations of elements .

                                       

          We can conclude that the permutation of this question is 4! / (4-3)! = 4! = 24 ( individual )

        Python Realization

         Method 1 : We can loop through 3 A digital , Because it is required not to repeat numbers , Therefore, make conditional judgment when outputting , Only output hundreds of digits, tens of digits, and numbers with different single digits .

arrange = 0  #  Define the number of permutations 
nums = range(1, 5)

#  Method 1 
for i in nums:  #  Hundreds of digits 
    for j in nums:  #  Ten digits 
        for k in nums:  #  Single digit 
            if ((i != j) and (j != k) and (k != i)):  #  Only output numbers without repetition 
                print(i, j, k)
                arrange += 1
print(arrange)

The operation results are as follows : 

1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
24  individual 

Process finished with exit code 0

        Method 2 : Can be called directly itertools Of permutations() class ,# itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator

import itertools  # itertools, yes python A built-in module for , Powerful , Mainly used for efficient loop creation iterators . Be careful. , He did not return list, It is iterator

arrange = 0  #  Define the number of permutations 
nums = range(1, 5)
for i in itertools.permutations(nums, 3):  # ( Iterating elements , Iteration sequence length )
    print(i)
    arrange += 1
print(arrange)

  The operation results are as follows :

(1, 2, 3)
(1, 2, 4)
(1, 3, 2)
(1, 3, 4)
(1, 4, 2)
(1, 4, 3)
(2, 1, 3)
(2, 1, 4)
(2, 3, 1)
(2, 3, 4)
(2, 4, 1)
(2, 4, 3)
(3, 1, 2)
(3, 1, 4)
(3, 2, 1)
(3, 2, 4)
(3, 4, 1)
(3, 4, 2)
(4, 1, 2)
(4, 1, 3)
(4, 2, 1)
(4, 2, 3)
(4, 3, 1)
(4, 3, 2)
24

The second method is used itertools.permutations class , Such contents are as follows: :

class permutations(object):
    """
    Return successive r-length permutations of elements in the iterable.
    
    permutations(range(3), 2) --> (0,1), (0,2), (1,0), (1,2), (2,0), (2,1)
    """
    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __init__(self, range, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __next__(self, *args, **kwargs): # real signature unknown
        """ Implement next(self). """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __setstate__(self, *args, **kwargs): # real signature unknown
        """ Set state information for unpickling. """
        pass

    def __sizeof__(self, *args, **kwargs): # real signature unknown
        """ Returns size in memory, in bytes. """
        pass

permutations Class to create an iterator , return iterable All lengths in are r Project sequence , If you omit r, Then the length of the sequence is the same as iterable The number of items in is the same , A full permutation result will be returned .  

原网站

版权声明
本文为[Lazy smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050816110102.html