Delete strings: {name, John, John Doe, Doe} to {name, John Doe} Announcing the arrival of...
Why is this method for solving linear equations systems using determinants works?
Split coins into combinations of different denominations
Would reducing the reference voltage of an ADC have any effect on accuracy?
Does the set of sets which are elements of every set exist?
Second order approximation of the loss function (Deep learning book, 7.33)
Need of separate security plugins for both root and subfolder sites Wordpress?
Protagonist's race is hidden - should I reveal it?
How would this chord from "Rocket Man" be analyzed?
"Rubric" as meaning "signature" or "personal mark" -- is this accepted usage?
What to do with someone that cheated their way through university and a PhD program?
What is it called when you ride around on your front wheel?
Map material from china not allowed to leave the country
Passing args from the bash script to the function in the script
All ASCII characters with a given bit count
How to keep bees out of canned beverages?
My admission is revoked after accepting the admission offer
Will I lose my paid in full property
The art of proof summarizing. Are there known rules, or is it a purely common sense matter?
Additive group of local rings
What do you call the part of a novel that is not dialog?
Does Mathematica have an implementation of the Poisson Binomial Distribution?
std::is_constructible on incomplete types
Multiple fireplaces in an apartment building?
Does Feeblemind produce an ongoing magical effect that can be dispelled?
Delete strings: {name, John, John Doe, Doe} to {name, John Doe}
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Highlighting text with StringReplacePart but also using Style, SubscriptDateList's aggressive interpretation of TimeML duration codesUsing NumberString, DigitCharacter, or a similar directive to find all string representations of integers in a stringImporting strings in MathematicaFind names similar to a given name in a large data setInput a non-string input — output as a stringHow to collect elements in one list with the constraints provided in a second listUsing n (new line) as delimiter in StringSplitConvert variable name to filenamePartitioning string into longest substrings of given characters
$begingroup$
I am not very familiar with coding and I am trying to solve the following:
Input:
{name, John, John Doe, Doe}
Output:
{name, John Doe}
So, it should delete {John, Doe} only if there is a string which contains already both of them.
I would be very grateful for any help! Thank you very much!
string-manipulation
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am not very familiar with coding and I am trying to solve the following:
Input:
{name, John, John Doe, Doe}
Output:
{name, John Doe}
So, it should delete {John, Doe} only if there is a string which contains already both of them.
I would be very grateful for any help! Thank you very much!
string-manipulation
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago
add a comment |
$begingroup$
I am not very familiar with coding and I am trying to solve the following:
Input:
{name, John, John Doe, Doe}
Output:
{name, John Doe}
So, it should delete {John, Doe} only if there is a string which contains already both of them.
I would be very grateful for any help! Thank you very much!
string-manipulation
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I am not very familiar with coding and I am trying to solve the following:
Input:
{name, John, John Doe, Doe}
Output:
{name, John Doe}
So, it should delete {John, Doe} only if there is a string which contains already both of them.
I would be very grateful for any help! Thank you very much!
string-manipulation
string-manipulation
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 6 hours ago
user64494
3,61411122
3,61411122
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 15 hours ago
Coffee_09Coffee_09
312
312
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Coffee_09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago
add a comment |
$begingroup$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago
$begingroup$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago
$begingroup$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago
add a comment |
4 Answers
4
active
oldest
votes
$begingroup$
strings = {"name", "John", "John Doe", "Doe"};
Pick[strings,
BitAnd @@ (IdentityMatrix[Length[strings]] +
Boole[Outer[StringFreeQ, strings, strings]]), 1]
{"name", "John Doe"}
Or equivalently:
Pick[strings,
MapIndexed[StringFreeQ[Delete[strings, #2], #] &, strings],
ConstantArray[True, Length[strings] - 1]]
$endgroup$
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
I'll assume that the list will contain strings, not variables:
L = {"name", "John", "John Doe", "Doe"};
Delete element sequences like {"John", "Doe"} that are together contained in a single string "John Doe":
ClearAll[f];
SetAttributes[f, Orderless];
(f[Sequence @@ #, x___] = f[x]) & /@ Select[StringSplit /@ L, Length[#] > 1 &];
f[x___] = {x};
f @@ L
{"John Doe", "name"}
This method does not keep the original sorting order of the list. If this order needs to be kept, we can replace the last line with
SortBy[f @@ L, Position[L, #][[1, 1]] &]
{"name", "John Doe"}
$endgroup$
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching onOrderlessfunctions.
$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
# /. Flatten[Cases[#,
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]
{"name", "John Doe"}
$endgroup$
add a comment |
$begingroup$
If you have exact matches this is as short as it gonna get:
DeleteCases[{"name","John","John Doe","Doe"},"John"|"Doe"]
{name,John Doe}
$endgroup$
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
});
}
});
Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f196906%2fdelete-strings-name-john-john-doe-doe-to-name-john-doe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
strings = {"name", "John", "John Doe", "Doe"};
Pick[strings,
BitAnd @@ (IdentityMatrix[Length[strings]] +
Boole[Outer[StringFreeQ, strings, strings]]), 1]
{"name", "John Doe"}
Or equivalently:
Pick[strings,
MapIndexed[StringFreeQ[Delete[strings, #2], #] &, strings],
ConstantArray[True, Length[strings] - 1]]
$endgroup$
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
strings = {"name", "John", "John Doe", "Doe"};
Pick[strings,
BitAnd @@ (IdentityMatrix[Length[strings]] +
Boole[Outer[StringFreeQ, strings, strings]]), 1]
{"name", "John Doe"}
Or equivalently:
Pick[strings,
MapIndexed[StringFreeQ[Delete[strings, #2], #] &, strings],
ConstantArray[True, Length[strings] - 1]]
$endgroup$
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
strings = {"name", "John", "John Doe", "Doe"};
Pick[strings,
BitAnd @@ (IdentityMatrix[Length[strings]] +
Boole[Outer[StringFreeQ, strings, strings]]), 1]
{"name", "John Doe"}
Or equivalently:
Pick[strings,
MapIndexed[StringFreeQ[Delete[strings, #2], #] &, strings],
ConstantArray[True, Length[strings] - 1]]
$endgroup$
strings = {"name", "John", "John Doe", "Doe"};
Pick[strings,
BitAnd @@ (IdentityMatrix[Length[strings]] +
Boole[Outer[StringFreeQ, strings, strings]]), 1]
{"name", "John Doe"}
Or equivalently:
Pick[strings,
MapIndexed[StringFreeQ[Delete[strings, #2], #] &, strings],
ConstantArray[True, Length[strings] - 1]]
edited 13 hours ago
answered 14 hours ago
CoolwaterCoolwater
15.5k32553
15.5k32553
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
$begingroup$
Great! thank you very much for your help, Coolwater!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
I'll assume that the list will contain strings, not variables:
L = {"name", "John", "John Doe", "Doe"};
Delete element sequences like {"John", "Doe"} that are together contained in a single string "John Doe":
ClearAll[f];
SetAttributes[f, Orderless];
(f[Sequence @@ #, x___] = f[x]) & /@ Select[StringSplit /@ L, Length[#] > 1 &];
f[x___] = {x};
f @@ L
{"John Doe", "name"}
This method does not keep the original sorting order of the list. If this order needs to be kept, we can replace the last line with
SortBy[f @@ L, Position[L, #][[1, 1]] &]
{"name", "John Doe"}
$endgroup$
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching onOrderlessfunctions.
$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
I'll assume that the list will contain strings, not variables:
L = {"name", "John", "John Doe", "Doe"};
Delete element sequences like {"John", "Doe"} that are together contained in a single string "John Doe":
ClearAll[f];
SetAttributes[f, Orderless];
(f[Sequence @@ #, x___] = f[x]) & /@ Select[StringSplit /@ L, Length[#] > 1 &];
f[x___] = {x};
f @@ L
{"John Doe", "name"}
This method does not keep the original sorting order of the list. If this order needs to be kept, we can replace the last line with
SortBy[f @@ L, Position[L, #][[1, 1]] &]
{"name", "John Doe"}
$endgroup$
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching onOrderlessfunctions.
$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
I'll assume that the list will contain strings, not variables:
L = {"name", "John", "John Doe", "Doe"};
Delete element sequences like {"John", "Doe"} that are together contained in a single string "John Doe":
ClearAll[f];
SetAttributes[f, Orderless];
(f[Sequence @@ #, x___] = f[x]) & /@ Select[StringSplit /@ L, Length[#] > 1 &];
f[x___] = {x};
f @@ L
{"John Doe", "name"}
This method does not keep the original sorting order of the list. If this order needs to be kept, we can replace the last line with
SortBy[f @@ L, Position[L, #][[1, 1]] &]
{"name", "John Doe"}
$endgroup$
I'll assume that the list will contain strings, not variables:
L = {"name", "John", "John Doe", "Doe"};
Delete element sequences like {"John", "Doe"} that are together contained in a single string "John Doe":
ClearAll[f];
SetAttributes[f, Orderless];
(f[Sequence @@ #, x___] = f[x]) & /@ Select[StringSplit /@ L, Length[#] > 1 &];
f[x___] = {x};
f @@ L
{"John Doe", "name"}
This method does not keep the original sorting order of the list. If this order needs to be kept, we can replace the last line with
SortBy[f @@ L, Position[L, #][[1, 1]] &]
{"name", "John Doe"}
edited 14 hours ago
answered 14 hours ago
RomanRoman
6,03611132
6,03611132
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching onOrderlessfunctions.
$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching onOrderlessfunctions.
$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
Thank you very much, Roman! Is there a possibility to keep the original sorting order?
$endgroup$
– Coffee_09
14 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching on
Orderless functions.$endgroup$
– Roman
13 hours ago
$begingroup$
To be honest this is probably not the most efficient solution, but I wrote it down as an exercise in using pattern-matching on
Orderless functions.$endgroup$
– Roman
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
$begingroup$
Thank you very much for your solution, Roman!
$endgroup$
– Coffee_09
13 hours ago
add a comment |
$begingroup$
# /. Flatten[Cases[#,
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]
{"name", "John Doe"}
$endgroup$
add a comment |
$begingroup$
# /. Flatten[Cases[#,
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]
{"name", "John Doe"}
$endgroup$
add a comment |
$begingroup$
# /. Flatten[Cases[#,
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]
{"name", "John Doe"}
$endgroup$
# /. Flatten[Cases[#,
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]
{"name", "John Doe"}
answered 3 hours ago
halmirhalmir
10.8k2544
10.8k2544
add a comment |
add a comment |
$begingroup$
If you have exact matches this is as short as it gonna get:
DeleteCases[{"name","John","John Doe","Doe"},"John"|"Doe"]
{name,John Doe}
$endgroup$
add a comment |
$begingroup$
If you have exact matches this is as short as it gonna get:
DeleteCases[{"name","John","John Doe","Doe"},"John"|"Doe"]
{name,John Doe}
$endgroup$
add a comment |
$begingroup$
If you have exact matches this is as short as it gonna get:
DeleteCases[{"name","John","John Doe","Doe"},"John"|"Doe"]
{name,John Doe}
$endgroup$
If you have exact matches this is as short as it gonna get:
DeleteCases[{"name","John","John Doe","Doe"},"John"|"Doe"]
{name,John Doe}
answered 1 hour ago
Vitaliy KaurovVitaliy Kaurov
58.2k6163285
58.2k6163285
add a comment |
add a comment |
Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.
Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.
Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.
Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f196906%2fdelete-strings-name-john-john-doe-doe-to-name-john-doe%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$
Welcome to the forum! You should watch the TOUR ;-)
$endgroup$
– Vitaliy Kaurov
1 hour ago