Home > Back-end >  How to use a step outside the range() fonction
How to use a step outside the range() fonction

Time:03-08

Good evening,

I'm working on this code :

Alright = []
for i in range(0, 384, 48):
    Alright.append(i)
Alright2 = []
Alright3 = []
for j in range(len(Alright)):
    if j % 2 == 0:
        Alright2 = (Alright[j], Alright[j 1])
        Alright3.append(Alright2)
print(Alright3)

Out : [(0, 48), (96, 144), (192, 240), (288, 336)]

That's Good i wanted those ranges. Now what i want to do is the same thing but for ranges that are not in the past one (Alright3) and get a list with ranges that are not in Alright3, so the output must be like shown in the next step (Wanted Out). Here is my code :

Alright4 = []
for i in range(49, 384, 46):
    Alright4.append(i)
Alright5 = []
Alright6 = []
for j in range(len(Alright4)):
    if j % 2 == 0:
        Alright5 = (Alright4[j], Alright4[j 1])
        Alright6.append(Alright5)
print(Alright6)
Out : [(49, 95), (141, 187), (233, 279), (325, 371)]

I want to have this out :

Wanted Out : [(49, 95), (145, 191), (241, 287), (337, 384)]

Thank you for your time guys !

CodePudding user response:

I think it would be a little overcomplicated run a new loops. Just try adjusting your first method like this:

Alright = []
for i in range(0, 384, 48):
    Alright.append(i)
Alright2 = []
Alright3 = []
Alright4 = [] #numbers not in Alright3
last = None #saves the higher value of the last added tuple

for j in range(len(Alright)):
    if j % 2 == 0:
        if last != None: #made to skip the first iteration
            Alright4.append((last 1, Alright[j]-1)) #saves the range between the last added tuple and the one being added in this iteration
        Alright2 = (Alright[j], Alright[j 1])
        Alright3.append(Alright2)
        last = Alright[j 1] #saves the higher value of the last added tuple

if last != 384:
    Alright4.append((last 1, 384))

print(Alright4)

The contents of Alright4 will look like this:

Out : [(49, 95), (145, 191), (241, 287), (337, 384)]

CodePudding user response:

Alright4 = []
mul = 4
for i in range(49, 384, 46):
    Alright4.append(i)
print(Alright4)
Alright5 = []
Alright6 = []
for j in range(len(Alright4)):
    if j % 2 == 0:
      if j == 0:
        Alright5 = (Alright4[j], Alright4[j 1])
        Alright6.append(Alright5)
      else:
        if j == len(Alright4)-2:
          mul  = 1
        Alright5 = (Alright4[j] mul, Alright4[j 1] mul)
        Alright6.append(Alright5)
        mul  = 4
print(Alright6)

CodePudding user response:

First, let's refactor your initial code a bit:

Alright = list(range(0, 384, 48))
Alright3 = []
for j in range(0, len(Alright), 2):
    Alright2 = (Alright[j], Alright[j 1])
    Alright3.append(Alright2)

Now I understand what you're trying to do. I would write it as follows:

ranges = [(i, i   48) for i in range(0, 384, 48 * 2)]

So because we're going to skip every other entry anyway, why not make the original range with a step twice as large? We also know the second value of each tuple has to be 48 more than the first value of that tuple.

This has the advantage we can use that same method to find the complement:

other_ranges = [(i, i   46) for i in range(49, 384, 48 * 2)]

Note that the step is the same, but the second element of each tuple is only 46 more than the first.

The only issue is that the last value is 383 rather than 384 as you specified. I believe this is probably going to be the right value, but it's possible to correct afterwards if you really do need 384:

first_value, second_value = other_ranges[-1]
other_ranges[-1] = first_value, second_value   1
  • Related