Use reversed() function (efficient since range implements __reversed__):
reversed(range(10))
It's much more meaningful.
Update: list cast
If you want it to be a list (as @btk pointed out):
list(reversed(range(10)))
Update: range-only solution
If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)
For example, to generate a list [3, 2, 1, 0], you can use the following:
range(3, -1, -1)
It may be less intuitive, but it works the same with less text. This answer by @Wolf indicates this approach is slightly faster than reversed.
Use reversed() function (efficient since range implements __reversed__):
reversed(range(10))
It's much more meaningful.
Update: list cast
If you want it to be a list (as @btk pointed out):
list(reversed(range(10)))
Update: range-only solution
If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)
For example, to generate a list [3, 2, 1, 0], you can use the following:
range(3, -1, -1)
It may be less intuitive, but it works the same with less text. This answer by @Wolf indicates this approach is slightly faster than reversed.
Use the 'range' built-in function. The signature is range(start, stop, step). This produces a sequence that yields numbers, starting with start, and ending if stop has been reached, excluding stop.
>>> range(9,-1,-1)
range(9, -1, -1)
>>> list(range(9,-1,-1))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> list(range(-2, 6, 2))
[-2, 0, 2, 4]
The list constructor converts range (which is a python generator), into a list.
Reversing a range using a FOR LOOP
Reverse Range in Swift - Stack Overflow
linear algebra - Reverse range of numbers, scaling - Mathematics Stack Exchange
How do I reverse a range?
Are there any performance considerations when using reverse range?
Can I combine reverse range with other Python functions or libraries?
Can I use reverse range with non-numeric values, such as strings or objects?
Videos
I'm doing self-study program in Python and I'm being challenged to create a function that act exactly like the RANGE function, but it reverse the range. Let's call it reverserange().
reverserange(0,5) will return tuple (4, 3, 2, 1, 0)
I can't for the life of me figure it out. I've been trying for a couple hours. I know it requires a for loop...
Can anyone assist?
Update For latest Swift 3 (still works in Swift 4)
You can use the reversed() method on a range
for i in (1...5).reversed() { print(i) } // 5 4 3 2 1
Or stride(from:through:by:) method
for i in stride(from:5,through:1,by:-1) { print(i) } // 5 4 3 2 1
stride(from:to:by:) is similar but excludes the last value
for i in stride(from:5,to:0,by:-1) { print(i) } // 5 4 3 2 1
Update For latest Swift 2
First of all, protocol extensions change how reverse is used:
for i in (1...5).reverse() { print(i) } // 5 4 3 2 1
Stride has been reworked in Xcode 7 Beta 6. The new usage is:
for i in 0.stride(to: -8, by: -2) { print(i) } // 0 -2 -4 -6
for i in 0.stride(through: -8, by: -2) { print(i) } // 0 -2 -4 -6 -8
It also works for Doubles:
for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }
Be wary of floating point compares here for the bounds.
Earlier edit for Swift 1.2: As of Xcode 6 Beta 4, by and ReverseRange don't exist anymore :[
If you are just looking to reverse a range, the reverse function is all you need:
for i in reverse(1...5) { println(i) } // prints 5,4,3,2,1
As posted by 0x7fffffff there is a new stride construct which can be used to iterate and increment by arbitrary integers. Apple also stated that floating point support is coming.
Sourced from his answer:
for x in stride(from: 0, through: -8, by: -2) {
println(x) // 0, -2, -4, -6, -8
}
for x in stride(from: 6, to: -2, by: -4) {
println(x) // 6, 2
}
There's something troubling about the asymmetry of this:
for i in (1..<5).reverse()
...as opposed to this:
for i in 1..<5 {
It means that every time I want to do a reverse range, I have to remember to put the parentheses, plus I have to write that .reverse() on the end, sticking out like a sore thumb. This is really ugly in comparison to C-style for loops, which are symmetrical counting up and counting down. So I tended to use C-style for loops instead. But in Swift 2.2, C-style for loops are going away! So I've had to scurry around replacing all my decrementing C-style for loops with this ugly .reverse() construct — wondering all the while, why on earth isn't there a reverse-range operator?
But wait! This is Swift — we're allowed to define our own operators!! Here we go:
infix operator >>> {
associativity none
precedence 135
}
func >>> <Pos : ForwardIndexType where Pos : Comparable>(end:Pos, start:Pos)
-> ReverseRandomAccessCollection<(Range<Pos>)> {
return (start..<end).reverse()
}
So now I'm allowed to say:
for i in 5>>>1 {print(i)} // 4, 3, 2, 1
This covers just the most common case that occurs in my code, but it is far and away the most common case, so it's all I need at present.
I had a kind of internal crisis coming up with the operator. I would have liked to use >.., as being the reverse of ..<, but that's not legal: you can't use a dot after a non-dot, it appears. I considered ..> but decided it was too hard to distinguish from ..<. The nice thing about >>> is that it screams at you: "down to!" (Of course you're free to come up with another operator. But my advice is: for super symmetry, define <<< to do what ..< does, and now you've got <<< and >>> which are symmetrical and easy to type.)
Swift 3 version (Xcode 8 seed 6):
infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound) ->
ReversedRandomAccessCollection<CountableRange<Bound>>
where Bound : Comparable, Bound.Stride : Integer {
return (minimum..<maximum).reversed()
}
Swift 4 version (Xcode 9 beta 3):
infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
-> ReversedRandomAccessCollection<CountableRange<Bound>>
where Bound : Comparable & Strideable {
return (minimum..<maximum).reversed()
}
Swift 4.2 version (Xcode 10 beta 1):
infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
-> ReversedRandomAccessCollection<Range<Bound>>
where Bound : Strideable {
return (minimum..<maximum).reversed()
}
Swift 5 version (Xcode 10.2.1):
infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
-> ReversedCollection<Range<Bound>>
where Bound : Strideable {
return (minimum..<maximum).reversed()
}
Note, that if you pick $x$ and $y$ is its reverse, then $x+y=2$ should hold. So, you can calculate the reverse by $$f(x)=2-x$$
Let the vector storing the numbers be $v$. Then $v(1)=0.5$ and $v(n)=1.25$ where $n\times 1$ is the size of your vector. Then form a new vector $u$ of size $n\times 1$ such that $$u(i)=v(n+1-i)$$ then $u$ contains the numbers in reversed order.