Tag Info

Hot answers tagged

15

This is a bug in version 10.1.0. We decided it was serious enough to warrant a fix via an automatic paclet update. The paclet has been pushed live and Mathematica should install it automatically once it does a periodic check with the paclet server. It should take about a week or so. To install it right away, you can do PacletInstall["StringPatternFix"]. You ...


10

if the keys are unique, then this seems to be rather elegant, short and still clear: Pick[ln, nm, "tat"] probably look at the documentation of Pick...


8

You can start by simply creating a function that tests, whether a string is a list of rules or not isTransformable[str_String] := SyntaxQ[str] && MatchQ[MakeExpression[str], HoldComplete[{_Rule ..}]]; isTransformable[___] := False; Note that this function does much more that search for a "->" inside a string. First, it tests, whether the ...


7

Perhaps, using: dateslalala={2003364, 2003157, 2003314, 2003302, 2003181, 2003062, 2003254, \ 2003070, 2003365, 2003338, 2003233, 2003073, 2003020, 2003010, \ 2003238, 2003107, 2003310, 2003347, 2003204, 2003066, 2005364, \ 2005157, 2005314, 2005302, 2005181, 2005062, 2005254, 2005070, \ 2005365, 2005338, 2005233, 2005073, 2005020, 2005010, 2005238, \ ...


6

Another option: AssociationThread[nm -> ln]["tat"] If you store AssociationThread[lm -> ln] in a symbol you can use it for many quick lookups without having to recreate the association every time. Without Pick and AssociationThread you might do something like Identity @@ Cases[Transpose[{nm, ln}], {"tat", v_} :> v] Note that I'm using ...


5

You could measure the similarity between two strings. One simple approach is to convert all strings into the bag-of-words model and then compare the resulting vectors. This could work well if the strings contain the same words, but not in the same order. Nearest[vectors, x, DistanceFunction -> CosineDistance] Will give you a measure of how close ...


5

You should take a look at: New in the Wolfram Language: GrammarRules Programmable Linguistic Interface Sequence Alignment & Comparison You can also use Machine Learning to help classify strings. Here is how to train a classifier to understand if a string talks about a cat a or a dog. cat = ToLowerCase[Import["http://en.wikipedia.org/wiki/Cat"]]; ...


5

I am not sure if this is the best way of doing it, so the following is not exactly an answer to the question. Here is how I would do it assuming that I have padding. 0 Preparation data={1,2,3};(*some test data*) (*exporting to some files*) Table[Export["myData" <> IntegerString[i, 10, 5] <> ".txt", data], {i,10}]; (*and deleting one to make it ...


4

I would love this to be uniform for both integer and rational numbers a2 = {2/3, 4/5, 9/7, 3/7, 1.5, 3, 1/9}; StringTrim@StringJoin[" " <> ToString[#, InputForm] & /@ a2] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) Row[ToString[#, InputForm] & /@ a2, " "] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) StringReplace[ToString[a, InputForm], {"{" | "}" -> ...


3

Have a look at the documentation for CSV. The first issue you have is that your file extension is .txt so Mma imports it as text file instead of a CSV file. Your second issue is that "Table" is not a supported element for either CSV or TXT so I think it is just being ignored. Even though your file does not have the .csv file type you can still tell Mma ...


3

Or StringTake[ToString[a, FormatType -> InputForm], {2, -2}] The inelegant use of StringTake strips off the leading and trailing brackets.


3

StringTrim["ahfiehfke$jfwfjejf0", "$" ~~ __]; StringDelete["ahfiehfke$jfwfjejf0", "$" ~~ __]; (*V10.1*)


3

How about this? SelectFirst[ln, StringMatchQ[#, "tat" ~~ __] &] Admittedly my method is not as elegant as others, but it's fast if your ln and nm are large: ln = ConstantArray[t, 10^5] /. t :> StringJoin@RandomSample[CharacterRange["A", "z"], 10]; nm = StringTake[#, 3] & /@ ln; The following Timing[SelectFirst[ln, StringMatchQ[#, nm[[1]] ~~ ...


2

You can try using Row to layout the text, and use Spacer command to adjust the spacing as needed Plot[Sin[x], {x, -Pi, Pi}, PlotLabel -> Text@Style[ TraditionalForm[ Row[{"MLS", Spacer[5], Subscript[u, 2], Spacer[20], OverTilde[\[Eta]] , Spacer[5], "= 0.048%"}]], FontSize -> 18], ImagePadding -> 20] Or, the way I would ...


2

InputForm[ ToString@StringForm["SomeText=`` as well as OtherText=``.", "textA", "textB"]] "SomeText=textA as well as OtherText=textB." If you have version 10 you might want to try StringTemplate StringTemplate["SomeText=`` as well as OtherText=``."]["textA", "textB"] "SomeText=textA as well as OtherText=textB."


2

lst = {"1", "2", "3", "4", "5", "6"}; Developer`PartitionMap[StringTrim[ToString@#, "{" | "}"] &, lst, 3] (* {"1, 2, 3", "4, 5, 6"} *)


2

StringTake[ToString@#, {2, -2}] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}


1

The thought is DatePlus. split[x_Integer] := {{FromDigits@#[[1 ;; 4]], 1, 1}, FromDigits@#[[5 ;; -1]]} &@IntegerDigits[x] split[2003305] (* {{2003,1,1},305} *) DatePlus[split[2003305][[1]], 305] (* {2003,11,2} *) f = Block[{$DateStringFormat = {"Year", "Month", "Day"}, res}, res = split[#]; DatePlus[res[[1]], res[[2]] - 1]] ...


1

Your output is formatted wrongly, because what you gave to Style is not a series of things to style, but a multiplication of its elements. Just evaluate the arguments on their own in your notebook to see the effect: "MLS" Subscript[u, 2] OverTilde[\[Eta]] "=0.048%" (* "=0.048%" "MLS" \!\(\*OverscriptBox[\(\[Eta]\), \(~\)]\) Subscript[u, 2] *) To get what ...


1

This is very similar to your previous question and so is the solution: lst = {"1", "2", "3", "4", "5", "6"} ToString @ Row[#, ", "] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}


1

This gives exactly the output you wrote down: str1 = StringDrop[StringDrop[ToString@Partition[lst, 3][[1]], 1], -1] str2 = StringDrop[StringDrop[ToString@Partition[lst, 3][[2]], 1], -1] Could be made more elegant of course, and more flexible.


1

Assuming you always want strings with 3 items: Map[StringJoin @@ Riffle[#, ", "] &, Partition[Map[ToString, lst], 3]]


1

Terse: a = {1, 17, 2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; ToString @ Row[InputForm /@ a, " "] "1 17 2/3 4/5 9/7 3/7 1/7 1/9"


1

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; StringJoin@Cases[a, Rational[x_, y_] :> " "<>ToString[x] <> "/" <> ToString[y]] reply to comment: a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9, 5, 6, 99/10}; f[Rational[x_, y_]] := " " <> ToString[x] <> "/" <> ToString[y]; f[x_] := " " <> ToString[x]; StringJoin[f[#] & /@ a] ...


1

Pick[ln, Tr /@ StringPosition[nm, #], 1] &@"dow" Pick[ln, Tr /@ StringPosition[nm, #], 1] &@{"dow", _ ~~ "at"} Let's you use single and lists of targets, patterns (unlike straight pick)... assumptions about list correspondence apply, returns results in order of nm.


1

I don't quite understand your code, especially the key_/;key->val_ part, but the following code shall do your work: ToExpression@str //. key_String /; StringMatchQ[key, ___ ~~ "->" ~~ ___] :> ToExpression[key]



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