Tag Info

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 ...
• 69.1k
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 ...
• 69.1k
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 ...
• 69.1k
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) ...
• 69.1k
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. ...
• 69.3k

How can a verbal reasoning question be solved with Mathematica?

Riffing off of DanielHuber's answer, and with less user intervention: ...
• 15.3k

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, ...
• 398k
Accepted

StringContainsQ, but anywhere in order

StringMatchQ["aabbc", "*a*c*"] True StringMatchQ["aabbc", "*b*a*"] False You can also use ...
• 398k
Accepted

...
• 398k

Number of sign flips in list?

list = {1, 1, -1, 1, -1, -1, 1, 1, 1, -1}; Total[Unitize[Subtract[list, RotateRight @ list]]] 6 ...
• 398k

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 ...
• 131k

Replace zeros in a list with last nonzero value

Define a simple function, using FoldList ...
• 16.8k

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_, ...
• 398k
Accepted

Pattern matching for repeated elements

Use the Longest pattern command to find the longest repeated sequence that matches the pattern. ...
• 3,180
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: ...
• 238k

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 ...
• 398k
Accepted

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

You can modify DownValues[f] directly. For example ...
• 85.1k
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}]

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 ...
• 108k
Accepted

HoldPattern vs Unevaluated

HoldPattern is a persistent head that prevents evaluation of its argument(s) yet is transparent to pattern matching. Unevaluated...
• 272k
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 ...
• 9,488
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 ...
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 ...
• 398k

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} ...
• 398k

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 ...
• 23.8k

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: <...
• 23.9k

Pattern matching for repeated elements

Another possibility is to use SequenceReplace: SequenceReplace[{1, 2, 2, 2, 3, 4}, {x_, x_ ..} -> Sequence[]] {1, 3, 4}
• 131k
Accepted

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

Perhaps this?: ...
• 238k
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., ...
• 23.9k