Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

How to swap two elements of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127570] How to swap two elements of a list
  • From: Diana Mecum <diana.mecum at gmail.com>
  • Date: Sat, 4 Aug 2012 21:46:35 -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

I would like to find a snazzy way to elements in the MASTER LIST. The method I want to use is the following:

1. Swap the first element of SWAP LIST 1, with the first element of SWAP LIST 2, in the MASTER LIST.
. . . . a. Swap the 2 and 4.
. . . . b. Swap the 3 and 8.
. . . . c. Swap the 4 and 9.

I have done it with an awkward process, by creating a new list, and parsing element by element. My Mathematica code is below.

The final result I want is:

FINAL LIST:

{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,103823,109561,113569,117649,120409,79,121801,124609,128881,130321,131072}

MASTER LIST:

{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}

SWAP LIST 1:

{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}

SWAP LIST 2

{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}

MY CURRENT MATHEMATICA CODE:

Table[A000961Working[[n]],{n,1,130}]
{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}

Prime[Range[142]]
{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}

ShortList=Complement[A000961Working,Prime[Range[1000000]]]
{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}

Take[Table[If[A000961Working[[i]]==Prime[Range[142]][[j]],A163813Working[[i]]=ShortList[[j]],],{i,1,142},{j,1,142}],142];

Take[Table[If[A000961Working[[i]]==ShortList[[j]],A163813Working[[i]]=Prime[Range[142]][[j]],],{i,1,142},{j,1,142}],142];

A163813=Prepend[A163813Working,1];

Take[A163813,142]
{1,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,103823,109561,113569,117649,120409,79,121801,124609,128881,130321,131072}



  • Prev by Date: Why no OSX SystemDialogInput["RecordSound"]?
  • Next by Date: Re: Evaluation of a whole notebook from another one
  • Previous by thread: Re: Why no OSX SystemDialogInput["RecordSound"]?
  • Next by thread: Re: How to swap two elements of a list