3
$\begingroup$

Given a list as

list = {"2018-9-1",1,2,3,"2018-9-2","2018-9-3",4,5,6,"2018-9-5",7,8,9,"2018-9-6","2018-9-7",10,11,12,"2018-9-8",13,14,15,"2018-9-9",16,17,18}

or

list = {"2018-9-1","a","b","c","2018-9-2","2018-9-3","d","e","f","2018-9-5","h","i","j","2018-9-6","2018-9-7","k","l","m","2018-9-8","n","o","p","2018-9-9","q","r","s"}

how can adjacent date strings being pruned to get this result?

{"2018-9-1",1,2,3,"2018-9-3",4,5,6,"2018-9-5",7,8,9,"2018-9-7",10,11,12,"2018-9-8",13,14,15,"2018-9-9",16,17,18}

or

{"2018-9-1","a","b","c","2018-9-3","d","e","f","2018-9-5","h","i","j","2018-9-7","k","l","m","2018-9-8","n","o","p","2018-9-9","q","r","s"}

such that the function Partition could be applied properly. Thanks!

$\endgroup$
  • 4
    $\begingroup$ Something like: SequenceReplace[list, {_String, s_String} -> s]? $\endgroup$ – Carl Woll Sep 22 '18 at 4:17
  • $\begingroup$ @Call Woll Thanks! $\endgroup$ – Jerry Sep 22 '18 at 4:24
5
$\begingroup$

There are many ways to accomplish this. One way uses SequenceReplace. First define a dateStringQ predicate:

dateStringQ = StringMatchQ[DatePattern[{"Year","Month","Day"}]];

Your examples:

list1 = {
    "2018-9-1",1,2,3,"2018-9-2","2018-9-3",4,5,6,"2018-9-5",7,8,9,
    "2018-9-6","2018-9-7",10,11,12,"2018-9-8",13,14,15,"2018-9-9",16,17,18
};

list2 = {
    "2018-9-1","a","b","c","2018-9-2","2018-9-3","d","e","f","2018-9-5",
    "h","i","j","2018-9-6","2018-9-7","k","l","m","2018-9-8","n","o","p",
    "2018-9-9","q","r","s"
};

Using SequenceReplace:

SequenceReplace[
    list1,
    {_String?dateStringQ, s_String?dateStringQ} :> s
]

SequenceReplace[
    list2,
    {_String?dateStringQ, s_String?dateStringQ} :> s
]

{"2018-9-1", 1, 2, 3, "2018-9-3", 4, 5, 6, "2018-9-5", 7, 8, 9, "2018-9-7", 10, 11, 12, "2018-9-8", 13, 14, 15, "2018-9-9", 16, 17, 18}

{"2018-9-1", "a", "b", "c", "2018-9-3", "d", "e", "f", "2018-9-5", "h", "i", "j", "2018-9-7", "k", "l", "m", "2018-9-8", "n", "o", "p", "2018-9-9", "q", "r", "s"}

$\endgroup$
  • $\begingroup$ why SequenceReplace[list2, {_String?StringMatchQ[ DatePattern[{"Year", "Month", "Day"}]], s_String?StringMatchQ[DatePattern[{"Year", "Month", "Day"}]]} :> s] doesnot work? $\endgroup$ – Jerry Sep 22 '18 at 5:48
  • $\begingroup$ @Jerry The issue is precedence, _String?StringMatchQ[DataPattern[{"Year", "Month", "Day"}]] is parsed as (_String?StringMatchQ)[DatePattern[{"Year", "Month", "Day"}]] not _String?(StringMatchQ[DatePattern[{"Year", "Month", "Day"}]]). $\endgroup$ – Carl Woll Sep 22 '18 at 5:52
  • $\begingroup$ I see, Thanks very much!:) $\endgroup$ – Jerry Sep 22 '18 at 6:00
3
$\begingroup$

SequenceReplace is suitable, as @CarlWoll pointed out in the comment. But it needs further elaboration to work with both of your lists. Below is my version

stringPattern = RegularExpression["\\d{4}-\\d-\\d"];
stringMatchQ = If[StringQ[#], StringMatchQ[#, stringPattern], False] &;
rule = {a_?stringMatchQ, b_?stringMatchQ} :> b;

SequenceReplace[list1, rule]
SequenceReplace[list2, rule]

{"2018-9-1", 1, 2, 3, "2018-9-3", 4, 5, 6, "2018-9-5", 7, 8, 9, "2018-9-7", 10, 11, 12, "2018-9-8", 13, 14, 15, "2018-9-9", 16, 17, 18}

{"2018-9-1", "a", "b", "c", "2018-9-3", "d", "e", "f", "2018-9-5", "h", "i", "j", "2018-9-7", "k", "l", "m", "2018-9-8", "n", "o", "p", "2018-9-9", "q", "r", "s"}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.