Skip to main content


The Sweet Jesus Code in Python ;)

The Sweet Jesus Code in Python

I challenged myself and got stuck solving it.

The kind of result I am looking for is d1 should repeat of 3 times and d2 should give result in list but in sequence without repeat.

something like....

d1 = [1,2,3,4]
d2 = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000]

Desired Output:-

1 [10, 20, 30, 40, 50]

1 [60, 70, 80, 90, 100]

1 [200, 300, 400, 500, 600]

2 [700, 800, 900, 1000]

but my code ...

>>> d1 = [1,2,3,4]
>>> d2 = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000]
>>> 
>>> how_many_times = 3
>>> no_of_list = 5
>>> 
>>> for i in d1:
	for x in range(0,how_many_times):
		print(i,d2[0+x:no_of_list+x])

		
1 [10, 20, 30, 40, 50]
1 [20, 30, 40, 50, 60]
1 [30, 40, 50, 60, 70]
2 [10, 20, 30, 40, 50]
2 [20, 30, 40, 50, 60]
2 [30, 40, 50, 60, 70]
3 [10, 20, 30, 40, 50]
3 [20, 30, 40, 50, 60]
3 [30, 40, 50, 60, 70]
4 [10, 20, 30, 40, 50]
4 [20, 30, 40, 50, 60]
4 [30, 40, 50, 60, 70]
>>> 

But this was not what I wanted.

I got little help from a reddit user name mattwandcow who guided me to the right direction.

And he says....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So it looks like you want to print d2 by dividing it into a number of specified sized chunks, with an number in front of it that increments after being used a specified number of times, correct?

The rest of this is based on the assumption you just answered yes.

There are 2 flaws I can see with your code, both revolving around the for loops. at the end of the for loops, the variables in them are reset, meaning when you're iterating through d2 to print, each loop it goes back to the start with the next row being back at 0, off set by X

that X offset, by the way, is set back to 0 when the outer loop swings back around. The way the outer loop is set up, you're going to end up with a number of lines = d1*how_many_times, no matter how long d2 actually is.

I think nested for loops aren't what you need here. You don't want to loop through d2 more than once. I'd use a while loop. my code would look something like this.

d1_counter=0
d1_times=1
d2_counter=0
while d2_counter<len(d2):
    print(d1[d1_counter],d2[d2_counter:d2_counter + no_of_list])
    d2_counter=d2_counter+no_of_list
    d1_times=d1_times+1
    if d1_times==how_many_times:
        d1_times=1
        d1_counter=d1_counter+1

(I think that would do it? away from a compiler atm, so can't check how well it functions. )

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So finally I did a little correction and it worked like a charm.

And I call this code "The Sweet Jesus Code"

Here it is...



import time

d1 = [1,2,3,4]
d2 = [10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000]

how_many_times = 3
no_of_list = 5
time_interval = 2

d1_counter=0
d1_times=1

d2_counter=0

try:
     while d2_counter˂len(d2):
          print(d1[d1_counter],d2[d2_counter:d2_counter + no_of_list])
          time.sleep(time_interval)
          d2_counter=d2_counter+no_of_list
          d1_times=d1_times+1
          if d1_times==how_many_times+1:
               d1_times=1
               d1_counter=d1_counter+1
               
except:
     print()



˃˃˃ #The Output is...
1 [10, 20, 30, 40, 50]
1 [60, 70, 80, 90, 100]
1 [200, 300, 400, 500, 600]
2 [700, 800, 900, 1000, 2000]
2 [3000, 4000, 5000, 6000, 7000]
2 [8000, 9000, 10000]






🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍🐍

Popular Posts

Python underline string, Python underline text, Underline python print

Python pip - Installing modules from IDLE (Pyton GUI) for python 3.7

Top 40 Python - String Processing in Python | Working with String in Python

Top 11 Essential Python Tips and Tricks

Python Program - When was I born? / Date of Birth / MY BIRTHDAY (using Python3+)





Subscribe to our Channel


Follow us on Facebook Page

Join our python facebook groups



Join us on Telegram