10
$\begingroup$

I like to delete the sublist that have the second element "" (or any other you can set as "-1" or "A")

In the next example, I would like to erase the first and third sublist. How can I do this with DeleteCases or a similar way?. Thinking in larger lists.

a = {
  {"A", "", 0, 23},
  {"B", "ber", 0, 23},
  {"C", "", 0, 23},
  {"D", "der", 0, 23},
  {"E", "eer", 0, 23}
  }
DeleteCases[a, a[[i, 2]] == ""]

====================== SOLVED in the comment of J.M.

$\endgroup$
3
  • 5
    $\begingroup$ DeleteCases[a, {_, "", __}]. $\endgroup$ Jul 26, 2016 at 15:18
  • $\begingroup$ @J.M. thank you, ok. The documentation is not much usefull for me. This forum is the best for learning. $\endgroup$
    – Mika Ike
    Jul 26, 2016 at 15:25
  • 6
    $\begingroup$ If you understood how that worked, consider writing an answer to your question. $\endgroup$ Jul 26, 2016 at 15:29

4 Answers 4

5
$\begingroup$

Use only Delete:

a = {{"A", "", 0, 23}, {"B", "ber", 0, 23}, {"C", "", 0, 23}, {"D", 
   "der", 0, 23}, {"E", "eer", 0, 23}}

$\left( \begin{array}{cccc} \text{A} & \text{} & 0 & 23 \\ \text{B} & \text{ber} & 0 & 23 \\ \text{C} & \text{} & 0 & 23 \\ \text{D} & \text{der} & 0 & 23 \\ \text{E} & \text{eer} & 0 & 23 \\ \end{array} \right)$

Delete[a, {{1}, {3}}]

$\left( \begin{array}{cccc} \text{B} & \text{ber} & 0 & 23 \\ \text{D} & \text{der} & 0 & 23 \\ \text{E} & \text{eer} & 0 & 23 \\ \end{array} \right)$

$\endgroup$
2
$\begingroup$
a = {{"A", "", 0, 23}, {"B", "ber", 0, 23}, {"C", "", 0, 23},
     {"D", "der", 0, 23}, {"E", "eer", 0, 23}};

Using Cases:

Cases[a, {_, Except[""], ___}]

(*{{"B", "ber", 0, 23}, {"D", "der", 0, 23}, {"E", "eer", 0, 23}}*)

Or using Pick:

Pick[#, Map[#[[2]] =!= "" &, #]] &@a

(*{{"B", "ber", 0, 23}, {"D", "der", 0, 23}, {"E", "eer", 0, 23}}*)
$\endgroup$
2
$\begingroup$
a = 
 {{"A", "", 0, 23}, {"B", "ber", 0, 23}, {"C", "", 0, 23}, 
  {"D", "der", 0, 23}, {"E", "eer", 0, 23}};

Using SequenceSplit (new in 11.3)

Catenate @ SequenceSplit[a, {{_, "", __}}]

{{"B", "ber", 0, 23}, {"D", "der", 0, 23}, {"E", "eer", 0, 23}}

$\endgroup$
2
$\begingroup$
a = {{"A", "", 0, 23}, {"B", "ber", 0, 23}, {"C", "", 0, 23}, {"D", 
    "der", 0, 23}, {"E", "eer", 0, 23}};

delpos = Position[a[[All, 2]], "A" | ""]

Delete[a, delpos]

or to add an Alternative to J.M.'s comment:

DeleteCases[a, {_, "" | "A", __}]

or

Select[a, FreeQ[#[[2]], "" | "A"] &]

{{"B", "ber", 0, 23}, {"D", "der", 0, 23}, {"E", "eer", 0, 23}}

$\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.