本文介绍了表[ ] 输出基数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Table[ ] 命令通常返回一个与迭代器基数相同的列表.

The Table[ ] command usually returns a list with the same cardinality of its iterator.

Table[i, {i,4}]
(*
->{1,2,3,4}
*)

很容易证明,可以返回一个比迭代器更大基数的列表

It is easy to show that is possible to return a list with a greater cardinality than the iterator

Table[Sequence @@ ConstantArray[1, i], {i, 2}]
(*
->{1,1,1}
*)

但是......有没有办法返回一个比迭代器LESS基数的列表?

But ... Are there ways to return a list with LESS cardinality than the iterator?

推荐答案

一个简单的例子:

Table[Sequence @@ ConstantArray[1, i - 1], {i, 2}]
Out[1] = {1}

这不需要总是返回具有较小基数的列表.例如,{i,3} 返回相等而 {i,4} 返回更多.

This need not always return a list with smaller cardinality. For e.g., {i,3} returns equal and {i,4} returns more.

或者更愚蠢的例子是

Table[Sequence @@ {}, {i, 2}]

但我不知道它是否重要.

but I don't know if it counts.

您也可以在 Table

Table[Sequence @@ Piecewise[{
    {ConstantArray[1, i], i < 3},
    {ConstantArray[2, i], 3 <= i < 5},
    {{}, i >= 5}}],
 {i, 20}]

Out[2] = {1, 1, 1, 2, 2, 2, 2, 2, 2, 2}

这篇关于表[ ] 输出基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:46