Skip to main content
23 votes

Reverse Lookup: getting keys from values

PositionIndex[assoc][<value>] For example: ...
user1066's user avatar
  • 20.9k
22 votes
Accepted

Is this a bug in Replace?

This is not a bug. However, this is a great opportunity to shed some light on the inner workings of Replace. What happens, in short, is that it first fully ...
Leonid Shifrin's user avatar
19 votes

Reverse Lookup: getting keys from values

One way would be to "invert" the association and then look up values in the result: ...
WReach's user avatar
  • 69.2k
18 votes

Evaluating expressions by applying corresponding rules stored in another list

MapThread[ReplaceAll,{{x,x^2,x^3},{x->1,x->2,x->3}}]
cvgmt's user avatar
  • 79.5k
14 votes
Accepted

A simple problem about a Rule example

Pattern matching is performed based on the form of an expression, not its (mathematical) meaning. Using jargon, pattern matching is performed syntactically, not semantically. Specifically, here ...
Αλέξανδρος Ζεγγ's user avatar
14 votes
Accepted

I'm not able to delete -> in {{x -> a}, {x -> b}, {x -> c, x -> d}}

DeleteCases does not by default operate on heads. You can set the Heads option to True to ...
Mr.Wizard's user avatar
  • 273k
13 votes
Accepted

Pattern matching Association in rules

The problem can be seen in simpler expressions: MatchQ[<||>, <||>] (* True *) MatchQ[<||>, HoldPattern[<||>]] (* False *) This behaviour ...
WReach's user avatar
  • 69.2k
12 votes
Accepted

Add the values in the rule

Merge[Total]@{a -> 1, a -> 2, b -> 3, b -> 4}
rnotlnglgq's user avatar
  • 3,750
11 votes

Why and how does RuleDelayed evaluate to itself?

RuleDelayed is unconventional in that it strips any number of occurrences of Unevaluated from the right-hand side: ...
WReach's user avatar
  • 69.2k
11 votes

Can't understand one paragraph in Power Programming with Mathematica

He is simply saying that the first argument of Set is not evaluated before Set creates the definition. However, the sub-parts ...
Szabolcs's user avatar
  • 236k
11 votes
Accepted

Is there a simple way to apply a function to the RHS of a substitution?

...
Bob Hanlon's user avatar
  • 161k
11 votes
Accepted

StringReplace with multiple patterns

Short Version Order matters when specifying replacement rules. Rules are tried from left-to-right. Each rule will attempt to match and replace as much of the string as possible before moving on to ...
WReach's user avatar
  • 69.2k
11 votes

Evaluating expressions by applying corresponding rules stored in another list

I would use MapThread as it is the function designed for this purpose; but here is another solution step by step: ...
Syed's user avatar
  • 56.2k
11 votes

How can I change the expression x1 to x[1] in a simple way?

I added xy to exp0 just to prove there's no xy problem :) as well as a two-digit index: <...
Michael E2's user avatar
  • 241k
10 votes

Reverse Lookup: getting keys from values

You may use Position on an Association to return the Key (or keys) that holds a value. With ...
Edmund's user avatar
  • 43.1k
10 votes

Reverse Lookup: getting keys from values

Pick may be worthy of consideration: ...
Mr.Wizard's user avatar
  • 273k
10 votes

Pattern matching Association in rules

To @WReach's examples, one can add these: MatchQ[<||>, Unevaluated@<||>] MatchQ[<||>, HoldPattern[Evaluate@<||>]] (* False True *) It ...
Michael E2's user avatar
  • 241k
10 votes
Accepted

Joining all the keys of a list of rules that point to matching values

There are many ways, here's one: KeyValueMap[#2 -> # &] @ GroupBy[rules, Last -> First, Flatten]
Kuba's user avatar
  • 137k
10 votes
Accepted

How can I get new random values with each blank in ReplaceAll?

You can simply change the Rule -> to RuleDelayed :> to give ...
NonDairyNeutrino's user avatar
10 votes
Accepted

Delete zeros from a sparse array

You can just apply SparseArray to a SparseArray object to normalize it. For your example: ...
Carl Woll's user avatar
  • 131k
10 votes
Accepted

Convert a list of lists into a list of rules

Why not list = {{a, b}, {c, d}, {e, f}}; Rule @@@ list {a -> b, c -> d, e -> f} This will work as well as ...
m_goldberg's user avatar
  • 108k
10 votes

StringReplace with multiple patterns

WReach covered this well, but in supplement consider using the third parameter of StringReplace to see how a replacement evolves: ...
Mr.Wizard's user avatar
  • 273k
10 votes

Extract all Rules from Plot function

You can get the options associated with the plot theme "Detailed" using the (undocumented) internal function ...
kglr's user avatar
  • 399k
10 votes
Accepted

Manipulating rules

a = {x -> 2, y -> 3}; b = {x -> 3, y -> 5}; <|a|> + <|b|> I // Normal {x -> 2 + 3 I, y -> 3 + 5 I} ...
eldo's user avatar
  • 82.5k
9 votes
Accepted

Simple rule does not work

You can use patterns in your replacement rules: ...
Gyebro's user avatar
  • 386
9 votes

Is there a simple way to apply a function to the RHS of a substitution?

Additional alternatives: You can use MapAt to map myFunction at positions {All, -1, -1}: <...
kglr's user avatar
  • 399k
9 votes
Accepted

How to map a function to the value parts of rules

MapIndexed[#2[[1]] -> f @@ # &, pole] ...
kglr's user avatar
  • 399k
9 votes

Evaluating expressions by applying corresponding rules stored in another list

We define the following lists: list = {x, x^2, x^3}; rules = {x -> 1, x -> 2, x -> 3}; And then we can run Map ...
bmf's user avatar
  • 16.4k
9 votes
Accepted

Smart way to remove rules from lists, leaving just values, specifically from FindMinimum output

I want to add this reply in response to the comment written under the OP. The function Values does not work, as all the elements of the list are not rules. (the ...
bmf's user avatar
  • 16.4k
9 votes

Manipulating rules

{a, b} = {{x -> 2, y -> 3}, {x -> 3, y -> 5}}; Normal @ Merge[Apply @ Complex] @ {a, b} ...
kglr's user avatar
  • 399k

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