14 votes
Accepted

Association vs Pair - Creation performance, Selection performance

Simply use keys = Range[1000000]; vals = Range[1000000]^2; result = Pick[vals, PrimeQ[keys]]; to get the best of both worlds (zero construction cost and even ...
user avatar
11 votes
Accepted

Maps between finite sets

alist = {3, 4, 5}; blist = {"a", "b"}; mapfs = Tuples@Outer[Rule, blist, alist] $$\begin{array}{l} \{\text{a}\to 3,\text{b}\to 3\} \\ \{\text{...
user avatar
  • 14.1k
9 votes
Accepted

Sorting a list of "Low", "Medium", "High" into low, medium, high

The idea in the code below is to replace each entry in the list by a number corresponding to the "priority" of that element, and then sorting according to that priority using ...
user avatar
  • 58.2k
6 votes

A Nothing for Associations

You could make a symbol that turned any Rule it is part of into Nothing with something like ...
user avatar
  • 62.4k
6 votes

Maps between finite sets

There's no built-in function but the following constructs the mappings: ...
user avatar
  • 212k
5 votes
Accepted

Replacement, PatternTest, Module related Question

The reason your attempt fails is that at the time the Function is applied to its arguments, m has not yet been replaced by ...
user avatar
  • 23.2k
5 votes
Accepted

Classifying elements, by the value of a fixed function, using Replace

L = Table[ConstantArray[1, RandomInteger[999]], 100000]; m = Replace[L, {x_List :> Quotient[Length@x, 100]}, {1}]; $$\{7,2,8,8,6,5,1,5,2,1,1,3,6,7,2,4,7,2,1,8,...
user avatar
  • 14.1k
3 votes

Sorting a list of "Low", "Medium", "High" into low, medium, high

Permute[SortBy[Gather[list],First], Cycles[{{2,1,3}}]]//Flatten where ...
user avatar
  • 10.2k
2 votes

How to pick lists to satisfy a given form?

Not sure if this kind of answer is appropriate on this forum. There are many assumptions baked in, and it might be hard to modify if those assumptions are wrong. It also seems more complicated that ...
user avatar
  • 3,452
1 vote
Accepted

Create a Table with 3 different variables from 2 Lists and 1 Table

Let's assume that the x-coordinates are in a list called xs. Similarly for a list called ys. Let's say ...
user avatar
  • 3,452
1 vote

Modify list elements if last two entries are equal

If your data is always a list of lists and you always want the transformation performed at that second level, then this works (I've named your data theData): ...
user avatar
  • 3,452
1 vote

Modify list elements if last two entries are equal

Another way: ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible