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!

  • 4
    Something like: SequenceReplace[list, {_String, s_String} -> s]? – Carl Woll Sep 22 at 4:17
  • @Call Woll Thanks! – Jerry Sep 22 at 4:24
up vote 5 down vote accepted

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"}

  • why SequenceReplace[list2, {_String?StringMatchQ[ DatePattern[{"Year", "Month", "Day"}]], s_String?StringMatchQ[DatePattern[{"Year", "Month", "Day"}]]} :> s] doesnot work? – Jerry Sep 22 at 5:48
  • @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"}]]). – Carl Woll Sep 22 at 5:52
  • I see, Thanks very much!:) – Jerry Sep 22 at 6:00

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"}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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