How do I transpose the first and deepest levels of an arbitrarily nested array? The Next CEO of Stack OverflowA question about transforming one List into two Lists with additional requirementsEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered array`Part` like `Delete`: How to delete list of columns or arbitrarily deeper levelsHow to mesh a region using adaptive cubic elementsHow to efficiently Flatten nested lists while preserving select levels?Distribute elements of one line across arbitrary dimension of another listDeep level nested list addition`Transpose` nested `Association`How to extract the first element in nested lists

Elegant way to replace substring in a regex with optional groups in Python?

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

Received an invoice from my ex-employer billing me for training; how to handle?

Should I tutor a student who I know has cheated on their homework?

What was the first Unix version to run on a microcomputer?

Why do professional authors make "consistency" mistakes? And how to avoid them?

Why has the US not been more assertive in confronting Russia in recent years?

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Can we say or write : "No, it'sn't"?

How did people program for Consoles with multiple CPUs?

How to prevent changing the value of variable?

How does the Z80 determine which peripheral sent an interrupt?

To not tell, not take, and not want

Why does the UK parliament need a vote on the political declaration?

Sending manuscript to multiple publishers

Would a galaxy be visible from outside, but nearby?

Which tube will fit a -(700 x 25c) wheel?

Multiple labels for a single equation

Is it ever safe to open a suspicious html file (e.g. email attachment)?

How to count occurrences of text in a file?

What happened in Rome, when the western empire "fell"?

If a black hole is created from light, can this black hole then move at speed of light?

How do I transpose the first and deepest levels of an arbitrarily nested array?

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?



How do I transpose the first and deepest levels of an arbitrarily nested array?



The Next CEO of Stack OverflowA question about transforming one List into two Lists with additional requirementsEmulating R data frame getters with UpValuesQuickly pruning elements in one structured array that exist in a separate unordered array`Part` like `Delete`: How to delete list of columns or arbitrarily deeper levelsHow to mesh a region using adaptive cubic elementsHow to efficiently Flatten nested lists while preserving select levels?Distribute elements of one line across arbitrary dimension of another listDeep level nested list addition`Transpose` nested `Association`How to extract the first element in nested lists










6












$begingroup$


Is there a straightforward way to convert



arr = 
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;


to:



a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b


?



I need to swap the first and last dimension. Which should in principle be possible, because, although arr does not have a fixed structure, the 'bottom' is always uniform:



Level[arr, -2]



a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b



Had Transpose/Flatten/MapThread accepted a negative level specification, it would have been easy. That is not the case.



One can think about that question as: How do I create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]]?



EDIT:



In general Level[arr, -2] should be a rectangular array, but rows do not need to be the same.



So this:



a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;


should end up:



 a1, a2, a3, a4, a5, a6, a7 , ...;









share|improve this question











$endgroup$











  • $begingroup$
    Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
    $endgroup$
    – Roman
    11 hours ago










  • $begingroup$
    Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
    $endgroup$
    – Carl Woll
    11 hours ago











  • $begingroup$
    Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
    $endgroup$
    – Roman
    11 hours ago











  • $begingroup$
    @Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
    $endgroup$
    – Kuba
    11 hours ago










  • $begingroup$
    @Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
    $endgroup$
    – Roman
    11 hours ago
















6












$begingroup$


Is there a straightforward way to convert



arr = 
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;


to:



a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b


?



I need to swap the first and last dimension. Which should in principle be possible, because, although arr does not have a fixed structure, the 'bottom' is always uniform:



Level[arr, -2]



a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b



Had Transpose/Flatten/MapThread accepted a negative level specification, it would have been easy. That is not the case.



One can think about that question as: How do I create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]]?



EDIT:



In general Level[arr, -2] should be a rectangular array, but rows do not need to be the same.



So this:



a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;


should end up:



 a1, a2, a3, a4, a5, a6, a7 , ...;









share|improve this question











$endgroup$











  • $begingroup$
    Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
    $endgroup$
    – Roman
    11 hours ago










  • $begingroup$
    Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
    $endgroup$
    – Carl Woll
    11 hours ago











  • $begingroup$
    Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
    $endgroup$
    – Roman
    11 hours ago











  • $begingroup$
    @Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
    $endgroup$
    – Kuba
    11 hours ago










  • $begingroup$
    @Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
    $endgroup$
    – Roman
    11 hours ago














6












6








6





$begingroup$


Is there a straightforward way to convert



arr = 
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;


to:



a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b


?



I need to swap the first and last dimension. Which should in principle be possible, because, although arr does not have a fixed structure, the 'bottom' is always uniform:



Level[arr, -2]



a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b



Had Transpose/Flatten/MapThread accepted a negative level specification, it would have been easy. That is not the case.



One can think about that question as: How do I create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]]?



EDIT:



In general Level[arr, -2] should be a rectangular array, but rows do not need to be the same.



So this:



a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;


should end up:



 a1, a2, a3, a4, a5, a6, a7 , ...;









share|improve this question











$endgroup$




Is there a straightforward way to convert



arr = 
a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b
;


to:



a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b


?



I need to swap the first and last dimension. Which should in principle be possible, because, although arr does not have a fixed structure, the 'bottom' is always uniform:



Level[arr, -2]



a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b



Had Transpose/Flatten/MapThread accepted a negative level specification, it would have been easy. That is not the case.



One can think about that question as: How do I create arr2 so that arr[[whatever__, y_]] == arr2[[y, whatever__]]?



EDIT:



In general Level[arr, -2] should be a rectangular array, but rows do not need to be the same.



So this:



a1, b1, a2, b2, a3, b3, a4, b4, a5, b5, a6, b6, a7,b7 ;


should end up:



 a1, a2, a3, a4, a5, a6, a7 , ...;






list-manipulation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









J. M. is slightly pensive

98.7k10311467




98.7k10311467










asked 11 hours ago









KubaKuba

107k12210531




107k12210531











  • $begingroup$
    Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
    $endgroup$
    – Roman
    11 hours ago










  • $begingroup$
    Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
    $endgroup$
    – Carl Woll
    11 hours ago











  • $begingroup$
    Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
    $endgroup$
    – Roman
    11 hours ago











  • $begingroup$
    @Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
    $endgroup$
    – Kuba
    11 hours ago










  • $begingroup$
    @Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
    $endgroup$
    – Roman
    11 hours ago

















  • $begingroup$
    Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
    $endgroup$
    – Roman
    11 hours ago










  • $begingroup$
    Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
    $endgroup$
    – Carl Woll
    11 hours ago











  • $begingroup$
    Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
    $endgroup$
    – Roman
    11 hours ago











  • $begingroup$
    @Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
    $endgroup$
    – Kuba
    11 hours ago










  • $begingroup$
    @Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
    $endgroup$
    – Roman
    11 hours ago
















$begingroup$
Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
$endgroup$
– Roman
11 hours ago




$begingroup$
Not a solution, but Flatten[MapIndexed[RotateRight[#2] -> #1 &, arr, -1]] gives you a list of rules of what needs to be constructed. I don't know of a way to construct it though: SparseArray does not construct ragged structures.
$endgroup$
– Roman
11 hours ago












$begingroup$
Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
$endgroup$
– Carl Woll
11 hours ago





$begingroup$
Maybe something along the lines of arr /. a,b->a,a,b->b? Or perhaps more generally, arr /. a_?VectorQ :> First@a, a_?VectorQ :> Last@a?
$endgroup$
– Carl Woll
11 hours ago













$begingroup$
Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
$endgroup$
– Roman
11 hours ago





$begingroup$
Does your list always contain a,b at the lowest level, or can there be anything there as long as they're all of same length?
$endgroup$
– Roman
11 hours ago













$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba
11 hours ago




$begingroup$
@Roman Level[arr, -2]` should be a rectangular array but rows do not need to be the same.
$endgroup$
– Kuba
11 hours ago












$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
11 hours ago





$begingroup$
@Kuba maybe you can come up with a recursion that constructs the result from the list of rules I gave 4 lines up? That would be a handy tool to have in any case.
$endgroup$
– Roman
11 hours ago











3 Answers
3






active

oldest

votes


















8












$begingroup$

arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;

SetAttributes[f1, Listable]
Apply[f1, arr, 0, -3] /. f1 -> List



a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b







share|improve this answer









$endgroup$




















    3












    $begingroup$

    This is what the list at the lowest level looks like:



    el = First@Level[list, -2];


    Using this, we can solve it with a rules-based approach:



    list /. el -> # & /@ el


    or a recursive approach like this:



    walk[lists : __List, i_] := walk[#, i] & /@ lists
    walk[atoms : __, i_] := i
    walk[list, #] & /@ el





    share|improve this answer









    $endgroup$




















      2












      $begingroup$

      Terrible solution using Table but works:



      Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]





      share|improve this answer









      $endgroup$













        Your Answer





        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194206%2fhow-do-i-transpose-the-first-and-deepest-levels-of-an-arbitrarily-nested-array%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8












        $begingroup$

        arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;

        SetAttributes[f1, Listable]
        Apply[f1, arr, 0, -3] /. f1 -> List



        a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b







        share|improve this answer









        $endgroup$

















          8












          $begingroup$

          arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;

          SetAttributes[f1, Listable]
          Apply[f1, arr, 0, -3] /. f1 -> List



          a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b







          share|improve this answer









          $endgroup$















            8












            8








            8





            $begingroup$

            arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;

            SetAttributes[f1, Listable]
            Apply[f1, arr, 0, -3] /. f1 -> List



            a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b







            share|improve this answer









            $endgroup$



            arr = a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b;

            SetAttributes[f1, Listable]
            Apply[f1, arr, 0, -3] /. f1 -> List



            a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 11 hours ago









            andre314andre314

            12.3k12352




            12.3k12352





















                3












                $begingroup$

                This is what the list at the lowest level looks like:



                el = First@Level[list, -2];


                Using this, we can solve it with a rules-based approach:



                list /. el -> # & /@ el


                or a recursive approach like this:



                walk[lists : __List, i_] := walk[#, i] & /@ lists
                walk[atoms : __, i_] := i
                walk[list, #] & /@ el





                share|improve this answer









                $endgroup$

















                  3












                  $begingroup$

                  This is what the list at the lowest level looks like:



                  el = First@Level[list, -2];


                  Using this, we can solve it with a rules-based approach:



                  list /. el -> # & /@ el


                  or a recursive approach like this:



                  walk[lists : __List, i_] := walk[#, i] & /@ lists
                  walk[atoms : __, i_] := i
                  walk[list, #] & /@ el





                  share|improve this answer









                  $endgroup$















                    3












                    3








                    3





                    $begingroup$

                    This is what the list at the lowest level looks like:



                    el = First@Level[list, -2];


                    Using this, we can solve it with a rules-based approach:



                    list /. el -> # & /@ el


                    or a recursive approach like this:



                    walk[lists : __List, i_] := walk[#, i] & /@ lists
                    walk[atoms : __, i_] := i
                    walk[list, #] & /@ el





                    share|improve this answer









                    $endgroup$



                    This is what the list at the lowest level looks like:



                    el = First@Level[list, -2];


                    Using this, we can solve it with a rules-based approach:



                    list /. el -> # & /@ el


                    or a recursive approach like this:



                    walk[lists : __List, i_] := walk[#, i] & /@ lists
                    walk[atoms : __, i_] := i
                    walk[list, #] & /@ el






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 11 hours ago









                    C. E.C. E.

                    50.9k399205




                    50.9k399205





















                        2












                        $begingroup$

                        Terrible solution using Table but works:



                        Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]





                        share|improve this answer









                        $endgroup$

















                          2












                          $begingroup$

                          Terrible solution using Table but works:



                          Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]





                          share|improve this answer









                          $endgroup$















                            2












                            2








                            2





                            $begingroup$

                            Terrible solution using Table but works:



                            Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]





                            share|improve this answer









                            $endgroup$



                            Terrible solution using Table but works:



                            Table[Map[#[[i]] &, arr, -2], i, Last[Dimensions[Level[arr, -2]]]]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 10 hours ago









                            RomanRoman

                            4,0161022




                            4,0161022



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Mathematica Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                Use MathJax to format equations. MathJax reference.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f194206%2fhow-do-i-transpose-the-first-and-deepest-levels-of-an-arbitrarily-nested-array%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

                                2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

                                Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?