5
$\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$
2
  • 5
    $\begingroup$ Something like: SequenceReplace[list, {_String, s_String} -> s]? $\endgroup$
    – Carl Woll
    Commented Sep 22, 2018 at 4:17
  • $\begingroup$ @Call Woll Thanks! $\endgroup$
    – Jerry
    Commented Sep 22, 2018 at 4:24

5 Answers 5

6
$\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$
3
  • $\begingroup$ why SequenceReplace[list2, {_String?StringMatchQ[ DatePattern[{"Year", "Month", "Day"}]], s_String?StringMatchQ[DatePattern[{"Year", "Month", "Day"}]]} :> s] doesnot work? $\endgroup$
    – Jerry
    Commented Sep 22, 2018 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
    Commented Sep 22, 2018 at 5:52
  • $\begingroup$ I see, Thanks very much!:) $\endgroup$
    – Jerry
    Commented Sep 22, 2018 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$
2
$\begingroup$
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"};

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

Grabbing the @Carl Woll's dateStringQ definition and using SequenceCases:

patts1 = {s : {__String?dateStringQ} :> Last@s, s : {__?NumericQ} :> s};

patts2 = {s : {__String?dateStringQ} :> Last@s, s : {__String?LetterQ} :> s};

Flatten[Riffle[#1, #2] & @@ (SequenceCases[list1, #] & /@ patts1)]

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

Flatten[Riffle[#1, #2] & @@ (SequenceCases[list2, #] & /@ patts2)]

{"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$
1
$\begingroup$
la =
  {"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};

lb =
  {"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 SequenceSplit (new in 11.3)

foo = Flatten @ SequenceSplit[#, x : {__String?(StringLength[#] > 1 &)} :> Last[x]] &;

foo @ la

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

foo @ lb

{"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$
1
$\begingroup$
Clear["Global`*"];

alist = {"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};

blist = {"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"};

f = SequenceReplace[#, { _String?(DateObjectQ@*DateObject), 
     t_String?(DateObjectQ@*DateObject)} :> t] &;

f /@ {alist, blist}

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}, {"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 and acknowledge you have read our privacy policy.

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