Re: How to swap two elements of a list

• To: mathgroup at smc.vnet.net
• Subject: [mg127598] Re: How to swap two elements of a list
• From: Andrzej Kozlowski <akozlowski at gmail.com>
• Date: Mon, 6 Aug 2012 04:36:28 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-newout@smc.vnet.net
• Delivered-to: mathgroup-newsend@smc.vnet.net
• References: <jvklja\$643\$1@smc.vnet.net> <20120805185854.57EE0683E@smc.vnet.net>

```On 5 Aug 2012, at 20:58, Diana Mecum wrote:

> ***,
>
> Thank you for getting back to me. I had a little difficulty describing what I was trying to do.
>
>
> {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
>

Well, I don't know if it is clear or not because the answer you gave in your first post seems wrong and, in particular, your Master List has length 130 and your Final List has length 141. How you can get form a list of length 130 to a list of length 141 by simply replacing elements is beyond my understanding. Assuming, however, that my interpretation of your problem is correct, a simple way to deal with it is:

swaplist1 = {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};
swaplist2 = {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};
masterlist = {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};

rules1 = Rule @@@ Transpose[{swaplist1, swaplist2}];
rules2 = Reverse /@ rules1;
rules = Join[rules1, rules2];

finallist = masterlist /. rules

{4,8,2,9,16,3,5,25,27,7,32,49,64,11,13,81,121,17,125,128,169,243,19,256,289,343,23,361,512,529,625,29,729,841,961,1024,1331,1369,1681,1849,31,37,2048,41,2187,2197,2209,2401,2809,3125,3481,3721,43,4096,4489,4913,5041,5329,6241,6561,6859,6889,7921,8192,9409,10201,10609,47,11449,53,11881,12167,12769,14641,15625,16129,16384,59,16807,17161,18769,19321,19683,22201,22801,61,24389,24649,26569,27889,67,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,71,73441,76729,73,78125,78961,79507,80089,83521,85849,94249,96721,97969,100489}

Andrzej Kozlowski

```

• Prev by Date: Re: How to swap two elements of a list
• Next by Date: Simplifying expressions using rules with recursion
• Previous by thread: Re: How to swap two elements of a list
• Next by thread: Re: How to swap two elements of a list