Message Boards Message Boards

0
|
6103 Views
|
9 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Transform a list of Rules into a list of Values?

Solve[] and other Wolfram functions submit results as the list of rules. Sometime, there is necessity of transforming the list of rules into a list of values. It may be done, e.g., in the following ways:

In[1]:= Table[Subscript[x, i] -> i, {i, 100}]

Out[1]= {Subscript[x, 1] -> 1, Subscript[x, 2] -> 2, 
 Subscript[x, 3] -> 3, Subscript[x, 4] -> 4, Subscript[x, 5] -> 5, 
 Subscript[x, 6] -> 6, Subscript[x, 7] -> 7, Subscript[x, 8] -> 8, 
 Subscript[x, 9] -> 9, Subscript[x, 10] -> 10, Subscript[x, 11] -> 11,
  Subscript[x, 12] -> 12, Subscript[x, 13] -> 13, 
 Subscript[x, 14] -> 14, Subscript[x, 15] -> 15, 
 Subscript[x, 16] -> 16, Subscript[x, 17] -> 17, 
 Subscript[x, 18] -> 18, Subscript[x, 19] -> 19, 
 Subscript[x, 20] -> 20, Subscript[x, 21] -> 21, 
 Subscript[x, 22] -> 22, Subscript[x, 23] -> 23, 
 Subscript[x, 24] -> 24, Subscript[x, 25] -> 25, 
 Subscript[x, 26] -> 26, Subscript[x, 27] -> 27, 
 Subscript[x, 28] -> 28, Subscript[x, 29] -> 29, 
 Subscript[x, 30] -> 30, Subscript[x, 31] -> 31, 
 Subscript[x, 32] -> 32, Subscript[x, 33] -> 33, 
 Subscript[x, 34] -> 34, Subscript[x, 35] -> 35, 
 Subscript[x, 36] -> 36, Subscript[x, 37] -> 37, 
 Subscript[x, 38] -> 38, Subscript[x, 39] -> 39, 
 Subscript[x, 40] -> 40, Subscript[x, 41] -> 41, 
 Subscript[x, 42] -> 42, Subscript[x, 43] -> 43, 
 Subscript[x, 44] -> 44, Subscript[x, 45] -> 45, 
 Subscript[x, 46] -> 46, Subscript[x, 47] -> 47, 
 Subscript[x, 48] -> 48, Subscript[x, 49] -> 49, 
 Subscript[x, 50] -> 50, Subscript[x, 51] -> 51, 
 Subscript[x, 52] -> 52, Subscript[x, 53] -> 53, 
 Subscript[x, 54] -> 54, Subscript[x, 55] -> 55, 
 Subscript[x, 56] -> 56, Subscript[x, 57] -> 57, 
 Subscript[x, 58] -> 58, Subscript[x, 59] -> 59, 
 Subscript[x, 60] -> 60, Subscript[x, 61] -> 61, 
 Subscript[x, 62] -> 62, Subscript[x, 63] -> 63, 
 Subscript[x, 64] -> 64, Subscript[x, 65] -> 65, 
 Subscript[x, 66] -> 66, Subscript[x, 67] -> 67, 
 Subscript[x, 68] -> 68, Subscript[x, 69] -> 69, 
 Subscript[x, 70] -> 70, Subscript[x, 71] -> 71, 
 Subscript[x, 72] -> 72, Subscript[x, 73] -> 73, 
 Subscript[x, 74] -> 74, Subscript[x, 75] -> 75, 
 Subscript[x, 76] -> 76, Subscript[x, 77] -> 77, 
 Subscript[x, 78] -> 78, Subscript[x, 79] -> 79, 
 Subscript[x, 80] -> 80, Subscript[x, 81] -> 81, 
 Subscript[x, 82] -> 82, Subscript[x, 83] -> 83, 
 Subscript[x, 84] -> 84, Subscript[x, 85] -> 85, 
 Subscript[x, 86] -> 86, Subscript[x, 87] -> 87, 
 Subscript[x, 88] -> 88, Subscript[x, 89] -> 89, 
 Subscript[x, 90] -> 90, Subscript[x, 91] -> 91, 
 Subscript[x, 92] -> 92, Subscript[x, 93] -> 93, 
 Subscript[x, 94] -> 94, Subscript[x, 95] -> 95, 
 Subscript[x, 96] -> 96, Subscript[x, 97] -> 97, 
 Subscript[x, 98] -> 98, Subscript[x, 99] -> 99, 
 Subscript[x, 100] -> 100}

In[2]:= Table[Subscript[x, i] -> i, {i, 100}] /. (_ -> x_) -> x

Out[2]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, \
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, \
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, \
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, \
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, \
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}

In[3]:= Table[Subscript[x, i] -> i, {i, 100}] /. Rule[_, x_] -> x

Out[3]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, \
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, \
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, \
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, \
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, \
86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}

Are these approaches good enough or there are other more elegant approaches?

9 Replies

Sure! Only the forms of writing are different!

Number 3 and 4 are exactly the same btw:

FullForm[(_ -> x_)]
Rule[Blank[], Pattern[x, Blank[]]]
POSTED BY: Sander Huisman

Agree! Values is nice.

In[5]:= list = Table[Subscript[x, i] -> i, {i, 1000000}];

In[17]:= {
 (*1*)(Values[list] // AbsoluteTiming)[[1]],
 (*2*)(list[[All, 2]] // AbsoluteTiming)[[1]],
 (*3*)(list /. (_ -> x_) -> x // AbsoluteTiming)[[1]],
 (*4*)(list /. Rule[_, x_] -> x // AbsoluteTiming)[[1]],
 (*5*)(Map[Last, list] // AbsoluteTiming)[[1]],
 (*6*)((#2 & @@@ list) // AbsoluteTiming)[[1]],
 (*7*)(list[[#, 2]] & /@ Range[Length[list]] // AbsoluteTiming)[[1]],
 (*8*)((variableList = Table[Subscript[x, i], {i, 10000}];
     variableList /. list) // AbsoluteTiming)[[1]]
 }

Out[17]= {0.0548996, 0.0471586, 0.575051, 0.564489, 0.551187, \
0.674558, 4.75912, 10.5918}

Can't be done more efficient than the list[[All,2]]. That is directly taking a 'piece' of memory. No computation. Another method, not mentioned here, but syntacticly nicer is to use Values, rather than Part (list[[All,2]]):

Values[list]

which is equally fast than list[[All,2]] but much more readable. I would ill-advice all the other methods, not only for speed, but also readability and maybe future changes.

POSTED BY: Sander Huisman

Good observation!!! But the test is on a list with 1 million elements. So the results of testing are:

In[3]:= list = Table[Subscript[x, i] -> i, {i, 1000000}];

In[7]:= {
 (*1*)(list[[All, 2]] // AbsoluteTiming)[[1]],
 (*2*)(list /. (_ -> x_) -> x // AbsoluteTiming)[[1]],
 (*3*)(list /. Rule[_, x_] -> x // AbsoluteTiming)[[1]],
 (*4*)(Map[Last, list] // AbsoluteTiming)[[1]],
 (*5*)((#2 & @@@ list) // AbsoluteTiming)[[1]],
 (*6*)(l = Length[list]; 
   Table[list[[i]][[2]], {i, l}] // AbsoluteTiming)[[1]],
 (*7*)((variableList = Table[Subscript[x, i], {i, 10000}];
     variableList /. list) // AbsoluteTiming)[[1]]
 }

Out[7]= {0.0522145, 0.5495, 0.544369, 0.553599, 0.677075, 4.00628, \
8.24465}

Nevertheless, the question remains... Can be found a more efficient code?

Why, if it's just about getting the values not just?

list[[All, 2]] // AbsoluteTiming

{0.000050084, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}}

POSTED BY: l van Veen

I provided a simple time experiment with the above codes and a list with 1 mln elements:

In[1]:= list = Table[Subscript[x, i] -> i, {i, 1000000}];

In[2]:= { 
 (*1*) (list /. (_ -> x_) -> x // AbsoluteTiming)[[1]],
 (*2*) (list /. Rule[_, x_] -> x // AbsoluteTiming)[[1]],
 (*3*) (Map[Last, list] // AbsoluteTiming)[[1]],
 (*4*) ((#2 & @@@ list) // AbsoluteTiming)[[1]],
 (*5*) (l = Length[list]; 
   Table[list[[i]][[2]], {i, l}] // AbsoluteTiming)[[1]],
 (*6*) ((variableList = Table[Subscript[x, i], {i, 10000}]; 
     variableList /. list) // AbsoluteTiming)[[1]]
 }

Out[2]= {0.333781, 0.322281, 0.380717, 0.471882, 3.18167, 12.5134}

With:

rList = Table[Subscript[x, i] -> i, {i, 100}]

one more way:

#2 & @@@ rList
POSTED BY: Henrik Schachner

Two more ways:

    variableList = Table[Subscript[x, i], {i, 100}];
    ruleList = Table[Subscript[x, i] -> i, {i, 100}];
    variableList /. ruleList
    Map[Last, ruleList]

The replacement variableList /. ruleList seems conceptually cleaner to me. I suspect that Map[Last, ruleList] may be faster for very large lists, but I didn't check.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract