Tag Info

Hot answers tagged

14

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]*"] ]


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}, {}}}


6

I think you need to use a group-theoretical construction. In this way you will have full freedom in specifying any group of permutations you need. In your case the group is G = PermutationGroup[{Cycles[{{1, 2}, {4, 5}}], Cycles[{{1, 2, 3}, {4, 5, 6}}]}]; This generates a symmetric group on {1, 2, 3}, which also forces the same permutations on {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

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

diameter[x_List] := Max[EuclideanDistance @@@ Subsets[x, {2}]] diameter[pointset] (* 2 Sqrt[5] *) Now, let's improve the performance "a little bit". I believe the maximal distance will be realized at the points' convex hull (I'll not demonstrate it,but it's quite intuitive). Now, if you have a lot of points Mathematica provides a convenient and fast way ...


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

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


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

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


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

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_ /; ...


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:


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).


1

I think your real problem is the "definition of correct": the only reference for Mathematica (or the "Wolfram language") is the documentation, and that is in many cases somewhat vague. So for every behavior that is not in a clear conflict with the documentation you basically have to accept what you get. The level 0 matching of Pick might be a corner case but ...


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}}} *)



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