Skip to main content
22 votes
Accepted

How can I remove empty lists { } from a nested list?

Using ReplaceAll (which can also be written /.) we can replace all the empty lists { } with Nothing. ...
bill s's user avatar
  • 69.3k
12 votes

What's the difference between With and ReplaceAll?

With is a scoping construct, meaning that symbols localized by With will be unaffected by whatever value they may have outside ...
Sjoerd Smit's user avatar
  • 23.8k
12 votes

How to match a pattern only when it is not used as a head?

This trick is often useful with ReplaceAll: if you want some things to remain unaffected, you add a trivial replacement rule for that case before the replacement ...
Sjoerd Smit's user avatar
  • 23.8k
11 votes
Accepted

Why doesn't b = a; f[a_] := b; f[2] return 2?

Caution The post is long. If you just want to know how to fix the code, please search "fix the code in title in 10 ways" in the page. If you want to read discussions about global/local ...
xzczd's user avatar
  • 67k
10 votes

Apply function to all first arguments of a nested list of rules

list = {1 -> a, 2 -> {3 -> c, 4 -> d, 5 -> {6 -> f, 7 -> {8 -> h, 9 -> j}}}}; 1. MapAt ...
eldo's user avatar
  • 78.5k
10 votes

How can I remove empty lists { } from a nested list?

Using DeleteCases: ...
E. Chan-López's user avatar
10 votes

What's the difference between With and ReplaceAll?

The way With[{x=x0, …}, body] works is to take body, and replace every occurrence of x, etc. ...
xzczd's user avatar
  • 67k
9 votes
Accepted

Why does this pattern not match?

The first one matches because HoldPattern just prevents the pattern from evaluation, so it is equivalent to ...
Domen's user avatar
  • 27.3k
9 votes
Accepted

How can I write Log[a]/Log[b] in the form Log[b,a]?

sol = First@ Solve[2^x + 2^{x + 4} + 2^{x + 3} == 3^x + 3^{x + 4} + 3^{x + 3}, x, Reals] // FullSimplify x /. sol /. Log[a_]/Log[b_] :> Defer[Log[b, a]] ...
Syed's user avatar
  • 55.7k
9 votes
Accepted

How to convert this logarithm into this form?

Using the property ...
Nasser's user avatar
  • 146k
9 votes
Accepted

Create sublists whose totals exceed a certain threshold and that are as short as possible

Why not just use Most: ...
ydd's user avatar
  • 4,177
8 votes

Apply function to all first arguments of a nested list of rules

Using Replace / Map : ...
Syed's user avatar
  • 55.7k
8 votes
Accepted

Find non-descending sequences of list pairs

SequenceCases[list, x : {_, __} /; LessEqual @@ x[[All, -1]]]
lericr's user avatar
  • 30.3k
8 votes
Accepted

How to match a pattern only when it is not used as a head?

expr = -27 f + 27 f[1 + n] - 9 f[2 + n] + f[3 + n] == 1 + 3 n; p = Position[expr, f, Heads -> False] {{1, 1, 2}} ...
eldo's user avatar
  • 78.5k
8 votes
Accepted

Replace number in string with ? and /;

You've mixed up the usage of PatternTest (?) and Condition (...
xzczd's user avatar
  • 67k
8 votes
Accepted

Reversing the order of function arguments by a replacement rule

expr = g[x3, x2, x1]; You must use RuleDelayed (:>) instead of Rule ...
eldo's user avatar
  • 78.5k
8 votes
Accepted

Plotting a function containing a $\pm$ sign

Define: ( because I can ) ...
Syed's user avatar
  • 55.7k
7 votes
Accepted

Replace a column/row of a matrix under a condition by a random number

Modify -> (Rule) to :> (RuleDelayed).
xzczd's user avatar
  • 67k
7 votes

Why does Blank sometimes not match the empty expression?

One source of confusion here is about what MatchQ[, _] actually does. Contrary to what you think, the first argument of MatchQ here is not "the empty ...
Sjoerd Smit's user avatar
  • 23.8k
7 votes

How can I remove empty lists { } from a nested list?

list = {{{1}, {2}, {1, 2}, {1, 2, 3}, {}, {}}, {{2}, {3}, {}}}; p = Position[{}] @ list; {{1, 5}, {1, 6}, {2, 3}} ...
eldo's user avatar
  • 78.5k
7 votes
Accepted

Pattern matching based on parts of Head

Maybe something like this: Cases[expr, _?(StringMatchQ[ToString[Head[#]], "Fun*"] &), All]
lericr's user avatar
  • 30.3k
7 votes
Accepted

More concise forms generate list with elements 0 and 1

Update: An alternative approach to construct the restricted permutations directly (rather than filtering all permutations): ...
kglr's user avatar
  • 398k
7 votes

How to express the notion of "from "a" to the nearest "z"" with patterns?

str = "abczabvncz"; Borrowing the Shortest-pattern from vindobona (but with a triple underscore) ...
eldo's user avatar
  • 78.5k
6 votes

Selecting sublists from a ragged array of lists of strings

Select[MatchQ[{___, _?(StringMatchQ["data" ~~ ___]), ___}]] @ records ...
kglr's user avatar
  • 398k
6 votes
Accepted

Element position and repetition syntax

...
Syed's user avatar
  • 55.7k
6 votes
Accepted

Crafting a replacement rule to convert derivatives to finite differences

Aha, this question is for me. ...
xzczd's user avatar
  • 67k
6 votes
Accepted

Switch with string patterns

Switch doesn't use the form of the first argument to somehow predict what kind of comparison you want to do. So, it doesn't automatically try a string match just ...
lericr's user avatar
  • 30.3k
6 votes

Why does Blank sometimes not match the empty expression?

Your pattern is actually interpreted as a multiplication: FullForm[_{_Symbol}] (* List[Times[Blank, Blank[Symbol]]] *) To match a list consisting of a Symbol, use ...
MarcoB's user avatar
  • 67.4k
6 votes

Using patterns in ReplaceAll to match different symbols

If I understand the question, then k1 and k2 (shown below) are two groups that dictate the outcomes of the dot products. ...
Syed's user avatar
  • 55.7k
6 votes
Accepted

BlankNullSequence (___) when input variable is Null

Several core language features have involved in here. First of all, f[] isn't equivalent to f[Null]. Indeed, as mentioned in ...
xzczd's user avatar
  • 67k

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