5
$\begingroup$

I have a nested list of the following form

List1 = {{1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 6}, {1, 2, 4, 5, 
  6}, {1, 2, 3, 5, 6}, {1, 2, 3, 4, 6}, {1, 2, 3, 4, 5}, {1, 2, 5, 
  6}, {1, 2, 4, 6}, {1, 2, 4, 5}, {1, 2, 3, 6}, {1, 2, 3, 5}, {1, 2, 
  3, 4}, {5, 6}, {4, 6}, {4, 5}, {3, 6}, {3, 5}, {3, 4}, {2, 6}, {2, 
  5}, {2, 4}, {6}, {5}, {4}, {3}, {2}, {1}, {}, {3, 4, 5, 6}, {2, 4, 
  5, 6}, {2, 3, 5, 6}, {2, 3, 4, 6}, {2, 3, 4, 5}, {1, 4, 5, 6}, {1, 
  3, 5, 6}, {1, 3, 4, 6}, {1, 3, 4, 5}, {4, 5, 6}, {3, 5, 6}, {3, 4, 
  6}, {3, 4, 5}, {2, 5, 6}, {2, 4, 6}, {2, 4, 5}, {2, 3, 6}, {2, 3, 
  5}, {2, 3, 4}, {1, 5, 6}, {1, 4, 6}, {1, 4, 5}, {1, 3, 6}, {1, 3, 
  5}, {1, 3, 4}, {1, 2, 6}, {1, 2, 5}, {1, 2, 4}, {1, 2, 3}, {2, 
  3}, {1, 6}, {1, 5}, {1, 4}, {1, 3}, {1, 2}}

I want to select those sublists that have only 2 elements or 4 elements, I wanted to use

Select[List1,Length[#]==2||Length[#]==n-2&]

where n == 6.

However, I am getting only the set with 4 elements. Does anyone know why this is happening?

$\endgroup$

closed as off-topic by Szabolcs, march, MarcoB, user9660, WReach Jun 25 '16 at 4:30

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Szabolcs, WReach
  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – march, MarcoB, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ You must not have assigned n its value. Make sure that the symbol n is black in your code. If it's blue, it doesn't have a value yet. Make sure to add n = 6 before you run the Select expression and it will work fine. $\endgroup$ – MarcoB Jun 22 '16 at 15:26
  • 1
    $\begingroup$ With such questions, please show a short, self-contained example that reproduces the problem. Without knowing what you did exactly, people will just start guessing about what the problem may be. $\endgroup$ – Szabolcs Jun 22 '16 at 15:48
  • $\begingroup$ With[{n = 6}, Cases[List1, {Repeated[_, {n - 2}] | Repeated[_, {2}]}]] $\endgroup$ – march Jun 22 '16 at 15:48
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jun 22 '16 at 17:49
4
$\begingroup$

Example

Code

Select[list, Length @ # == 2  ||  Length @ # == 4 &]

or with n

n = 6;
Select[list, Length @ # == 2  ||  Length @ # == n - 2 &]

Output

{{1, 2, 5, 6}, {1, 2, 4, 6}, {1, 2, 4, 5}, {1, 2, 3, 6}, {1, 2, 3, 5}, {1, 2, 3, 4}, {5, 6}, {4, 6}, {4, 5}, {3, 6}, {3, 5}, {3, 4}, {2, 6}, {2, 5}, {2, 4}, {3, 4, 5, 6}, {2, 4, 5, 6}, {2, 3, 5, 6}, {2, 3, 4, 6}, {2, 3, 4, 5}, {1, 4, 5, 6}, {1, 3, 5, 6}, {1, 3, 4, 6}, {1, 3, 4, 5}, {2, 3}, {1, 6}, {1, 5}, {1, 4}, {1, 3}, {1, 2}}

Note: list is your original data

$\endgroup$
5
$\begingroup$

Never forget Pick for problems involving picking elements from a list.

data = 
  {{1, 2, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 6}, {1, 2, 4, 5, 6}, 
   {1, 2, 3, 5, 6}, {1, 2, 3, 4, 6}, {1, 2, 3, 4, 5}, {1, 2, 5, 6}, {1, 2, 4, 6}, 
   {1, 2, 4, 5}, {1, 2, 3, 6}, {1, 2, 3, 5}, {1, 2, 3, 4}, {5, 6}, {4, 6}, {4, 5}, 
   {3, 6}, {3, 5}, {3, 4}, {2, 6}, {2, 5}, {2, 4}, {6}, {5}, {4}, {3}, {2}, {1}, {}, 
   {3, 4, 5, 6}, {2, 4, 5, 6}, {2, 3, 5, 6}, {2, 3, 4, 6}, {2, 3, 4, 5}, 
   {1, 4, 5, 6}, {1, 3, 5, 6}, {1, 3, 4, 6}, {1, 3, 4, 5}, {4, 5,6}, {3, 5, 6}, 
   {3, 4, 6}, {3, 4, 5}, {2, 5, 6}, {2, 4, 6}, {2, 4, 5}, {2, 3, 6}, {2, 3, 5}, 
   {2, 3, 4}, {1, 5, 6}, {1, 4, 6}, {1, 4,5}, {1, 3, 6}, {1, 3, 5}, {1, 3, 4}, 
   {1, 2, 6}, {1, 2, 5}, {1, 2,4}, {1, 2, 3}, {2, 3}, {1, 6}, {1, 5}, 
   {1, 4}, {1, 3}, {1, 2}};

With[{n = 6}, Pick[data, Length /@ data, 2 | n - 2]]
{{1, 2, 5, 6}, {1, 2, 4, 6}, {1, 2, 4, 5}, {1, 2, 3, 6}, {1, 2, 3, 5}, 
 {1, 2, 3, 4}, {5, 6}, {4, 6}, {4, 5}, {3, 6}, {3, 5}, {3, 4}, {2, 6}, {2, 5}, 
 {2, 4}, {3, 4, 5, 6}, {2, 4, 5, 6}, {2, 3, 5, 6}, {2, 3, 4, 6}, {2, 3, 4, 5}, 
 {1, 4, 5, 6}, {1, 3, 5, 6}, {1, 3, 4, 6}, {1, 3, 4, 5}, {2, 3}, {1, 6}, {1, 5}, 
 {1, 4}, {1, 3}, {1, 2}}
$\endgroup$
4
$\begingroup$

Just for our unanswered rate:

With[{n = 6}, Select[List1, Length[#] == 2 || Length[#] == n-2 &]]

With[{n = 6}, Pick[List1, Length/@List1, 2 | n-2]]

Try With: it will always help if you want to plug something in an expression fast.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.