MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to swap two elements of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127597] Re: How to swap two elements of a list
  • From: Ulrich Arndt <ulrich.arndt at data2knowledge.de>
  • Date: Mon, 6 Aug 2012 04:36:08 -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>

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
>




  • Prev by Date: Re: Problem finding maximum
  • Next by Date: Re: How to swap two elements of a list
  • Previous by thread: Re: How to swap two elements of a list
  • Next by thread: Re: How to swap two elements of a list