Eliminate empty elements from a list with a specific pattern Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?List operation on specific elementsSelect elements from list with given headSelecting elements from a list with nullsReplace empty list elements with patternReplacing Non-Constant Elements from List with Patternselecting elements from a list with two numbersRemoving elements of a specific length from a listEliminate types of elements from the listListPlot3D with empty matrix elementsDelete a large pattern from one list
How is simplicity better than precision and clarity in prose?
How do I automatically answer y in bash script?
Need a suitable toxic chemical for a murder plot in my novel
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
Choo-choo! Word trains
Determine whether f is a function, an injection, a surjection
What do you call the holes in a flute?
Stopping real property loss from eroding embankment
Mortgage adviser recommends a longer term than necessary combined with overpayments
Jazz greats knew nothing of modes. Why are they used to improvise on standards?
Who can trigger ship-wide alerts in Star Trek?
What is the order of Mitzvot in Rambam's Sefer Hamitzvot?
Unable to start mainnet node docker container
Why does tar appear to skip file contents when output file is /dev/null?
Is it possible to ask for a hotel room without minibar/extra services?
What items from the Roman-age tech-level could be used to deter all creatures from entering a small area?
3 doors, three guards, one stone
Single author papers against my advisor's will?
Can I add database to AWS RDS MySQL without creating new instance?
What are the performance impacts of 'functional' Rust?
What computer would be fastest for Mathematica Home Edition?
When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?
Is there a documented rationale why the House Ways and Means chairman can demand tax info?
What can I do if my MacBook isn’t charging but already ran out?
Eliminate empty elements from a list with a specific pattern
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?List operation on specific elementsSelect elements from list with given headSelecting elements from a list with nullsReplace empty list elements with patternReplacing Non-Constant Elements from List with Patternselecting elements from a list with two numbersRemoving elements of a specific length from a listEliminate types of elements from the listListPlot3D with empty matrix elementsDelete a large pattern from one list
$begingroup$
I am really new in this patterns part of Mathematica. Basically what I need to do is eliminate null elements from a list but that has a specific name before the empty element. For example, my list is:
list="a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2"
and I need to obtain
list="a12-b11-1", "d33-c22-2"
The list was created using
list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <>
ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]],
i, 1, 4, j, 1, 4], 1]
and for some values it writtes because there is not a value equal to $0.5$. Until now I have been able to do it term by term as
list//."a11-b11-"-> Sequence[]
but the real list contains a lot of elements and could be almost impossible to do it that way to solve the problem. I think my main problem is that I am not sure how to specify the pattern search (something like " *-name " in gnu/linux). Is there a wise way to do this?. Thanks in advance.
list-manipulation filtering
$endgroup$
add a comment |
$begingroup$
I am really new in this patterns part of Mathematica. Basically what I need to do is eliminate null elements from a list but that has a specific name before the empty element. For example, my list is:
list="a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2"
and I need to obtain
list="a12-b11-1", "d33-c22-2"
The list was created using
list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <>
ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]],
i, 1, 4, j, 1, 4], 1]
and for some values it writtes because there is not a value equal to $0.5$. Until now I have been able to do it term by term as
list//."a11-b11-"-> Sequence[]
but the real list contains a lot of elements and could be almost impossible to do it that way to solve the problem. I think my main problem is that I am not sure how to specify the pattern search (something like " *-name " in gnu/linux). Is there a wise way to do this?. Thanks in advance.
list-manipulation filtering
$endgroup$
$begingroup$
Have a look atDeleteCases
andStringMatchQ
orStringContainsQ
.
$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in theint
function, and then construct strings only from the remaining ones.
$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09
add a comment |
$begingroup$
I am really new in this patterns part of Mathematica. Basically what I need to do is eliminate null elements from a list but that has a specific name before the empty element. For example, my list is:
list="a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2"
and I need to obtain
list="a12-b11-1", "d33-c22-2"
The list was created using
list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <>
ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]],
i, 1, 4, j, 1, 4], 1]
and for some values it writtes because there is not a value equal to $0.5$. Until now I have been able to do it term by term as
list//."a11-b11-"-> Sequence[]
but the real list contains a lot of elements and could be almost impossible to do it that way to solve the problem. I think my main problem is that I am not sure how to specify the pattern search (something like " *-name " in gnu/linux). Is there a wise way to do this?. Thanks in advance.
list-manipulation filtering
$endgroup$
I am really new in this patterns part of Mathematica. Basically what I need to do is eliminate null elements from a list but that has a specific name before the empty element. For example, my list is:
list="a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2"
and I need to obtain
list="a12-b11-1", "d33-c22-2"
The list was created using
list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <>
ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]],
i, 1, 4, j, 1, 4], 1]
and for some values it writtes because there is not a value equal to $0.5$. Until now I have been able to do it term by term as
list//."a11-b11-"-> Sequence[]
but the real list contains a lot of elements and could be almost impossible to do it that way to solve the problem. I think my main problem is that I am not sure how to specify the pattern search (something like " *-name " in gnu/linux). Is there a wise way to do this?. Thanks in advance.
list-manipulation filtering
list-manipulation filtering
edited Apr 8 at 19:43
Roman
5,29511131
5,29511131
asked Apr 8 at 14:13
morsmors
716
716
$begingroup$
Have a look atDeleteCases
andStringMatchQ
orStringContainsQ
.
$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in theint
function, and then construct strings only from the remaining ones.
$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09
add a comment |
$begingroup$
Have a look atDeleteCases
andStringMatchQ
orStringContainsQ
.
$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in theint
function, and then construct strings only from the remaining ones.
$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09
$begingroup$
Have a look at
DeleteCases
and StringMatchQ
or StringContainsQ
.$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
Have a look at
DeleteCases
and StringMatchQ
or StringContainsQ
.$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in the
int
function, and then construct strings only from the remaining ones.$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in the
int
function, and then construct strings only from the remaining ones.$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
If the list elements are strings, as it appears after your comment, you can use Select
with a string pattern:
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringMatchQ[__ ~~ ""]]
"a12-b11-1", "d33-c22-2"
You could also Select
before making the strings:
L = DeleteCases[
Flatten[
Table[
namea[[i]], nameb[[j]], Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1],
i, 4, j, 4],
1],
_, _, ];
and then make these into strings:
StringRiffle[ToString /@ #, "-"] & /@ L
I can't check this because you didn't supply functioning code.
$endgroup$
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
add a comment |
$begingroup$
In 10.1, two functions were added to handle a pair of very common cases: StringStartsQ
and StringEndsQ
which return True
if the string matches a pattern at the beginning or end, respectively. So, while Roman's answer gives you the full general form, most of the pattern can be eliminated by using
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringEndsQ[""]]
instead.
$endgroup$
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194815%2feliminate-empty-elements-from-a-list-with-a-specific-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If the list elements are strings, as it appears after your comment, you can use Select
with a string pattern:
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringMatchQ[__ ~~ ""]]
"a12-b11-1", "d33-c22-2"
You could also Select
before making the strings:
L = DeleteCases[
Flatten[
Table[
namea[[i]], nameb[[j]], Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1],
i, 4, j, 4],
1],
_, _, ];
and then make these into strings:
StringRiffle[ToString /@ #, "-"] & /@ L
I can't check this because you didn't supply functioning code.
$endgroup$
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
add a comment |
$begingroup$
If the list elements are strings, as it appears after your comment, you can use Select
with a string pattern:
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringMatchQ[__ ~~ ""]]
"a12-b11-1", "d33-c22-2"
You could also Select
before making the strings:
L = DeleteCases[
Flatten[
Table[
namea[[i]], nameb[[j]], Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1],
i, 4, j, 4],
1],
_, _, ];
and then make these into strings:
StringRiffle[ToString /@ #, "-"] & /@ L
I can't check this because you didn't supply functioning code.
$endgroup$
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
add a comment |
$begingroup$
If the list elements are strings, as it appears after your comment, you can use Select
with a string pattern:
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringMatchQ[__ ~~ ""]]
"a12-b11-1", "d33-c22-2"
You could also Select
before making the strings:
L = DeleteCases[
Flatten[
Table[
namea[[i]], nameb[[j]], Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1],
i, 4, j, 4],
1],
_, _, ];
and then make these into strings:
StringRiffle[ToString /@ #, "-"] & /@ L
I can't check this because you didn't supply functioning code.
$endgroup$
If the list elements are strings, as it appears after your comment, you can use Select
with a string pattern:
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringMatchQ[__ ~~ ""]]
"a12-b11-1", "d33-c22-2"
You could also Select
before making the strings:
L = DeleteCases[
Flatten[
Table[
namea[[i]], nameb[[j]], Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1],
i, 4, j, 4],
1],
_, _, ];
and then make these into strings:
StringRiffle[ToString /@ #, "-"] & /@ L
I can't check this because you didn't supply functioning code.
edited Apr 8 at 18:44
answered Apr 8 at 14:37
RomanRoman
5,29511131
5,29511131
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
add a comment |
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you, I forgot to specify the way the list is created. I create the list as 'list = Flatten[Table[ ToString[namea[[i]] <> "-" <> nameb[[j]] <> "-" <> ToString[ Select[int[i, j, 0.5], Abs[#] == 0.5 &, 1] ]], i, 1, 4, j, 1, 4], 1] '
$endgroup$
– mors
Apr 8 at 14:39
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
$begingroup$
Thank you. The second part of your answer was really helpfull.
$endgroup$
– mors
Apr 11 at 11:49
add a comment |
$begingroup$
In 10.1, two functions were added to handle a pair of very common cases: StringStartsQ
and StringEndsQ
which return True
if the string matches a pattern at the beginning or end, respectively. So, while Roman's answer gives you the full general form, most of the pattern can be eliminated by using
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringEndsQ[""]]
instead.
$endgroup$
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
add a comment |
$begingroup$
In 10.1, two functions were added to handle a pair of very common cases: StringStartsQ
and StringEndsQ
which return True
if the string matches a pattern at the beginning or end, respectively. So, while Roman's answer gives you the full general form, most of the pattern can be eliminated by using
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringEndsQ[""]]
instead.
$endgroup$
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
add a comment |
$begingroup$
In 10.1, two functions were added to handle a pair of very common cases: StringStartsQ
and StringEndsQ
which return True
if the string matches a pattern at the beginning or end, respectively. So, while Roman's answer gives you the full general form, most of the pattern can be eliminated by using
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringEndsQ[""]]
instead.
$endgroup$
In 10.1, two functions were added to handle a pair of very common cases: StringStartsQ
and StringEndsQ
which return True
if the string matches a pattern at the beginning or end, respectively. So, while Roman's answer gives you the full general form, most of the pattern can be eliminated by using
list = "a11-b11-", "a12-b11-1", "c11-d22-", "d33-c22-2";
Select[list, Not@*StringEndsQ[""]]
instead.
answered Apr 8 at 20:44
rcollyerrcollyer
28.7k674166
28.7k674166
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
add a comment |
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
$begingroup$
Than you, it looks really useful, I will try to implement it.
$endgroup$
– mors
Apr 11 at 11:50
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194815%2feliminate-empty-elements-from-a-list-with-a-specific-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Have a look at
DeleteCases
andStringMatchQ
orStringContainsQ
.$endgroup$
– b.gatessucks
Apr 8 at 14:31
$begingroup$
I think it would be more efficient to first filter out the unwanted cases in the
int
function, and then construct strings only from the remaining ones.$endgroup$
– Roman
Apr 8 at 15:01
$begingroup$
@b.gatessucks Thank you, I will look those option in Mathematica.
$endgroup$
– mors
Apr 8 at 15:07
$begingroup$
@Roman You are right, but I am new ih this cases stuf in Mathematica and I did no know how to do it when I created the list.
$endgroup$
– mors
Apr 8 at 15:09