Python: nested 'for' loops

前端 未结 5 1253
天涯浪人
天涯浪人 2021-02-07 09:47

I\'d like to go through all n-digit numbers such that second digit of the number is always lower or equal to the first, third is lower or equal to the second etc. I can get this

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-07 10:20

    this an approach using itertools:

    from itertools import combinations_with_replacement
    
    N = 3
    
    for kji in combinations_with_replacement((str(i) for i in range(10)), N):
        print(''.join(reversed(kji)))
    

    note that the order is not the same as in your original approach.

    i recently had a simliar question...

提交回复
热议问题