Tag Info

Hot answers tagged

13

I propose: StringCases[ {"Fe3O4", "CO", "MgO", "Uut14AuO6"}, DigitCharacter .. | (_?UpperCaseQ ~~ ___?LowerCaseQ) ] {{"Fe", "3", "O", "4"}, {"C", "O"}, {"Mg", "O"}, {"Uut", "14", "Au", "O", "6"}} Or as a RegularExpression: StringCases[ {"Fe3O4", "CO", "MgO", "Uut14AuO6"}, RegularExpression["\\d+|[A-Z][a-z]*"] ]


10

The problem does not lie with WordBoundary, it is due to the use of __. The string pattern WordBoundary ~~ __ ~~ "man" will find a word boundary properly, but then will match a following sequence of characters up to "man" without restriction -- including characters that lie on word boundaries themselves. To exclude that possibility, we should restrict the ...


9

Here's a way, that is a bit like @kguler's, but it's simpler and a little faster: list = {{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}}; Cases[list, {___, {__}, ___}] (* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *) The pattern ___ (three underscores or BlankNullSequence) matches zero or more things and the pattern __ (two underscores or BlankSequence) matches ...


8

Another functional approach using the Flat attribute: (Credit to Mr Wizard for the clever form of the second line) SetAttributes[f, Flat]; f[1, 0, 0] = Style[#, Red] & /@ f[1, 0, 0]; List @@ f @@ list


7

Using highlight from my answer to Formatting text through pattern matching: ToString[list] /. highlight["1, 0, 0", Style[#, Red] &]


7

Just define a new function that could take different inputs, either through overloading or using If, Which, or Switch. Clear[f] f[x_. Power[z, e_.]] := {x, e} f[Times[x_, Power[z, e_.]]] := {x, e} f[x_] := {x, 0} Cases[lis, y : Alternatives[x_., x_. Power[z, e_.], Times[x_, Power[z, e_.]]] :> f[y]] (* {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} *) ...


7

list = {{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}}; Cases[list, Except[{{} ..}]] (* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *) or Cases[list, {___, Except[{}], ___}] (* {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} *) You can also use PatternTest (_?func) where func is any selector function that you might have used as the second argument of Select. For example: ...


6

Unless you have other requirements I recommend DeleteCases: list = {{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}}; DeleteCases[list, {{} ..}] {{{1, 2}, {3}, {4, 5}}, {{6}, {}}} For deletion at all levels you could use: list /. {{} ..} -> Sequence[] {{{1, 2}, {3}, {4, 5}}, {{6}, {}}}


5

Another possibility is to use ReplaceAll on the rhs of RuleDelayed in the second argument of Cases: rules = Alternatives[x_. Power[z, e_.] :> {x, e}, Times[x_, Power[z, e_.]] :> {x, e}, x_. :> {x, 0}]; (* your prefered second argument for Cases *) Cases[lis, pat : rules[[All, 1]] :> (pat /. List @@ rules)] (* {{6, 0}, {1, ...


5

There is a reason to use WordBoundary, but your example sentence doesn't bring it out. Consider mystring = "I am a demanding fan of Superman, Spiderman and Batman."; StringCases[mystring, LetterCharacter ... ~~ "man"] {"deman", "Superman", "Spiderman", "Batman"} so you might prefer StringCases[mystring, LetterCharacter ... ~~ "man" ~~ WordBoundary] ...


5

One possibility is to use Replace with Sow/Reap: Scan[ Replace[#, { x_. Power[z, e_.] :> Sow[{x, e}], Times[x_, Power[z, e_.]] :> Sow[{x, e}], x_ :> Sow[{x, 0}]}] &, lis] // Reap // #[[-1, -1]] & {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} However, the wildcard x_ could be tricky if you needed to operate deeper ...


5

Using string manipulations seems to speed things up significantly: randomList = RandomInteger[{0, 1}, 1000]; m1 = randomList //. {b___, PatternSequence[1, 0, 0], a___} -> {b, Sequence @@ (Style[#, Red] & /@ {1, 0, 0}), a} // AbsoluteTiming; m2 = StringSplit[StringJoin @@ (ToString /@ randomList), "100" -> Sequence @@ (Style[#, ...


4

The General Case Alas, Cases does not permit multiple replacement rules. But Replace does, although we must take care to 1) create an output list, 2) omit non-matching elements and 3) operate only upon the first level: lis = 6 + 1/z^4 + 4/z^2 + 4 z^2 + z^4; Replace[List @@ lis, {x_. z^e_. :> {x, e}, x_ :> {x, 0}, _ :> Sequence[]}, {1}] (* ...


4

I believe you're looking for RepeatedNull Count[IdentityMatrix[10], {0 ..., 1, 0 ...}] (* 10 *)


4

I prefer one of @Mr.Wizard's solutions based on StringCases, but here is a solution using StringSplit: StringSplit["Fe3O4", RegularExpression["(?=[A-Z]|\\d)"]] (* {"Fe", "3", "O", "4"} *) It splits the string at any position that is followed by an upper case letter or a digit. If multiple digits are possible: StringSplit["Fe23O42", ...


3

that picks up all the elements of the outer list that have at least one non-empty list as an element. I think Except is the logical choice and more functional also. But for fun, since Length[] applied to {{}} gives zero (after Flatten), may be this can be used to check lis = {{{1, 2}, {3}, {4, 5}}, {{6}, {}}, {{}, {}}}; Cases[lis, x_ /; ...


3

I keep on thinking how ListCorrelate sounds ideal for this but can't find a way. A functional way (but still slower) would be: g[g[b__], d_] := g[b, d]; g[a___, 1, 0, 0] := Sequence[a, Sequence @@ (Style[#, Red] & /@ {1, 0, 0})] and then using Fold: List @@ Fold[g, First@list, Rest@list] ---EDIT--- which, after Mr Wizard's recommendation, can ...


3

elements = SortBy[ElementData[#, "Abbreviation"] & /@ ElementData[], Minus@*StringLength]; StringCases["Fe3O2", DigitCharacter .. | elements] {"Fe", "3", "O", "2"} (Thanks to Mr.Wizard for syntax improvements.)


3

You can use chemSplit[s_String] := Module[{pos = StringPosition[s, {_?UpperCaseQ, NumberString}, Overlaps -> False][[All, 1]]}, StringSplit@StringInsert[s, " ", pos] ] chemSplit["Fe3O4"] {"Fe", "3", "O", "4"}


3

This should work for any function (f, g or whatever) and any independent variable (t or whatever): Cases[f g + Dt[f, t] g + Dt[g, {t, 2}] f, Dt[_, {_, order_}] :> order, Infinity] If you want to inlcude the first order derivative, there is a little bit more to write. Indeed, the pattern Dt[_, _] is evaluated to 1 before it can be used by Cases to find ...


3

I just checked the documentation in V10 and accidentally stumbled upon a built in command: SQLConnectionOpenQ[conn] This seems to do the trick.


3

Sow and Reap: {result, variables} = Reap[ expr /. f[x_] :> fNew[Sow@x] ] variables (* Out: {{x, y}} *) In your original solution you do not need Block: variables = {}; expr /. f[x_] :> (variables = Union[variables, {x}]; fNew[x]) And AppendTo can make your code shorter: variables = {}; expr /. f[x_] :> (AppendTo[variables, x]; fNew[x]) ...


3

This is because Mathematica looks for the largest piece of the expression that matches the pattern. Your pattern is x_, which means every conceivable expression there is will match that pattern. As a result, the entire expression {x,y,z} will be matched as it is the largest matching piece (x, y, and z won't be matched because they are smaller pieces compared ...


2

What goes wrong The help page of ReplaceAll explains why your code is not doing what you want. ReplaceAll (/.) applies a rule or list of rules in an attempt to transform each subpart of an expression expr In the end it applies the rule to the largest subpart it can match. The expression {x,y,z} has two levels (depth of two). {x, y, z} // FullForm ...


2

Replace[{x, y, z}, x_ :> ("^_^" <> ToString[x]), Infinity]


2

maxOrder[expr_] := Max[{0, (# /. _Symbol :> 1) & /@ Last /@ (Level[#, {-1}] & /@ Cases[expr, _Dt, Infinity])}]; maxOrder[f g + Dt[f, t] g + Dt[g, {t, 2}] f] 2 maxOrder[f g + Dt[f, t] g] 1 maxOrder[f g] 0


2

Just to be different f = Flatten[List @@@ WolframAlpha["formula " <> #, "Result"][[1, 1]]] &; f /@ {"Fe2O3", "MgO"} {{"Fe", 2, "O", 3}, {"Mg", "O"}} This approach seems to be stupid, but it can be easily extended to another chemical data (e.g. molar mass).


2

A faster variant of Seismatica's answer: List @@ StringReplace[StringJoin[ToString /@ list], "100" -> {Style[1, Red], Style[0, Red], Style[0, Red]}] /. x_String :> Table[0, {StringLength@x}] // Flatten Here' s a time table running the functions 100 times over a random 0 | 1 list with 1000 members:


1

How a bout this for all levels: lis = {{{1, 2}, {3}, {4, 5, {5}}}, {{6}, {}, {}}, {{}, {{}, {}}}, {{}, {}, {2}}}; Select[lis , #/# =!= # || # =!= # + # &] // Quiet (* {{{1, 2}, {3}, {4, 5, {5}}}, {{6}, {}, {}}, {{}, {}, {2}}} *)


1

mxOrdr = Max[0,Max@Cases[Replace[#, HoldPattern[Dt[x_, t:Except[_List]]] :> HoldForm[Dt[x, {t, 1}]], {0, Infinity}], x_Dt :> Unevaluated[x][[2, -1]], {0, Infinity}]] &; mxOrdr[f g + Dt[f, t] g + Dt[g, {t, 2}] f] (* 2 *)



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