Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to swap two elements of a list
Replies: 7   Last Post: Aug 7, 2012 3:03 AM

 Messages: [ Previous | Next ]
 Ulrich Arndt Posts: 53 Registered: 10/1/09
Re: How to swap two elements of a list
Posted: Aug 6, 2012 4:46 AM

How I understand your problem I would think the following would do:

l1 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131,
137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271,
277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353,
359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677,
683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
773, 787, 797, 809, 811, 821};
l2 = {4, 8, 9, 16, 25, 27, 32, 49, 64, 81, 121, 125, 128, 169, 243,
256, 289, 343, 361, 512, 529, 625, 729, 841, 961, 1024, 1331, 1369,
1681, 1849, 2048, 2187, 2197, 2209, 2401, 2809, 3125, 3481, 3721,
4096, 4489, 4913, 5041, 5329, 6241, 6561, 6859, 6889, 7921, 8192,
9409, 10201, 10609, 11449, 11881, 12167, 12769, 14641, 15625, 16129,
16384, 16807, 17161, 18769, 19321, 19683, 22201, 22801, 24389,
24649, 26569, 27889, 28561, 29791, 29929, 32041, 32761, 32768,
36481, 37249, 38809, 39601, 44521, 49729, 50653, 51529, 52441,
54289, 57121, 58081, 59049, 63001, 65536, 66049, 68921, 69169,
72361, 73441, 76729, 78125, 78961, 79507, 80089, 83521, 85849,
94249, 96721, 97969, 100489, 103823, 109561, 113569, 117649, 120409,
121801, 124609, 128881, 130321, 131072, 134689, 139129, 143641,
146689, 148877, 151321, 157609, 160801, 161051, 167281, 175561,
177147, 177241, 185761, 187489, 192721, 196249, 201601, 205379,
208849, 212521, 214369, 218089};
master = {2, 3, 4, 5, 7, 8, 9, 11, 13, 16, 17, 19, 23, 25, 27, 29, 31,
32, 37, 41, 43, 47, 49, 53, 59, 61, 64, 67, 71, 73, 79, 81, 83,
89, 97, 101, 103, 107, 109, 113, 121, 125, 127, 128, 131, 137, 139,
149, 151, 157, 163, 167, 169, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 243, 251, 256, 257, 263, 269,
271, 277, 281, 283, 289, 293, 307, 311, 313, 317, 331, 337, 343,
347, 349, 353, 359, 361, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487,
491, 499, 503, 509, 512, 521, 523, 529, 541, 547, 557, 563, 569,
571, 577, 587, 593, 599};

final = master /.Apply[Rule, Transpose[{Join[l1, l2], Join[l2, l1]}], {1}];

This will not match your FINAL LIST but this is longer then the master list.

final[[#]] == finalTest[[#]] & /@ Range@Length[final] gives true for all member of final...

Ulrich

Am 05.08.2012 um 20:58 schrieb Diana Mecum:

> ***,
>
> Thank you for getting back to me. I had a little difficulty describing what I was trying to do.
>
> I start with a list which begins as follows:
>
> {2,3,4,5,7,8,9,11,13,16,17,19,23,25,27,29,31
>
> Now, I want to swap the 2 with the 4, the 3 with the 8, the 5 with the 9. "2" is the first element of SWAP LIST 1. "4", which I want to swap with "2" is the first element of SWAP LIST 2. "3" is the second element of SWAP LIST 1. "8", which I want to swap with "3" is the second element of SWAP LIST 2. And, so on.
>
> The resulting list is
>
> {4,8,2,9,16,3,5,25,27,7,32,49,64,11,13,81,121
>
> Hope this is more clear.
>
> Diana
>

Date Subject Author
8/4/12 Diana
8/5/12 Diana
8/5/12 Diana
8/5/12 Nasser Abbasi
8/6/12 Ulrich Arndt
8/6/12 Andrzej Kozlowski
8/7/12 Diana