Mathematica:确定列表中的所有整数是否小于一个数字?

Nop*_*ope 5 wolfram-mathematica list

Mathematica中是否有一种方法可以确定列表中的所有整数是否小于设定的数字.例如,如果我想知道列表中的所有数字是否小于10:

theList = {1, 2, 3, 10};
magicFunction[theList, 10]; --> returns False
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

Joe*_*ert 7

查看列表的Max函数,它返回列表中的最大数字.从那里,您可以检查此值是否小于某个数字.


gde*_*ino 6

在提供我的解决方案之前,让我评论前两个解决方案.让我们调用Joey Robert的解决方案magicFunction1和Eric的解决方案magicFunction2.

magicFunction1非常短而优雅.我不喜欢的是,如果我有一个非常大的数字列表,并且第一个已经大于10,它仍将完成所有工作,找出不需要的最大数字.这也适用于magicFunction2

我开发了以下两种解决方案:

magicFunction3[lst_, val_] := 
 Position[# < val & /@ lst, False, 1, 1] == {}
Run Code Online (Sandbox Code Playgroud)

magicFunction4[lst_, val_] := 
 Cases[lst, x_ /; x >= val, 1, 1] == {}
Run Code Online (Sandbox Code Playgroud)

我找到了一个基准

In[1]:= data = Table[RandomInteger[{1, 10}], {10000000}];

In[2]:= Timing[magicFunction1[data, 10]]
Out[2]= {0.017551, False}

In[2]:= Timing[magicFunction2[data, 10]]
Out[2]= {10.0173, False}

In[2]:= Timing[magicFunction3[data, 10]]
Out[2]= {7.10192, False}

In[2]:= Timing[magicFunction4[data, 10]]
Out[2]= {0.402562, False}
Run Code Online (Sandbox Code Playgroud)

所以我最好的答案是magicFunction4,但我仍然不知道为什么它比magicFunction1慢.我也忽略了为什么magicFunction3和magicFunction4之间存在如此大的性能差异.