How to format two separate lists in columns, rather than rows in Mathematica?

泪湿孤枕 提交于 2019-12-04 07:45:02

Personally I prefer Grid to TableForm. Maybe something like this?

Some preliminaries:

x = {1, 2, 3, 4, 5};
y = {1, 4, 9, 16, 25};
grid = Transpose@{x, y};
headings = {{Item["x", Frame -> {True, True}], 
    Item["y", Frame -> {True, False}]}};

The following code,

Grid[Join[headings, grid], Alignment -> Right, Dividers -> All, 
 Spacings -> {3, 1}, FrameStyle -> Orange]

gives this as output, for example:

Often in Mathematica, you use Transpose to switch the role of row and column.

In[6]:= x = {1,2,3,4,5}; y = {1,4,9,16,25};

In[7]:= {x,y} // Transpose // TableForm

Out[7]//TableForm= 1   1

                   2   4

                   3   9

                   4   16

                   5   25

Instead of using Transpose you can use the option TableDirection:

x={1,2,3,4,5};y={1,4,9,16,25};
TableForm[{x,y},TableDirections->Row,TableHeadings->{{"x","y"}}]

 Grid[Transpose[{x, y}], Alignment -> Right]  

I would use:

x = {1, 2, 3, 4, 5};
y = {1, 4, 9, 16, 25};

TableForm[{{x, y}}, TableAlignments -> Right]


Here are some more convoluted examples, demonstrating the way TableForm works. It does get complicated, and I usually have to experiment a bit to get what I want.

a = {1, 2, 3};
b = {4, 5, 6};

{a, b} // TableForm

{{a}, {b}} // TableForm

{{{a}}, {{b}}} // TableForm

{{{a}, {b}}} // TableForm

{{List /@ a, List /@ b}} // TableForm

{{a}, b} // TableForm

{{{a}, b}} // TableForm

{{{a}}, {b}} // TableForm

{{{{a}}, {b}}} // TableForm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!