Swap Two Items in List<T>

Swap two items in ListT

Check the answer from Marc from C#: Good/best implementation of Swap method.

public static void Swap<T>(IList<T> list, int indexA, int indexB)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
}

which can be linq-i-fied like

public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
return list;
}

var lst = new List<int>() { 8, 3, 2, 4 };
lst = lst.Swap(1, 2);

How do I swap 2 elements in a list

To swap the values of two variables, the easiest method is using references. This is a classic pointer exercise in c++, but it can apply to C# as well.

// Replace int with any data type / class you need
void Swap (ref int a, ref int b)
{
int c = a;
a = b;
b = c;
}

The algorithm used is very simple, and the explanation is usually done like this: you have two glasses, one with water, and one with oil. To put the oil in the first glass, you will need to use a third glass, put the water inside, then put the oil in the first glass, and the water in the second one.


Here is what I had in mind. Look for the comments, so you can understand what's going on.:

// Unlike foreach, with for I can change the values in the list
for (int i = 0; i < inventory.Count; i++)
{
if (mouseRectangle.Intersects(inventory[i].ItemRectangle))
{
if (Input.EdgeDetectLeftMouseDown())
{
// You can replace the switch with this shorter structure
// if A is a bool value, !A will have the opposite value
inventory[i].ItemSelected = !inventory[i].ItemSelected;
}
}
else if (Input.EdgeDetectLeftMouseDown())
{
// You don't need a case-switch for a single condition. An if should suffice
if (inventory[i].ItemSelected)
inventory[i].ItemSelected = false;
}
else if (inventory[i].ItemSelected == true)
{
inventory[i].ItemPosition = new Vector2(mouseRectangle.X, mouseRectangle.Y);
inventory[i].ItemRectangle = new Rectangle(mouseRectangle.X, mouseRectangle.Y, 32, 32);
}
else if (inventory[i].ItemSelected == false && //a lot of checks to determine it is not intersecting with an equip slot
{
inventory[i].ItemPosition = inventory[i].OriginItemPosition;
inventory[i].ItemRectangle = inventory[i].OriginItemRectangle;
}

// Something definitely wrong with this line, a rectangle to instersect with itself??
else if (inventory[i].ItemRectangle.Intersects(inventory[PROBABLY_SOMETHING_ELSE].ItemRectangle))
{
Swap (ref inventory[i], ref inventory[PROBABLY_SOMETHING_ELSE])
}
}

The swapping of two elements in a list doesn't work

Since you are swapping (modifying the contents of list) you need to use indices in inner loop as well.

def sortingStoL(lst):
for i in range(len(lst)):
for j in range(i + 1,len(lst)):
if lst[j] < lst[i]:
print(lst[j], lst[i])
lst[i], lst[j] = lst[j], lst[i]
print(lst)
return lst

a_list = [5, 4, 3, 2]
print(sortingStoL(a_list))

Swapping two elements in a list covariantly

Does this work for you?

public static void Swap<T>(this List<T> list, int pos1, int pos2)
{
T tmp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = tmp;

}

This allows you to specify the type and make the swap possible.

Why can't I swap two items in a list in one line?

The reason why the first example is not working is because you are calling .index() multiple times, and after each time, the values in the list are changing, so the indices found in the code are not representative of the actual locations of the elements. The second example works because you have stored the first indices in two variables, and use both in the swap.

Overview of first example:

lol[lol.index("test")], lol[lol.index("test2")] = lol[lol.index("test2")], lol[lol.index("test")] 

First part: lol[lol.index("test")] stores 0

Second part: lol[lol.index("test2")] stores 1

Third part: lol[lol.index("test2")] still stores 1

This is when it gets interesting. The forth part of the example, lol[lol.index("test")], finds the index of test, however, test was assigned 1 from the third segment of the code. Therefore, lol[lol.index("test")] is 1, not 0. Consequently, lol[lol.index("test2")] still stores 1.

swapping two elements between two lists in python

This does not come from your swap function that works with regular arrays It has to do with the fact that you are using numpy.array.

Here is a similar question I found.

You need to do TEMP_B = np.copy(B[j]) since Numpy copies data lazily.



Related Topics



Leave a reply



Submit