我们需要在<条码>内使用<条码>If,例如<条码> 可使用[If[i<3, i],{i, 5}]。
但我希望结果为<代码>{1,2}。
这样做有什么办法?
EDIT:
What if we consider Table[If[i< 3, f[i]], {i, 5}]
which gives {f[1], f[2], Null, Null, Null}
我们需要在<条码>内使用<条码>If,例如<条码> 可使用[If[i<3, i],{i, 5}]。
但我希望结果为<代码>{1,2}。
这样做有什么办法?
EDIT:
What if we consider Table[If[i< 3, f[i]], {i, 5}]
which gives {f[1], f[2], Null, Null, Null}
Concisely:
Table[If[i < 3, i, ## &[]], {i, 5}]
This works because the function ## &
does not immediately evaluate.
## &
is a "vanishing" function.
{1, 2, ## &[], 3, 4}
----> {1, 2, 3, 4}
If you need to remove it from an existing list, you can use
DeleteCases[list, Null]
或
list /. Null -> Sequence[]
(a bit m或e advanced).
关于您的<代码>可下载,例如,首先指出,<代码>If中的第二 com是不必要的(甚至见pink):
list = Table[If[i < 3, i], {i, 5}]
To filter the table elements by a condition, you might want to use something similar to
list = Select[Table[i, {i, 5}], # < 3 &]
instead.
Finally, if you need to generate the list without ever adding rejected elements to it (to save mem或y), I suggest using Reap
and Sow
:
Reap@Do[If[i < 3, Sow[i]], {i, 5}]
list = %[[2, 1]]
I haven t actually verified the mem或y usage of this compared to a plain Table
, and note that if you generate only numbers, which can be st或ed in a packed array, the Table
construct may be m或e mem或y efficient. On the other hand if you generate a truly huge number of generic expressions, the maj或ity of which will be rejected in If
, Sow
/ Reap
may be better.
作为一种替代办法,您可使用
In[12]:= tableGenAltMD[i,{i,5},#<3&]
Out[12]= {1,2}
最后一个论点是代表条件的职能。 事实上,如果一个人能够直接使用<条码>i<>->->(和/或其他代变量)的辛醇,那么这种合成物可能不会增加。
在以前的航道中,<代码># &[]可改为“内在编号Nothing
。
Table[If[i < 3, i, Nothing], {i, 5}]
专 员
{1, 2}
如果你使用“公平”而不是“纽埃”,那么你可以这样做。
Table[If[i < 3, i, Hold[Sequence[]]] // ReleaseHold, {i, 5}]
I wished for a long time that If would have Attribute SequenceHold. I think I once suggested this to WRI, but there are probably (good?) reasons for If to not hat this attribute. One can try, if one dares to change built-in Symbols (which one should probably not do):
Unprotect[If];
SetAttributes[If, SequenceHold];
之后,如果只是工作的话,就应当做到:
Table[If[i < 3, i, Sequence[]], {i, 5}]
I have a problem with dereferencing a Javascript object and setting it to NULL. Here, I have a Folder implementation that supports recursive subdirectory removal. Please see my comments to ...
I know that you are supposed to use 0 instead of NULL in c++ (even though NULL is defined as 0 in C++ most of the time). Recently I came across some code where 0x0 was used instead, though. What is ...
Hey peepz! I have this footer image, that I want to align to the bottom of the stage, however I m getting errors. As you can see I have an ADDED_TO_STAGE listener in the constructor function. ...
On a php file that many variables are received by $_REQUEST[] or $_POST[], and I have to check them in case the value is null with the function isset(), it is quite troublesome. Is there a better ...
Following on from this question SELECT the newest record with a non null value in one column I know have a problem where I have this data id | keyword | count | date 1 | ipod | 200 | 2009-08-02 2 | ...
I have table data which looks like this id | keyword | count | date 1 | ipod | 200 | 2009-08-02 2 | ipod | 250 | 2009-09-01 3 | ipod | 150 | 2009-09-04 4 | ipod | NULL | 2009-09-07 Now what I am ...
Ok, I have these intersection methods to work with ranges, and they work well as long as the range endpoints are not null: public static bool Intersects<T>(this Range<T> first, Range<T&...
I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...