swap

Swapping rows within the same pandas dataframe

99封情书 提交于 2019-12-01 17:37:08
I'm trying to swap the rows within the same DataFrame in pandas. I've tried running a = pd.DataFrame(data = [[1,2],[3,4]], index=range(2), columns = ['A', 'B']) b, c = a.iloc[0], a.iloc[1] a.iloc[0], a.iloc[1] = c, b but I just end up with both the rows showing the values for the second row (3,4). Even the variables b and c are now both assigned to 3 and 4 even though I did not assign them again. Am I doing something wrong? Use a temporary varaible to store the value using .copy() , because you are changing the values while assigning them on chain i.e. Unless you use copy the data will be

Swapping rows within the same pandas dataframe

青春壹個敷衍的年華 提交于 2019-12-01 16:22:19
问题 I'm trying to swap the rows within the same DataFrame in pandas. I've tried running a = pd.DataFrame(data = [[1,2],[3,4]], index=range(2), columns = ['A', 'B']) b, c = a.iloc[0], a.iloc[1] a.iloc[0], a.iloc[1] = c, b but I just end up with both the rows showing the values for the second row (3,4). Even the variables b and c are now both assigned to 3 and 4 even though I did not assign them again. Am I doing something wrong? 回答1: Use a temporary varaible to store the value using .copy() ,

swap() function for variables's values [duplicate]

巧了我就是萌 提交于 2019-12-01 14:54:40
This question already has an answer here: Is JavaScript a pass-by-reference or pass-by-value language? 29 answers I am not able to achieve the desired result for this swap function below, where I want the values printed as 3,2 function swap(x,y){ var t = x; x = y; y = t; } console.log(swap(2,3)); Any clue will be appreciated ! Your function is swapping the values internally, but the function does not return a value. The following method returns an array of values in reverse order of what was supplied. function swap(x, y) { var t = x; x = y; y = t; return [x, y]; } console.log(swap(2, 3));

swap classes on click

不羁的心 提交于 2019-12-01 12:23:04
I have a list of 6 items that are in a global div (navigationagence) Right now I am able to add a class on click but right now they adds up which means that once my six items are clicked they all end up with the currentagence class. I want to be able to remove the add a class to the clicked item but remove it to the other, How can I achieve that? Here is the jquery code I am using right now. $("#navigationagence ul li").click(function () { $(this).toggleClass("currentagence"); }); You can use the siblings() method to get the other list items and remove the class from them: $("#navigationagence

Swapping two string pointers

我只是一个虾纸丫 提交于 2019-12-01 11:26:15
问题 i have to char[] in C and i wanted to swap between them, by only swaping the pointer to the array and not one char at a time so i wrote this code: #include <stdio.h> void fastSwap (char **i, char **d) { char *t = *d; *d = *i; *i = t; } int main () { char num1[] = "012345678910"; char num2[] = "abcdefghujk"; fastSwap ((char**)&num1,(char**)&num2); printf ("%s\n",num1); printf ("%s\n",num2); return 0; } I get this output (note the last 4 characters) abcdefgh8910 01234567ujk When I expect:

Is it possible to swap the addresses of two variables?

ⅰ亾dé卋堺 提交于 2019-12-01 11:23:36
I know that it is possible to swap the value of two variables like so #include <stdio.h> void swap(int *x, int *y); int main(){ int x=5,y=10; swap(&x, &y); printf("x: %d, y: %d\n", x, y); return 0; } void swap(int *x, int *y){ int temp; temp=*x; *x=*y; *y=temp; } But is it possible to swap the addresses of those two variables? And I don't mean just making a pointer to them and then swapping the values held by the pointers, I mean actually swapping the two, so that after the swap function the address of x is now the address of y before the swap function and vice versa. I apologise if this is a

How to swap two __m128i variables in C++03 given its an opaque type and an array?

99封情书 提交于 2019-12-01 08:35:34
What is the best practice for swapping __m128i variables? The background is a compile error under Sun Studio 12.2 , which is a C++03 compiler. __m128i is an opaque type used with MMX and SSE instructions, and its usually and unsigned long long[2] . C++03 does not provide the support for swapping arrays, and std:swap(__m128i a, __m128i b) fails under the compiler. Here are some related questions that don't quite hit the mark. They don't apply because std::vector is not available. How can we swap 2 arrays in constant complexity or O(1)? Is it possible to swap arrays of structs C++03 moving a

Swapping elements in lists in python

你离开我真会死。 提交于 2019-12-01 07:40:01
I have a list and I need to swap the 1st element in the list with the maximum element in the list. But why does code 1 work while code 2 doesn't: code 1: a = list.index(max(list)) list[0], list[a] = list[a], list[0] code 2: list[0], list[list.index(max(list))] = list[list.index(max(list))], list[0] I thought Python would first evaluate the right-hand side before assigning it to the names on the left? Python stores results in multiple targets from left to right , executing the assignment target expression in that order. So your second version essentially comes down to this: temp = list[list

Pythonic Swap?

走远了吗. 提交于 2019-12-01 03:07:06
I found that i have to perform a swap in python and i write something like this. arr[first], arr[second] = arr[second], arr[first] I suppose this is not so pythonic. Does somebody know how to do a swap in python more elegent? EDIT: I think another example will show my doubts self.memberlist[someindexA], self.memberlist[someindexB] = self.memberlist[someindexB], self.memberlist[someindexA] is this the only available solution for swap in python? I did searched a lot but didn't find a nice answer... Alex Martelli The one thing I might change in your example code: if you're going to use some long

How fast is std::swap for integer types?

北慕城南 提交于 2019-12-01 02:15:45
STL implements a generic std::swap function to swap 2 values. It can be presented in the following way: template <class T> void swap (T& a, T& b) { T c(std::move(a)); a=std::move(b); b=std::move(c); } However, there is a XOR swap algorithm to swap 2 integers ( http://en.wikipedia.org/wiki/XOR_swap_algorithm ): void swap_u( size_t& x, size_t& y ) { x = x^y; y = x^y; x = x^y; } My questions: Is it an optimization nowadays (on x86 or arm )? Does C++ standard favor this kind of optimization? Are there any real STL implementations in the wild that have std::swap specialization for integers? In the