4

I wonder if is possible flatten a list at the last level without flattening at the first level, I think that isn't a good explanation so here is my code

Table[Table[{Position[points[dx, dy, n], {i, j}][[1]], 
   Position[points[dx, dy, n], Vec[dx, dy, n, i, j][[k]]][[1]]}, {k, 
   1, Dimensions[Vec[dx, dy, n, i, j]][[1]]}], {j, -n, n}, {i, -n, n}]

This input gives me

{{{{{1}, {4}}, {{1}, {2}}}, {{{4}, {1}}, {{4}, {7}}, {{4}, {5}}},
{{{7}, {4}}, {{7}, {8}}}}, {{{{2}, {5}}, {{2}, {1}}, {{2}, {3}}},
{{{5}, {2}}, {{5}, {8}}, {{5}, {4}}, {{5}, {6}}}, {{{8}, {5}}, {{8},
{7}}, {{8}, {9}}}}, {{{{3}, {6}}, {{3}, {2}}}, {{{6}, {3}}, {{6},
{9}}, {{6}, {5}}}, {{{9}, {6}}, {{9}, {8}}}}}

If I do this I get the desired result

Flatten[Table[
  Table[Flatten[{Position[points[dx, dy, n], {i, j}][[1]], 
     Position[points[dx, dy, n], Vec[dx, dy, n, i, j][[k]]][[1]]}, 
    1], {k, 1, Dimensions[Vec[dx, dy, n, i, j]][[1]]}], {j, -n, 
   n}, {i, -n, n}], 2]

Which is

{{1, 4}, {1, 2}, {4, 1}, {4, 7}, {4, 5}, {7, 4}, {7, 8}, {2, 5}, {2, 
  1}, {2, 3}, {5, 2}, {5, 8}, {5, 4}, {5, 6}, {8, 5}, {8, 7}, {8, 
  9}, {3, 6}, {3, 2}, {6, 3}, {6, 9}, {6, 5}, {9, 6}, {9, 8}}

My question is if there is a way to do this without using Flatten twice. I hope I explained it well.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355

1 Answers1

4

You can use Flatten with a list second argument:

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

Flatten[data, {{1, 2, 3}, {4, 5}}]

{{1, 4}, {1, 2}, {4, 1}, {4, 7}, {4, 5}, {7, 4}, {7, 8}, {2, 5}, {2, 1}, {2, 3}, {5, 2}, {5, 8}, {5, 4}, {5, 6}, {8, 5}, {8, 7}, {8, 9}, {3, 6}, {3, 2}, {6, 3}, {6, 9}, {6, 5}, {9, 6}, {9, 8}}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355