Hot answers tagged

12

For large lists, this should be snappy: Pick[Range@Length@dat, StringTake[dat, UpTo@4], "star"] and actually, taking advantage of listability, Pick[Range@Length@dat, StringMatchQ[dat, "star*"]] is a bit faster it seems...


10

Position[dat, _String?(StringMatchQ[#, "star" ~~ ___] &)] or Position[dat, _String?(StringMatchQ[#, "star*"] &)] or Position[dat, _?(StringMatchQ[#, "star*"] &), Heads -> False] or Position[StringMatchQ[dat, "star*"], True] (*thanks: @TomD *) {{1}, {7}, {13}, {19}} Alternatively, Pick[Range@Length@dat, StringMatchQ[#, "star*"] ...


8

One way: label[_ -> s_] := StringReplace[s, "#" ~~ n__ ~~ ":" ~~ __ :> n] GatherBy[lis, label] (* Out: {{"#49:5" -> "#61:7", "#49:6" -> "#61:8", "#49:7" -> "#61:10", "#49:8" -> "#61:9"}, {"#49:5" -> "#62:5", "#49:6" -> "#62:4", "#49:7" -> "#62:3", "#49:8" -> "#62:6"}} *)


8

bin = 1011011; words = (bin // IntegerDigits) /. {1 -> "one", 0 -> "zero"}; (str = StringJoin[Riffle[words, " "]]) // InputForm (* "one zero one one zero one one" *) Speak[str]


5

Just for another approach (based on grouping by "#6n". Using GroupBy: lis = {"#49:5" -> "#61:7", "#49:5" -> "#62:5", "#49:6" -> "#61:8", "#49:6" -> "#62:4", "#49:7" -> "#61:10", "#49:7" -> "#62:3", "#49:8" -> "#61:9", "#49:8" -> "#62:6"}; gp = GroupBy[lis, StringTake[#[[2]], {2, 3}] &, Rule @@@ # &]; v = Values[gp] ...


4

This looks like a job for associations: input = "article{a2,author={F1name L1name,F2name M2name,L2name}, title={The title of the work}, journal={The Journal2},year=1994, number=3,pages={204-223},month=6, note={An optional note},volume=2}" type = StringCases[input, Shortest[StartOfString ~~ t : __ ~~ "{"] :> t][[1]] "article" key = ...


4

It seems that a simple modification of your code will work. fileList[prefix_String, m_Integer, n_Integer] := With[{dash = "-"}, prefix <> dash <> ToString[#] <> dash <> ToString[n] <> ".asc"] & /@ Range[m] Then fileList["D:\\20160615\\20160615", 5, 1] gives {"D:\\20160615\\20160615-1-1.asc", "D:\\20160615\...


4

There are two differences between Write and WriteString: Write adds a newline after all expressions, WriteString does not. Write uses the FormatType of the output stream (default InputForm), WriteString uses OutputForm. WriteString does not require its argument to be a string. Therefore, the simplest fix is to just change the second Write to WriteString....


4

Your solution doesn't seem to work when there are duplicates. Anyway, could you test the performance of the following method? example = RandomChoice[{"Clothing", "Clothing/Accessories", "Clothing/Accessories/Belts", "Watch", "Watches/Accessories", "Watches/Accessories/Pocket Watch Chains" }, 15000 ]; Split[Sort@example, StringStartsQ[#2, #] &...


4

Get the relation graph of your example graph = SimpleGraph[RelationGraph[StringStartsQ, example], VertexLabels -> "Name"] Get the end of the vertex in every weaky connected graph First@*TopologicalSort/@WeaklyConnectedGraphComponents[graph] {"Clothing/Accessories/Belts", "Watches/Accessories/Pocket Watch Chains"} Hope this can help. :)


4

ImportString[ StringReplace[ "{6 7 9 51 64 68 79 102 116 128 148 155 184 202 273 283 292 1 7 8 \ 10 52 65 69 80 103 117 129 149 156 185 203 274 284 293 2 8 9 11 53 66 \ 70 81 104 118 130 150 157 186 204 275 285 294 3 9 10 12 54 67 71 82 \ 105 119 131 151 158 187 205 276 286 295 4 10 11 13 55 68 72 83 106 \ 120 132 152 159 188 206 277 287 296 5 11 12 14 56 ...


3

Since space is implicit Times similar methods to Convert head Times to List can be applied: Block[{Times = List}, ToExpression @ "{1 2 3 4 5 6 7 8}"] // First {1, 2, 3, 4, 5, 6, 7, 8} Though in this case a much shorter method works too: ToHeldExpression[expression] ~Level~ {3} {1, 2, 3, 4, 5, 6, 7, 8}


3

I don't think that StringMatchQ["x", Except[{"*"}]] works as expected as well as StringMatchQ["x", Except["*"]]: StringMatchQ["x", Except[{"*"}]] StringMatchQ["x", Except["*"]] True True The string pattern "*" is an abbreviated string pattern consisted from the only metacharacter * which corresponds to zero or more characters according to the first ...


3

EDIT: My original answer, while quite a bit quicker than the current answer, dropped some words that should not be dropped, as does the other current answer, e.g.. {"pa","paperweight"} fails on both (unless that is the intent of the OP, which I doubt). This led me to the following, which does not suffer from the problem and is also quite a bit quicker yet: ...


3

To further simplify the old code and make it fully robust (since among other things VectorQ[{}, NumberQ] returns True): ValidCASNumberQ[n_String] := With[{l = StringCases[n, x : DigitCharacter :> FromDigits[x]]}, 4 < Length[l] <= 10 && Mod[Reverse[Most[l]].Range[Length[l] - 1], 10] == Last[l]] StringCases[] guarantees that ...


3

You can replace your TemplateApply with either TemplateApply["Print[``];", i++] or StringJoin["Print[", ToString[i++], "];"] There is also a quick fix that is not recommended, which is to replace Module by Block.


2

a good use for the "new in 10" StringTemplate : Table[ StringTemplate["D:\\20160615\\20160615-`1`-`2`.asc"][i, j], {i, 3}, {j, 3}] incedentally, if you need to zero pad your numbers as they often occur in file names you can use this mess: Table[ StringTemplate[ "D:\\20160615\\20160615-<*TemplateExpression[IntegerString[`1`,10,2]]*>-`...


1

I found the problem is with my original data; there are some unexpected Line Feed characters in my data which were causing me to misinterpret the result of "Import" using type CSV. The comments by Yode and JasonB prompted me to look at the file with notepad++ so I could see the LF characters. Sorry for the confusion.


1

Here is a slightly modified version of the function from the linked package ValidCASNumberQ[n_Integer] := Block[ {d = IntegerDigits[n]}, Mod[Most[d].Range[Length[d] - 1, 1, -1], 10] == Last[d]] ValidCASNumberQ[n_String] := Block[{ l = StringCases[StringDelete[n, "CAS"], x : DigitCharacter :> FromDigits[x]], }, If[! VectorQ[l, NumberQ],...


1

casQ[CASRN_] /; MatchQ[CASRN, _String | _Integer]:= Module[{digits, count}, digits = If[IntegerQ@CASRN, IntegerDigits[CASRN], StringCases[CASRN, x : DigitCharacter :> FromDigits[x]]]; count = Length@digits; count > 4 && count < 11 && Last@digits == Mod[Reverse@Drop[digits, -1].Range[count - 1],...


1

For (6) you need to specify two patterns, one for each ordering: words = {"abcd", "aydx", "axdy", "adx"}; Select[ words, StringContainsQ[("x" ~~ ___ ~~ "y") | ("y" ~~ ___ ~~ "x")] ] (* Out: {"aydx", "axdy"} *) StringContainsQ, StringStartsQ and StringEndQ can be used for some of your patterns and may either give a performance boost or at the very ...


1

In order to perform the Manipulate that you are looking for we really only need three functions (see Feyre's comment). My interpretation is that you could use: kbA[θ_] := 6.80414/((θ/10 - 0.119974) Sqrt[-1.19974 + θ]) kbM[θ_] := 37037/(20.468 + 22.4478 θ + 8.2063 θ^2 + θ^3) cfun[kA_, kM_] := kA^(1/3) + kM^(2/3) - 8 (kA + kM)/100 Now we can define your ...


1

I use code from J.M comment, and it works perfectly text = {"რომელმან", "შექმნა", "სამყარო", "ძალითა", "მით", "ძლიერითა", "ზეგარდმო", "არსნი", "სულითა", "ყვნა", "ზეცით", "მონაბერითა", "ჩვენ", "კაცთა", "მოგვცა", "ქვეყანა", "გვაქვს", "უთვალავი", "ფერითა", "მისგან", "არს", "ყოვლი", "ხელმწიფე", "სახითა", "მის", "მიერითა"} LetterCounts[StringRiffle[text]] ...



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