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

$\endgroup$
4
  • $\begingroup$ Have a look at DeleteCases and StringMatchQ or StringContainsQ. $\endgroup$ Commented Apr 8, 2019 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$ Commented Apr 8, 2019 at 15:01
  • $\begingroup$ @b.gatessucks Thank you, I will look those option in Mathematica. $\endgroup$ Commented Apr 8, 2019 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$ Commented Apr 8, 2019 at 15:09

2 Answers 2

7
$\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$
2
  • $\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$ Commented Apr 8, 2019 at 14:39
  • $\begingroup$ Thank you. The second part of your answer was really helpfull. $\endgroup$ Commented Apr 11, 2019 at 11:49
2
$\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$
1
  • $\begingroup$ Than you, it looks really useful, I will try to implement it. $\endgroup$ Commented Apr 11, 2019 at 11:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.