¿Rango fuera de orden de Python?

En lugar de hacer

for i in range(begin, end):

Quiero hacer algo como

for i in begin+1, begin+2, begin, begin+3 through end:

If this makes sense at all?

preguntado el 15 de mayo de 13 a las 04:05

What do you mean? Is there a specific pattern? -

no pattern, just the way I have it outlined above -

2 Respuestas

for i in [begin+1, begin+2, begin] + range(begin+3, end):

contestado el 15 de mayo de 13 a las 04:05

Not random -- the specific order outlined in the OP - John Smith

@JohnSmith, ok, something like this then? - Juan La Rooy

John, can you explain yourself better? You've got a few people asking for clarification, so merely repeating that you meant what you said initially isn't sufficient. Please give us a sample sequence so we can help you. - Jimothy

@Jimothy The pattern is explicitly stated in the OP - John Smith

John, I'm not the only one having trouble understanding your problem. But since all you're doing is repeating yet again "that pattern is in the OP," I guess you're not really interested in getting help. - Jimothy

Frankly I think this is a job for itertools.chain.

from itertools import chain
def disorderedrange(begin, end):
    return chain((begin+1, begin+2, begin, begin+3), range(begin+4, end))

for i in disorderedrange(1, 100):
    # loop content goes here.

This is better than gnibbler's solution if the range is large (as this avoids creating a temporary list holding the entire range.).

contestado el 15 de mayo de 13 a las 04:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.