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












5












$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!










share|improve this question









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
















5












$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!










share|improve this question









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














5












5








5





$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!










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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


















  • $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










4 Answers
4






active

oldest

votes


















4












$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]]





share|improve this answer











$endgroup$













  • $begingroup$
    Great! thank you very much for your help, Coolwater!
    $endgroup$
    – Coffee_09
    13 hours ago



















3












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







share|improve this answer











$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 on Orderless functions.
    $endgroup$
    – Roman
    13 hours ago










  • $begingroup$
    Thank you very much for your solution, Roman!
    $endgroup$
    – Coffee_09
    13 hours ago



















0












$begingroup$

# /. Flatten[Cases[#, 
x_ /; StringContainsQ[x, " "] :>
Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
"John Doe", "Doe"}]



{"name", "John Doe"}







share|improve this answer









$endgroup$





















    0












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







    share|improve this answer









    $endgroup$














      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.










      draft saved

      draft discarded


















      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









      4












      $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]]





      share|improve this answer











      $endgroup$













      • $begingroup$
        Great! thank you very much for your help, Coolwater!
        $endgroup$
        – Coffee_09
        13 hours ago
















      4












      $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]]





      share|improve this answer











      $endgroup$













      • $begingroup$
        Great! thank you very much for your help, Coolwater!
        $endgroup$
        – Coffee_09
        13 hours ago














      4












      4








      4





      $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]]





      share|improve this answer











      $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]]






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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


















      • $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











      3












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







      share|improve this answer











      $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 on Orderless functions.
        $endgroup$
        – Roman
        13 hours ago










      • $begingroup$
        Thank you very much for your solution, Roman!
        $endgroup$
        – Coffee_09
        13 hours ago
















      3












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







      share|improve this answer











      $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 on Orderless functions.
        $endgroup$
        – Roman
        13 hours ago










      • $begingroup$
        Thank you very much for your solution, Roman!
        $endgroup$
        – Coffee_09
        13 hours ago














      3












      3








      3





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







      share|improve this answer











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








      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 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, 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$
        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











      0












      $begingroup$

      # /. Flatten[Cases[#, 
      x_ /; StringContainsQ[x, " "] :>
      Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
      "John Doe", "Doe"}]



      {"name", "John Doe"}







      share|improve this answer









      $endgroup$


















        0












        $begingroup$

        # /. Flatten[Cases[#, 
        x_ /; StringContainsQ[x, " "] :>
        Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
        "John Doe", "Doe"}]



        {"name", "John Doe"}







        share|improve this answer









        $endgroup$
















          0












          0








          0





          $begingroup$

          # /. Flatten[Cases[#, 
          x_ /; StringContainsQ[x, " "] :>
          Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
          "John Doe", "Doe"}]



          {"name", "John Doe"}







          share|improve this answer









          $endgroup$



          # /. Flatten[Cases[#, 
          x_ /; StringContainsQ[x, " "] :>
          Thread[StringSplit[x] -> Nothing]]] &[{"name", "John",
          "John Doe", "Doe"}]



          {"name", "John Doe"}








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          halmirhalmir

          10.8k2544




          10.8k2544























              0












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







              share|improve this answer









              $endgroup$


















                0












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







                share|improve this answer









                $endgroup$
















                  0












                  0








                  0





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







                  share|improve this answer









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








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Vitaliy KaurovVitaliy Kaurov

                  58.2k6163285




                  58.2k6163285






















                      Coffee_09 is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Cannot install PyQt5 The Next CEO of Stack OverflowCannot install tcpreplay 3.4.4cannot...

                      Kapp-Putsch Acontecimentos | Outros artigos | Menu de navegação

                      Why did early computer designers eschew integers? The Next CEO of Stack OverflowWhat register...