Questions tagged [reverse]
Rearranging the order of a sequence such that the final order is a mirror image of the original.
reverse
3,810
questions
1231
votes
10
answers
964k
views
How do I sort a list of objects based on an attribute of the objects?
I have a list of Python objects that I want to sort by a specific attribute of each object:
[Tag(name="toe", count=10), Tag(name="leg", count=2), ...]
How do I sort the list by ....
1074
votes
28
answers
1.3m
views
Traverse a list in reverse order in Python [duplicate]
How do I traverse a list in reverse order in Python? So I can start from collection[len(collection)-1] and end in collection[0].
I also want to be able to access the loop index.
1055
votes
33
answers
846k
views
Reverse / invert a dictionary mapping
Given a dictionary like so:
my_map = {'a': 1, 'b': 2}
How can one invert this map to get:
inv_map = {1: 'a', 2: 'b'}
592
votes
56
answers
757k
views
How do you reverse a string in-place?
How do you reverse a string in-place in JavaScript when it is passed to a function with a return statement, without using built-in functions (.reverse(), .charAt() etc.)?
521
votes
20
answers
710k
views
Print a list in reverse order with range()?
How can you produce the following list with range() in Python?
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
478
votes
12
answers
232k
views
JQuery .each() backwards
I'm using JQuery to select some elements on a page and then move them around in the DOM. The problem I'm having is I need to select all the elements in the reverse order that JQuery naturally wants to ...
464
votes
7
answers
859k
views
Python list sort in descending order
How can I sort this list in descending order?
timestamps = [
"2010-04-20 10:07:30",
"2010-04-20 10:07:38",
"2010-04-20 10:07:52",
"2010-04-20 10:08:...
254
votes
7
answers
433k
views
Right way to reverse a pandas DataFrame?
Here is my code:
import pandas as pd
data = pd.DataFrame({'Odd':[1,3,5,6,7,9], 'Even':[0,2,4,6,8,10]})
for i in reversed(data):
print(data['Odd'], data['Even'])
When I run this code, i get the ...
247
votes
12
answers
330k
views
How to get a reversed list view on a list in Java?
I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this functionality?
I don't want to make ...
231
votes
17
answers
190k
views
angular ng-repeat in reverse
How can i get a reversed array in angular?
i'm trying to use orderBy filter, but it needs a predicate(e.g. 'name') to sort:
<tr ng-repeat="friend in friends | orderBy:'name':true">
<td&...
197
votes
21
answers
343k
views
How do you reverse a string in place in C or C++?
How do you reverse a string in C or C++ without requiring a separate buffer to hold the reversed string?
175
votes
21
answers
223k
views
How to read a file in reverse order?
How to read a file in reverse order using python? I want to read a file from last line to first line.
173
votes
16
answers
164k
views
Can one do a for each loop in java in reverse order?
I need to run through a List in reverse order using Java.
So where this does it forwards:
for(String string: stringList){
//...do something
}
Is there some way to iterate the stringList in reverse ...
156
votes
37
answers
148k
views
How to reverse a string in Go?
How can we reverse a simple string in Go?
155
votes
19
answers
161k
views
How do I exchange keys with values in a dictionary? [duplicate]
I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input's values and whose value will be the corresponding input keys. Values are unique.
For example, ...
108
votes
8
answers
120k
views
c# Trying to reverse a list
I have the following code:
public class CategoryNavItem
{
public int ID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public CategoryNavItem(int ...
105
votes
3
answers
90k
views
Best way to create a "reversed" list in Python? [duplicate]
In Python, what is the best way to create a new list whose items are the same as those of some other list, but in reverse order? (I don't want to modify the existing list in place.)
Here is one ...
89
votes
4
answers
49k
views
How do i pass GET parameters using django urlresolvers reverse
I am using django 1.2 and going from one view to another using the urlresolvers reverse method.
url = reverse(viewOne)
and I want to pass a get parameter, for example
name = 'joe'
so that in ...
87
votes
2
answers
99k
views
How to reverse order a vector?
Suppose I have a vector v, how do I get its reverse, i.e. last element first?
The first thing that comes to me is v[length(v):1], but it returns NA when v is numeric(0), while user normally expect ...
86
votes
6
answers
49k
views
Including a querystring in a django.core.urlresolvers reverse() call
I'm trying to reverse a named URL and include a querystring in it. Basically, I've modified the login function, and I want to send ?next= in it.
Here's what I'm doing now: reverse(name) + "?next=" + ...
81
votes
14
answers
577k
views
Printing an array in C++?
Is there a way of printing arrays in C++?
I'm trying to make a function that reverses a user-input array and then prints it out. I tried Googling this problem and it seemed like C++ can't print ...
73
votes
12
answers
154k
views
Best way to loop over a python string backwards
What is the best way to loop over a python string backwards?
The following seems a little awkward for all the need of -1 offset:
string = "trick or treat"
for i in range(len(string)-1, 0-1, -1):
...
71
votes
6
answers
51k
views
How to reverse String.fromCharCode?
String.fromCharCode(72) gives H. How to get number 72 from char H?
68
votes
2
answers
225k
views
How to reverse an std::string? [duplicate]
Im trying to figure out how to reverse the string temp when I have the string read in binary numbers
istream& operator >>(istream& dat1d, binary& b1)
{
string ...
67
votes
7
answers
65k
views
Linux: Find all symlinks of a given 'original' file? (reverse 'readlink')
Consider the following command line snippet:
$ cd /tmp/
$ mkdir dirA
$ mkdir dirB
$ echo "the contents of the 'original' file" > orig.file
$ ls -la orig.file
-rw-r--r-- 1 $USER $USER 36 ...
67
votes
3
answers
119k
views
Apache reverse proxy with basic authentication
Trying to configure my reverse proxy with basic authentication before forward the traffic to my back end server. Can any one give me a solution.
Example here:
User(internet) -> reverse proxy / ...
65
votes
3
answers
161k
views
How to unzip, edit and zip an android apk
I have an android apk and I deleted my source code and dont have the project again, I want to change the version code of the old apk. my question is how do I unzip and repack the apk so I can use the. ...
63
votes
3
answers
40k
views
How can I capture which direction is being panned using UIPanGestureRecognizer?
Ok so I have been looking around at just about every option under the sun for capturing multi-touch gestures, and I have finally come full circle and am back at the UIPanGestureRecognizer.
The ...
62
votes
5
answers
54k
views
android - reverse the order of an array
I have an array of objects.
Is it possible to make a new array that is a copy of this array, but in reverse order?
I was looking for something like this.
// my array
ArrayList<Element> ...
62
votes
5
answers
54k
views
Reverse the order of a legend
I use the following code to plot the bar graph and need to present a legend in reverse order. How can I do it?
colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2)))
p = numpy.empty(len(C2), dtype=...
60
votes
11
answers
147k
views
How to reverse order of keys in python dict?
This is my code :
a = {0:'000000',1:'11111',3:'333333',4:'444444'}
for i in a:
print i
it shows:
0
1
3
4
but I want it to show:
4
3
1
0
so, what can I do?
55
votes
5
answers
72k
views
Reverse Sorted Dictionary in .NET
Is there any way I can iterate backwards (in reverse) through a SortedDictionary in c#?
Or is there a way to define the SortedDictionary in descending order to begin with?
54
votes
3
answers
126k
views
Python Reverse Find in String
I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index.
An example: I want to find the index of the 2nd I by using the index and str....
51
votes
15
answers
208k
views
Reversing an Array in Java [duplicate]
If I have an array like this:
1 4 9 16 9 7 4 9 11
What is the best way to reverse the array so that it looks like this:
11 9 4 7 9 16 9 4 1
I have the code below, but I feel it is a little ...
50
votes
11
answers
98k
views
How to replace some characters from the end of a string?
I want to replace characters at the end of a python string. I have this string:
s = "123123"
I want to replace the last 2 with x. Suppose there is a method called replace_last:
>>&...
49
votes
18
answers
149k
views
Reversing a String with Recursion in Java
Here is some Java code to reverse a string recursively.
Could someone provide an explanation of how it works?
public static String reverse(String str) {
if ((null == str) || (str.length() <=...
48
votes
5
answers
53k
views
LinearLayoutManager setReverseLayout() == true but items stack from bottom
This seems like it would be an easy solution, but it seems that setting
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mLayoutManager;
.... // ...
48
votes
5
answers
74k
views
Can I loop through a javascript object in reverse order?
So I have a JavaScript object like this:
foo = {
"one": "some",
"two": "thing",
"three": "else"
};
I can loop this like:
for (var i in foo) {
if (foo.hasOwnProperty(i)) {
// do ...
46
votes
7
answers
17k
views
Rails: Is it bad to have an irreversible migration?
When is it acceptable to raise an ActiveRecord::IrreversibleMigration exception in the self.down method of a migration? When should you take the effort to actually implement the reverse of the ...
46
votes
2
answers
84k
views
Reverse string: string[::-1] works, but string[0::-1] and others don't
I am somewhat of a python/programming newbie, and I have just been playing around with string slicing. So the simple string reverse method of string[::-1] works just fine as we know, but there are ...
46
votes
6
answers
100k
views
Reverse Modulus Operator
Over 3 years after asking the question I found the solution. I have included it as an answer.
I have an expression with modulus in it that needs to be put in terms of x.
(a + x) mod m = b
I can't ...
44
votes
11
answers
49k
views
How do I reverse a String in Dart?
I have a String, and I would like to reverse it. For example, I am writing an AngularDart filter that reverses a string. It's just for demonstration purposes, but it made me wonder how I would reverse ...
43
votes
12
answers
97k
views
Translate integer to letters and vice versa (e.g. 0 = "A", 26 = "AA", 27 = "AB")
So I have this function:
function toAlpha($data){
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$alpha_flip = ...
43
votes
34
answers
192k
views
Java reverse an int value without using array
Can anyone explain to me how to reverse an integer without using array or String. I got this code from online, but not really understand why + input % 10 and divide again.
while (input != 0) {
...
42
votes
43
answers
182k
views
Reverse a string without using reversed() or [::-1]?
I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use the reversed method or the ...
42
votes
14
answers
25k
views
Reverse a string in Python two characters at a time (Network byte order)
Say you have this string:
ABCDEFGH
And you want to reverse it so that it becomes:
GHEFCDAB
What would be the most efficient / pythonic solution? I've tried a few different things but they all ...
42
votes
5
answers
143k
views
Python: How exactly can you take a string, split it, reverse it and join it back together again? [duplicate]
How exactly can you take a string, split it, reverse it and join it back together again without the brackets, commas, etc. using python?
42
votes
3
answers
59k
views
Sort a list of tuples by second value, reverse=True and then by key, reverse=False
I need to sort a dictionary by first, values with reverse=True, and for repeating values, sort by keys, reverse=False
So far, I have this
dict = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
...
42
votes
1
answer
2k
views
Reverse iterator returns garbage when optimized
I have a AsIterator template class which takes a numeric-like type, in this example just an int, and converts it into an iterator (++ and -- increment and decrement the number, and operator* just ...
41
votes
6
answers
46k
views
How do I construct a Django reverse/url using query args?
I have URLs like http://example.com/depict?smiles=CO&width=200&height=200 (and with several other optional arguments)
My urls.py contains:
urlpatterns = patterns('',
(r'^$', 'cansmi....