Skip to main content
32 votes
Accepted

Mysterious behaviour of Pick

This is not a bug. It is a consequence of the manner in which Pick scans its arguments. The Pick Process The documentation ...
WReach's user avatar
  • 69.1k
29 votes
Accepted

Why (and when) does pattern matching with f[__] perform MUCH more quickly than _f?

Times has the attributes Flat and Orderless. This means that any pattern that matches some ...
WReach's user avatar
  • 69.1k
26 votes
Accepted

How to replace all elements of a list by a rule "element" -> "element_"

Here is one way: Pattern[#, Blank[]] & /@ {a, b, c, d, e, f, g, h, i, j} (* {a_, b_, c_, d_, e_, f_, g_, h_, i_, j_} *) An inspection of the ...
WReach's user avatar
  • 69.1k
22 votes
Accepted

Count works differently depending on how an association was created

As Kirill Belov notes in a comment, the issue is related to the fact that the list a1 is a packed array (generated by Range) ...
WReach's user avatar
  • 69.1k
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
21 votes

How can a verbal reasoning question be solved with Mathematica?

Riffing off of DanielHuber's answer, and with less user intervention: ...
Michael Seifert's user avatar
20 votes

Replace zeros in a list with last nonzero value

SequenceReplace SequenceReplace[ p:{a_, 0..} :> Sequence @@ (p /. 0 -> a)] @ l {-1, -1, -1, 1, 1, 1, -1, -1, 1, 1, 1, 1, ...
kglr's user avatar
  • 398k
18 votes
Accepted

StringContainsQ, but anywhere in order

StringMatchQ["aabbc", "*a*c*"] True StringMatchQ["aabbc", "*b*a*"] False You can also use ...
kglr's user avatar
  • 398k
17 votes
Accepted

Delete redundant {x,y} pairs

...
kglr's user avatar
  • 398k
17 votes

Number of sign flips in list?

list = {1, 1, -1, 1, -1, -1, 1, 1, 1, -1}; Total[Unitize[Subtract[list, RotateRight @ list]]] 6 ...
kglr's user avatar
  • 398k
17 votes

Number of sign flips in list?

Here's my version: Total @ Unitize @ BitXor[list, RotateRight[list]] 6 Addendum Assuming the lists consist of only 1 and -1, then the ...
Carl Woll's user avatar
  • 131k
17 votes

Replace zeros in a list with last nonzero value

Define a simple function, using FoldList ...
mikado's user avatar
  • 16.8k
17 votes

How to replace all elements of a list by a rule "element" -> "element_"

A few additional alternatives to inject patterns on the rhs: list = {a, b, c, d, e, f, g, h, i, j}; Replace[list, a_ :> (x_ /. x -> a), 1] {a_, b_, c_, ...
kglr's user avatar
  • 398k
17 votes
Accepted

Pattern matching for repeated elements

Use the Longest pattern command to find the longest repeated sequence that matches the pattern. ...
TimRias's user avatar
  • 3,180
16 votes
Accepted

How to copy with the last 1 with pattern matching method in a list?

One way: {1, 2, 3, 1, 5, 8, 1, 9, 6} /. {x : Longest[___], 1, y___} :> {x, 100, y} (* {1, 2, 3, 1, 5, 8, 100, 9, 6} *) Another way: ...
Michael E2's user avatar
  • 238k
16 votes

Replace elements that do not match a pattern

Replace[mat, {0 -> 1, _ -> 0}, {2}] (* or Replace[mat, {0 -> 1, Except[0] -> 0}, {2}] *) {{1, 0, 0}, {0, 1, 0}, {1, 1, 0}} Also ...
kglr's user avatar
  • 398k
16 votes
Accepted

How to clear all definitions matching f[1, ...] but not f[2, ...]?

You can modify DownValues[f] directly. For example ...
Simon Woods's user avatar
  • 85.1k
16 votes
Accepted

Select data from a structured list

Maybe there is something more elegant, but there you go: Cases[data, {n_Integer} | {_?(LessThan[2]), b__} :> {n, b}]
Najib Idrissi's user avatar
15 votes

Why (and when) does pattern matching with f[__] perform MUCH more quickly than _f?

This is not an answer, but an extended comment. I experimented with your code using V11.0.1 on OS X 10.10.2 running on a seven year old iMac. I can confirm that something funny is going on that it ...
m_goldberg's user avatar
  • 108k
15 votes
Accepted

HoldPattern vs Unevaluated

HoldPattern is a persistent head that prevents evaluation of its argument(s) yet is transparent to pattern matching. Unevaluated...
Mr.Wizard's user avatar
  • 272k
15 votes
Accepted

Is it possible to set multiple type to a variable?

You may specify multiple alternative patterns (originally mentioned in a comment): f[x:(_Real|_Integer)] := 2 x; You may specify multiple alternative matches ...
eyorble's user avatar
  • 9,488
15 votes
Accepted

Why does MatchQ[a, r_ /; Head[r] != Plus] evaluate to False?

The problem is this: If a is a symbol then its Head is Symbol, so ...
Henrik Schumacher's user avatar
15 votes
Accepted

Is there a defined priority for pattern matching?

The Ordering Of Definitions ... The Wolfram System follows the principle of trying to put more general definitions after more specific ones. This means that special cases of rules are typically tried ...
kglr's user avatar
  • 398k
15 votes

Change part of a list

Try SequenceReplace SequenceReplace[{1, 2, 1} -> Sequence[2, 1, 2]] @ lis1 {1, 2, 3, 4, 1, 2, 3, 2, 1, 2} ...
kglr's user avatar
  • 398k
15 votes

StringContainsQ, but anywhere in order

Is this what you need? StringContainsQ["aabbc","a" ~~ ___ ~~ "c"] True The following documentation pages should help you get going with string patterns in ...
Sjoerd Smit's user avatar
  • 23.8k
15 votes

How to define a Rule to match a Head except if it is a Part of another?

You can use the fact that once an object is replaced, it doesn't get replaced by any further rules. So, just add a rule that replaces a DateObject with itself: <...
march's user avatar
  • 23.9k
15 votes

Pattern matching for repeated elements

Another possibility is to use SequenceReplace: SequenceReplace[{1, 2, 2, 2, 3, 4}, {x_, x_ ..} -> Sequence[]] {1, 3, 4}
Carl Woll's user avatar
  • 131k
15 votes
Accepted

Replace $n$-th occurrence with number $n$

Perhaps this?: ...
Michael E2's user avatar
  • 238k
15 votes
Accepted

How do I correctly apply patterns to function arguments using Pattern (:), PatternTest (?), and Condition (/;)

This is somewhat subtle, but really it's just a syntax issue. If you want to use the colon for pattern-matching, leave off the underscore, i.e., ...
march's user avatar
  • 23.9k
14 votes

Position and string patterns

For large lists, this should be snappy: Pick[Range@Length@dat, StringTake[dat, UpTo@4], "star"] and actually, taking advantage of listability, ...
ciao's user avatar
  • 25.9k

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