5
$\begingroup$

I have a long list. I want to select elements so that three first of this element present two or three times. E.g. {12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10} . How can I select?

 list={{1, 1, 3, -1, 1, -3}, {1, 2, 4, -1, 2, -2}, {1, 3, 1, 1, -3, -1}, {1,
       4, 2, 1, -4, -1}, {1, 5, 3, 1, -5, -1}, {1, 6, 4, 1, -6, -1}, {1, 
      7, 5, 1, -7, -1}, {1, 8, 6, 1, -8, -1}, {1, 9, 7, 1, -9, -1}, {1, 
      10, 8, 1, -10, -1}, {1, 11, 9, 1, -11, -1}, {1, 12, 10, 
      1, -12, -1}, {1, 13, 11, 1, -13, -1}, {1, 14, 12, 1, -14, -1}, {1, 
      15, 13, 1, -15, -1}, {1, 16, 14, 1, -16, -1}, {1, 17, 15, 
      1, -17, -1}, {1, 18, 16, 1, -18, -1}, {1, 19, 17, 1, -19, -1}, {1, 
      20, 18, 1, -20, -1}, {1, 21, 19, 1, -21, -1}, {1, 22, 20, 
      1, -22, -1}, {1, 23, 21, 1, -23, -1}, {1, 24, 22, 1, -24, -1}, {1, 
      25, 23, 1, -25, -1}, {1, 26, 24, 1, -26, -1}, {1, 27, 25, 
      1, -27, -1}, {1, 28, 26, 1, -28, -1}, {1, 29, 27, 1, -29, -1}, {1, 
      30, 28, 1, -30, -1}, {2, 1, 4, -1, 1, -4}, {2, 3, 6, -1, 3, -2}, {2,
       4, 1, 2, -2, -1}, {2, 4, 9, -2, 1, -3}, {2, 6, 3, 2, -3, -1}, {2, 
      6, 10, -2, 2, -2}, {2, 8, 5, 2, -4, -1}, {2, 10, 7, 2, -5, -1}, {2, 
      12, 9, 2, -6, -1}, {2, 14, 11, 2, -7, -1}, {2, 16, 13, 
      2, -8, -1}, {2, 18, 15, 2, -9, -1}, {2, 20, 17, 2, -10, -1}, {2, 22,
       19, 2, -11, -1}, {2, 24, 21, 2, -12, -1}, {2, 26, 23, 
      2, -13, -1}, {2, 28, 25, 2, -14, -1}, {2, 30, 27, 2, -15, -1}, {3, 
      1, 1, -3, -1, 1}, {3, 1, 5, -1, 1, -5}, {3, 2, 6, -1, 2, -3}, {3, 3,
       3, 1, 1, 1}, {3, 4, 8, -1, 4, -2}, {3, 6, 2, 3, -2, -1}, {3, 7, 
      15, -3, 1, -3}, {3, 9, 5, 3, -3, -1}, {3, 10, 16, -3, 2, -2}, {3, 
      12, 8, 3, -4, -1}, {3, 15, 11, 3, -5, -1}, {3, 18, 14, 
      3, -6, -1}, {3, 21, 17, 3, -7, -1}, {3, 24, 20, 3, -8, -1}, {3, 27, 
      23, 3, -9, -1}, {3, 30, 26, 3, -10, -1}, {4, 1, 2, -2, -1, 2}, {4, 
      1, 6, -1, 1, -6}, {4, 2, 1, -4, -1, 1}, {4, 5, 6, 1, 1, 2}, {4, 5, 
      10, -1, 5, -2}, {4, 5, 12, -2, 1, -4}, {4, 8, 3, 4, -2, -1}, {4, 9, 
      2, 1, -3, -2}, {4, 9, 14, -2, 3, -2}, {4, 10, 21, -4, 1, -3}, {4, 
      12, 7, 4, -3, -1}, {4, 14, 22, -4, 2, -2}, {4, 16, 11, 
      4, -4, -1}, {4, 20, 15, 4, -5, -1}, {4, 24, 19, 4, -6, -1}, {4, 28, 
      23, 4, -7, -1}, {5, 1, 7, -1, 1, -7}, {5, 2, 8, -1, 2, -4}, {5, 3, 
      1, -5, -1, 1}, {5, 3, 9, -1, 3, -3}, {5, 6, 4, 1, 2, 1}, {5, 6, 
      12, -1, 6, -2}, {5, 7, 9, 1, 1, 3}, {5, 10, 4, 5, -2, -1}, {5, 12, 
      4, 1, -4, -2}, {5, 13, 27, -5, 1, -3}, {5, 15, 9, 5, -3, -1}, {5, 
      18, 28, -5, 2, -2}, {5, 20, 14, 5, -4, -1}, {5, 25, 19, 
      5, -5, -1}, {5, 30, 24, 5, -6, -1}, {6, 1, 8, -1, 1, -8}, {6, 2, 
      3, -2, -1, 3}, {6, 3, 2, -3, -1, 2}, {6, 4, 1, -6, -1, 1}, {6, 4, 5,
       2, 1, 1}, {6, 6, 15, -2, 1, -5}, {6, 7, 14, -1, 7, -2}, {6, 8, 
      15, -2, 2, -3}, {6, 9, 12, 1, 1, 4}, {6, 9, 20, -3, 1, -4}, {6, 10, 
      2, 2, -2, -2}, {6, 12, 5, 6, -2, -1}, {6, 12, 18, -2, 4, -2}, {6, 
      15, 6, 1, -5, -2}, {6, 15, 22, -3, 3, -2}, {6, 18, 11, 
      6, -3, -1}, {6, 24, 17, 6, -4, -1}, {6, 30, 23, 6, -5, -1}, {7, 1, 
      9, -1, 1, -9}, {7, 2, 10, -1, 2, -5}, {7, 4, 12, -1, 4, -3}, {7, 5, 
      1, -7, -1, 1}, {7, 8, 16, -1, 8, -2}, {7, 9, 5, 1, 3, 1}, {7, 10, 8,
       1, 2, 2}, {7, 11, 15, 1, 1, 5}, {7, 14, 6, 7, -2, -1}, {7, 15, 3, 
      1, -3, -3}, {7, 18, 8, 1, -6, -2}, {7, 21, 13, 7, -3, -1}, {7, 28, 
      20, 7, -4, -1}, {8, 1, 10, -1, 1, -10}, {8, 3, 4, -2, -1, 4}, {8, 3,
       12, -1, 3, -4}, {8, 5, 2, -4, -1, 2}, {8, 6, 1, -8, -1, 1}, {8, 7, 
      10, 2, 1, 2}, {8, 7, 18, -2, 1, -6}, {8, 9, 18, -1, 9, -2}, {8, 13, 
      18, 1, 1, 6}, {8, 13, 28, -4, 1, -4}, {8, 15, 6, 2, -3, -2}, {8, 15,
       22, -2, 5, -2}, {8, 16, 7, 8, -2, -1}, {8, 21, 10, 1, -7, -2}, {8, 
      21, 30, -4, 3, -2}, {8, 24, 15, 8, -3, -1}, {9, 1, 11, -1, 
      1, -11}, {9, 2, 4, -3, -2, 1}, {9, 2, 12, -1, 2, -6}, {9, 5, 
      3, -3, -1, 3}, {9, 5, 7, 3, 1, 1}, {9, 5, 15, -1, 5, -3}, {9, 7, 
      1, -9, -1, 1}, {9, 10, 20, -1, 10, -2}, {9, 11, 25, -3, 1, -5}, {9, 
      12, 6, 1, 4, 1}, {9, 14, 4, 3, -2, -2}, {9, 14, 12, 1, 2, 3}, {9, 
      14, 24, -3, 2, -3}, {9, 15, 21, 1, 1, 7}, {9, 18, 8, 9, -2, -1}, {9,
       20, 6, 1, -4, -3}, {9, 20, 28, -3, 4, -2}, {9, 24, 12, 
      1, -8, -2}, {9, 27, 17, 9, -3, -1}, {10, 1, 12, -1, 1, -12}, {10, 2,
       6, -2, -2, 2}, {10, 4, 5, -2, -1, 5}, {10, 7, 2, -5, -1, 2}, {10, 
      8, 1, -10, -1, 1}, {10, 8, 7, 2, 2, 1}, {10, 8, 21, -2, 1, -7}, {10,
       10, 15, 2, 1, 3}, {10, 10, 20, -2, 2, -4}, {10, 11, 22, -1, 
      11, -2}, {10, 12, 21, -2, 3, -3}, {10, 15, 10, 1, 3, 2}, {10, 16, 3,
       2, -2, -3}, {10, 17, 24, 1, 1, 8}, {10, 18, 26, -2, 6, -2}, {10, 
      20, 9, 10, -2, -1}, {10, 20, 10, 2, -4, -2}, {10, 21, 4, 
      1, -3, -4}, {10, 27, 14, 1, -9, -2}, {10, 30, 19, 10, -3, -1}, {11, 
      1, 13, -1, 1, -13}, {11, 2, 14, -1, 2, -7}, {11, 3, 15, -1, 
      3, -5}, {11, 4, 16, -1, 4, -4}, {11, 6, 18, -1, 6, -3}, {11, 9, 
      1, -11, -1, 1}, {11, 12, 24, -1, 12, -2}, {11, 15, 7, 1, 5, 1}, {11,
       18, 16, 1, 2, 4}, {11, 19, 27, 1, 1, 9}, {11, 22, 10, 
      11, -2, -1}, {11, 25, 9, 1, -5, -3}, {11, 30, 16, 1, -10, -2}, {12, 
      1, 14, -1, 1, -14}, {12, 4, 5, -4, -2, 1}, {12, 5, 6, -2, -1, 
      6}, {12, 6, 9, 4, 1, 1}, {12, 7, 4, -3, -1, 4}, {12, 8, 3, -4, -1, 
      3}, {12, 9, 2, -6, -1, 2}, {12, 9, 14, 3, 1, 2}, {12, 9, 24, -2, 
      1, -8}, {12, 10, 1, -12, -1, 1}, {12, 13, 20, 2, 1, 4}, {12, 13, 
      26, -1, 13, -2}, {12, 13, 30, -3, 1, -6}, {12, 18, 6, 
      4, -2, -2}, {12, 21, 10, 3, -3, -2}, {12, 21, 30, -2, 7, -2}, {12, 
      21, 30, 1, 1, 10}, {12, 24, 11, 12, -2, -1}, {12, 25, 14, 
      2, -5, -2}, {13, 1, 15, -1, 1, -15}, {13, 2, 16, -1, 2, -8}, {13, 7,
       21, -1, 7, -3}, {13, 11, 1, -13, -1, 1}, {13, 14, 28, -1, 
      14, -2}, {13, 18, 8, 1, 6, 1}, {13, 20, 12, 1, 4, 2}, {13, 21, 15, 
      1, 3, 3}, {13, 22, 20, 1, 2, 5}, {13, 26, 12, 13, -2, -1}, {13, 27, 
      5, 1, -3, -5}, {13, 28, 8, 1, -4, -4}, {13, 30, 12, 1, -6, -3}, {14,
       1, 16, -1, 1, -16}, {14, 3, 18, -1, 3, -6}, {14, 4, 9, -2, -2, 
      3}, {14, 5, 20, -1, 5, -4}, {14, 6, 7, -2, -1, 7}, {14, 10, 27, -2, 
      1, -9}, {14, 11, 2, -7, -1, 2}, {14, 12, 1, -14, -1, 1}, {14, 12, 9,
       2, 3, 1}, {14, 12, 25, -2, 2, -5}, {14, 14, 14, 2, 2, 2}, {14, 15, 
      30, -1, 15, -2}, {14, 16, 25, 2, 1, 5}, {14, 16, 27, -2, 
      4, -3}, {14, 22, 4, 2, -2, -4}, {14, 24, 9, 2, -3, -3}, {14, 28, 13,
       14, -2, -1}, {14, 30, 18, 2, -6, -2}, {15, 1, 17, -1, 1, -17}, {15,
       2, 18, -1, 2, -9}, {15, 3, 7, -3, -3, 1}, {15, 4, 20, -1, 
      4, -5}, {15, 6, 6, -5, -2, 1}, {15, 6, 8, -3, -2, 2}, {15, 7, 11, 5,
       1, 1}, {15, 8, 24, -1, 8, -3}, {15, 9, 5, -3, -1, 5}, {15, 10, 10, 
      3, 2, 1}, {15, 11, 3, -5, -1, 3}, {15, 13, 1, -15, -1, 1}, {15, 13, 
      21, 3, 1, 3}, {15, 21, 9, 1, 7, 1}, {15, 22, 6, 3, -2, -3}, {15, 22,
       8, 5, -2, -2}, {15, 26, 24, 1, 2, 6}, {15, 28, 16, 3, -4, -2}, {15,
       30, 14, 15, -2, -1}, {16, 1, 18, -1, 1, -18}, {16, 3, 10, -2, -3, 
      2}, {16, 7, 8, -2, -1, 8}, {16, 11, 4, -4, -1, 4}, {16, 11, 18, 4, 
      1, 2}, {16, 11, 30, -2, 1, -10}, {16, 13, 2, -8, -1, 2}, {16, 14, 
      1, -16, -1, 1}, {16, 15, 28, -2, 3, -4}, {16, 19, 30, 2, 1, 6}, {16,
       25, 14, 1, 5, 2}, {16, 27, 14, 4, -3, -2}, {16, 27, 20, 1, 3, 
      4}, {17, 1, 19, -1, 1, -19}, {17, 2, 20, -1, 2, -10}, {17, 3, 
      21, -1, 3, -7}, {17, 6, 24, -1, 6, -4}, {17, 9, 27, -1, 9, -3}, {17,
       15, 1, -17, -1, 1}, {17, 24, 10, 1, 8, 1}, {17, 28, 18, 1, 4, 
      3}, {17, 30, 28, 1, 2, 7}, {18, 1, 20, -1, 1, -20}, {18, 6, 
      12, -2, -2, 4}, {18, 8, 7, -6, -2, 1}, {18, 8, 9, -2, -1, 9}, {18, 
      8, 13, 6, 1, 1}, {18, 11, 6, -3, -1, 6}, {18, 14, 3, -6, -1, 
      3}, {18, 14, 30, -2, 2, -6}, {18, 15, 2, -9, -1, 2}, {18, 16, 
      1, -18, -1, 1}, {18, 16, 11, 2, 4, 1}, {18, 17, 28, 3, 1, 4}, {18, 
      20, 21, 2, 2, 3}, {18, 26, 10, 6, -2, -2}, {18, 28, 5, 
      2, -2, -5}, {19, 1, 21, -1, 1, -21}, {19, 2, 22, -1, 2, -11}, {19, 
      4, 24, -1, 4, -6}, {19, 5, 25, -1, 5, -5}, {19, 10, 30, -1, 
      10, -3}, {19, 17, 1, -19, -1, 1}, {19, 27, 11, 1, 9, 1}, {19, 30, 
      16, 1, 6, 2}, {20, 1, 22, -1, 1, -22}, {20, 3, 24, -1, 3, -8}, {20, 
      6, 9, -4, -3, 1}, {20, 7, 28, -1, 7, -4}, {20, 9, 10, -2, -1, 
      10}, {20, 10, 10, -4, -2, 2}, {20, 12, 13, 4, 2, 1}, {20, 13, 22, 5,
       1, 2}, {20, 14, 5, -4, -1, 5}, {20, 15, 4, -5, -1, 4}, {20, 16, 27,
       4, 1, 3}, {20, 17, 2, -10, -1, 2}, {20, 18, 1, -20, -1, 1}, {20, 
      21, 18, 2, 3, 2}, {20, 28, 9, 4, -2, -3}, {21, 1, 23, -1, 
      1, -23}, {21, 2, 24, -1, 2, -12}, {21, 4, 10, -3, -4, 1}, {21, 9, 
      15, 7, 1, 1}, {21, 10, 8, -7, -2, 1}, {21, 10, 12, -3, -2, 3}, {21, 
      13, 7, -3, -1, 7}, {21, 15, 13, 3, 3, 1}, {21, 17, 3, -7, -1, 
      3}, {21, 18, 20, 3, 2, 2}, {21, 19, 1, -21, -1, 1}, {21, 30, 8, 
      3, -2, -4}, {21, 30, 12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}, {22, 1,
       24, -1, 1, -24}, {22, 4, 14, -2, -4, 2}, {22, 6, 15, -2, -3, 
      3}, {22, 8, 15, -2, -2, 5}, {22, 10, 11, -2, -1, 11}, {22, 19, 
      2, -11, -1, 2}, {22, 20, 1, -22, -1, 1}, {22, 20, 13, 2, 5, 1}, {22,
       26, 28, 2, 2, 4}, {23, 1, 25, -1, 1, -25}, {23, 2, 26, -1, 
      2, -13}, {23, 3, 27, -1, 3, -9}, {23, 4, 28, -1, 4, -7}, {23, 6, 
      30, -1, 6, -5}, {23, 21, 1, -23, -1, 1}, {24, 1, 26, -1, 
      1, -26}, {24, 5, 30, -1, 5, -6}, {24, 9, 14, -3, -3, 2}, {24, 10, 
      17, 8, 1, 1}, {24, 11, 12, -2, -1, 12}, {24, 12, 9, -8, -2, 1}, {24,
       15, 8, -3, -1, 8}, {24, 15, 26, 6, 1, 2}, {24, 17, 6, -4, -1, 
      6}, {24, 19, 4, -6, -1, 4}, {24, 20, 3, -8, -1, 3}, {24, 21, 
      2, -12, -1, 2}, {24, 22, 1, -24, -1, 1}, {25, 1, 27, -1, 
      1, -27}, {25, 2, 28, -1, 2, -14}, {25, 9, 11, -5, -3, 1}, {25, 14, 
      12, -5, -2, 2}, {25, 14, 16, 5, 2, 1}, {25, 19, 5, -5, -1, 5}, {25, 
      23, 1, -25, -1, 1}, {26, 1, 28, -1, 1, -28}, {26, 3, 30, -1, 
      3, -10}, {26, 10, 18, -2, -2, 6}, {26, 12, 13, -2, -1, 13}, {26, 23,
       2, -13, -1, 2}, {26, 24, 1, -26, -1, 1}, {26, 24, 15, 2, 6, 
      1}, {26, 28, 22, 2, 4, 2}, {26, 30, 27, 2, 3, 3}, {27, 1, 29, -1, 
      1, -29}, {27, 2, 30, -1, 2, -15}, {27, 5, 13, -3, -5, 1}, {27, 11, 
      19, 9, 1, 1}, {27, 14, 10, -9, -2, 1}, {27, 14, 16, -3, -2, 4}, {27,
       17, 9, -3, -1, 9}, {27, 20, 16, 3, 4, 1}, {27, 23, 3, -9, -1, 
      3}, {27, 25, 1, -27, -1, 1}, {27, 26, 30, 3, 2, 3}, {28, 1, 30, -1, 
      1, -30}, {28, 5, 18, -2, -5, 2}, {28, 8, 13, -4, -4, 1}, {28, 9, 
      20, -2, -3, 4}, {28, 13, 14, -2, -1, 14}, {28, 16, 15, -4, -2, 
      3}, {28, 17, 30, 7, 1, 2}, {28, 18, 17, 4, 3, 1}, {28, 20, 
      7, -4, -1, 7}, {28, 22, 26, 4, 2, 2}, {28, 23, 4, -7, -1, 4}, {28, 
      25, 2, -14, -1, 2}, {28, 26, 1, -28, -1, 1}, {29, 27, 1, -29, -1, 
      1}, {30, 8, 21, -2, -4, 3}, {30, 12, 13, -6, -3, 1}, {30, 12, 
      21, -2, -2, 7}, {30, 12, 21, 10, 1, 1}, {30, 14, 15, -2, -1, 
      15}, {30, 16, 11, -10, -2, 1}, {30, 16, 19, 6, 2, 1}, {30, 18, 
      14, -6, -2, 2}, {30, 19, 10, -3, -1, 10}, {30, 23, 6, -5, -1, 
      6}, {30, 24, 5, -6, -1, 5}, {30, 26, 3, -10, -1, 3}, {30, 27, 
      2, -15, -1, 2}, {30, 27, 26, 3, 3, 2}, {30, 28, 1, -30, -1, 1}, {30,
       28, 17, 2, 7, 1}}
$\endgroup$

5 Answers 5

7
$\begingroup$
GatherBy[list, #[[1 ;; 3]] &] // SortBy[#, Length] & // Grid

To select:

GatherBy[list, #[[1 ;; 3]] &] // Select[Length@# > 1 &]

{{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}}, {{21, 30, 12, 1,
10, 1}, {21, 30, 12, 7, -2, -2}}, {{30, 12, 21, -2, -2, 7}, {30,
12, 21, 10, 1, 1}}}

$\endgroup$
6
$\begingroup$
Values @ GroupBy[list, #[[;; 3]] &, If[Length @ # > 1, #, Nothing] &]
{{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}},   
 {{21, 30, 12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}},   
 {{30, 12, 21, -2, -2, 7}, {30, 12, 21, 10, 1, 1}}}
$\endgroup$
5
$\begingroup$

Aggregated in form of an Association

res = Select[GreaterThan @ 1] @ CountsBy[list, #[[1 ;; 3]] &]

res // Dataset

enter image description here

To see all values:

Select[list, MatchQ[#[[1 ;; 3]], Alternatives @@ Keys[res]] &]

returns

{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}, 
{21, 30, 12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}, 
{30, 12, 21, -2, -2, 7}, {30, 12, 21, 10, 1, 1}}
$\endgroup$
5
$\begingroup$

This is not an answer.

syed = RepeatedTiming[GatherBy[list, #[[1 ;; 3]] &] // Select[Length@# > 1 &]]

(* {0.000269034, {{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}}, {{21, 30, 
    12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}}, {{30, 12, 21, -2, -2, 7}, {30, 
    12, 21, 10, 1, 1}}}} *)

kglr = RepeatedTiming[
  Values@GroupBy[list, #[[;; 3]] &, If[Length@# > 1, #, Nothing] &]]

(* {0.000520763, {{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}}, {{21, 30, 
    12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}}, {{30, 12, 21, -2, -2, 7}, {30, 
    12, 21, 10, 1, 1}}}} *)

eldo = RepeatedTiming[(res = 
    Select[GreaterThan@1]@CountsBy[list, #[[1 ;; 3]] &];
   Select[list, MatchQ[#[[1 ;; 3]], Alternatives @@ Keys[res]] &])]

(* {0.00100829, {{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}, {21, 30, 12, 1,
    10, 1}, {21, 30, 12, 7, -2, -2}, {30, 12, 21, -2, -2, 7}, {30, 12, 21, 10,
    1, 1}}} *)

Dimensions /@ Last /@ {syed, kglr, eldo}

(* {{3, 2, 6}, {3, 2, 6}, {6, 6}} *)

t = First /@ {syed, kglr, eldo};
t/Min[t]

(* {1., 1.93567, 3.74782} *)

For this particular list, Syed's approach is quickest.

$\endgroup$
1
$\begingroup$

Using SplitBy and ReplaceAll:

SplitBy[list, #[[1 ;; 3]] &] /. x_ /; Length[x] == 1 :> Nothing

(*{{12, 21, 30, -2, 7, -2}, {12, 21, 30, 1, 1, 10}},
   {{21, 30, 12, 1, 10, 1}, {21, 30, 12, 7, -2, -2}}, 
   {{30, 12, 21, -2, -2, 7}, {30, 12, 21, 10, 1, 1}}}*)
$\endgroup$
1
  • 1
    $\begingroup$ This assumes that the original list is sorted. $\endgroup$
    – Bob Hanlon
    Dec 18, 2023 at 20:36

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.