How to remove adjacent date strings in a list?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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!
list-manipulation string-manipulation stringreplace
add a comment |Â
up vote
3
down vote
favorite
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!
list-manipulation string-manipulation stringreplace
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
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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!
list-manipulation string-manipulation stringreplace
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!
list-manipulation string-manipulation stringreplace
list-manipulation string-manipulation stringreplace
edited Sep 22 at 5:35
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,314725
2,314725
asked Sep 22 at 4:14
Jerry
35819
35819
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
add a comment |Â
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
4
4
Something like:
SequenceReplace[list, _String, s_String -> s]
?â Carl Woll
Sep 22 at 4:17
Something like:
SequenceReplace[list, _String, s_String -> s]
?â Carl Woll
Sep 22 at 4:17
@Call Woll Thanks!
â Jerry
Sep 22 at 4:24
@Call Woll Thanks!
â Jerry
Sep 22 at 4:24
add a comment |Â
2 Answers
2
active
oldest
votes
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"
whySequenceReplace[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
add a comment |Â
up vote
3
down vote
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["\d4-\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"
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"
whySequenceReplace[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
add a comment |Â
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"
whySequenceReplace[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
add a comment |Â
up vote
5
down vote
accepted
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"
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"
answered Sep 22 at 5:19
Carl Woll
59.4k278154
59.4k278154
whySequenceReplace[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
add a comment |Â
whySequenceReplace[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
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
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
@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
I see, Thanks very much!:)
â Jerry
Sep 22 at 6:00
add a comment |Â
up vote
3
down vote
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["\d4-\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"
add a comment |Â
up vote
3
down vote
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["\d4-\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"
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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["\d4-\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"
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["\d4-\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"
edited Sep 22 at 5:32
answered Sep 22 at 5:02
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,314725
2,314725
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182340%2fhow-to-remove-adjacent-date-strings-in-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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