Python nested for loops

0 votes

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 by writing a horrible code such as:

for i in range(10):
    for j in range(i+1):
        for k in range(j+1):

etc., but with 10-digit numbers my code starts looking horrible, and also that's a lot of writing, and indentation get horrible if I want to commend few of those. Is there a nice, concise way of getting this?

Edit: just so that people know why I'm bothering with this, https://projecteuler.net/problem=74 has me check numbers from 1 to one milion. Unfortunately, It's not as straightforward as I thought -- numbers with leading zeros are treated differently than the ones with zeros inside, so some additional magic had to be performed. Anyway, thanks to all for insightful suggestions.

Sep 3, 2018 in Python by bug_seeker
• 15,510 points
1,009 views

1 answer to this question.

0 votes

Could use itertools:

>>> for comb in itertools.combinations_with_replacement(range(9, -1, -1), 3):
        print comb

(9, 9, 9)
(9, 9, 8)
(9, 9, 7)
(9, 9, 6)
...
(4, 0, 0)
(3, 3, 3)
(3, 3, 2)
(3, 3, 1)
(3, 3, 0)
(3, 2, 2)
(3, 2, 1)
(3, 2, 0)
(3, 1, 1)
(3, 1, 0)
(3, 0, 0)
(2, 2, 2)
(2, 2, 1)
(2, 2, 0)
(2, 1, 1)
(2, 1, 0)
(2, 0, 0)
(1, 1, 1)
(1, 1, 0)
(1, 0, 0)
(0, 0, 0)

Or recursively, appending more and more digits until enough, which can more directly produce intobjects instead of digit tuples (not sure whether that's what you actually need):

def build(enough, prefix=0):
    if prefix >= enough:
        print(prefix)
        return
    for digit in range(prefix % 10 + 1) if prefix else range(1, 10):
        build(enough, prefix * 10 + digit)

Demo (note it leaves out "000", not sure whether you'd want that anyway):

>>> n = 3
>>> build(10**(n-1))
100
110
111
200
210
211
220
221
222
300
310
311
320
321
322
330
331
332
333
400
410
411
420
answered Sep 3, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
2 answers

Avoiding multiple nested for-loops in python

Instead of multi-loop, If you can categorize ...READ MORE

answered Sep 15, 2018 in Python by pedro67657
• 160 points

edited Sep 15, 2018 by Vardhan 16,263 views
+1 vote
1 answer

instead of using two for loops in python

This example might help: for x, y in ((a,b) ...READ MORE

answered Dec 24, 2019 in Python by Sumeir
1,248 views
0 votes
1 answer

Unique identification for data items in Python

Try the UUID module of Python. For example, ...READ MORE

answered Apr 17, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,150 views
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
3,931 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Avoiding multiple nested for-loops in python

You can replace the three loops with: from ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,020 points
894 views
0 votes
1 answer

Avoiding multiple nested for-loops in python

You can replace the three loops with: from ...READ MORE

answered Sep 10, 2018 in Python by Priyaj
• 58,020 points
3,236 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP