我得到3时,Mathematica Pick停止列表 [英] Mathematica Pick stop a list as soon as I get a 3
问题描述
这就是我所拥有的...
Here's what I have...
Select[roll, # <= 3 &]
现在,下面的列表可能有1个3或2个3.我希望它从左到右停在前三个.
Now, the list that follows may have one 3 or two three's. I want it to stop at the first three from left to right.
试图编码的数学家.
推荐答案
注意:我删除了原来的帖子,基本上与belisarius的帖子相同.这是另一种尝试...
Note: I removed my original posting, which was basically like that of belisarius. Here's another try...
如果您确定数字3是roll
的成员,那么这应该起作用:
If you are sure that the number 3 is a member of roll
then this should work:
TakeWhile[roll, # != 3 &]~Append~3
这等效于:
Take[roll, LengthWhile[roll, # != 3 &] +1]
如果您不能做出此假设,请先使用MemberQ
If you cannot make this assumption, just test first with MemberQ
编辑:感谢TomD在第二个解决方案中建议+1,从而消除了使用Append
的需要.
EDIT: Credit goes to TomD for suggesting the +1 in the second solution, which eliminates the need to use Append
.
这篇关于我得到3时,Mathematica Pick停止列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!