最大和最小值
函数 TakeLargest、TakeLargestBy、TakeSmallest 和 TakeSmallestBy 使用数值排序,为从列表和关联中选择元素提供了简单的方法.
对每个南美国家,将居民期望寿命和基于购买力平价(PPP)上的国内生产总值(GDP)进行关联.
In[1]:=
data = EntityValue[
EntityClass["Country",
"SouthAmerica"], {EntityProperty["Country", "LifeExpectancy"],
EntityProperty["Country",
"GDP", {"CurrencyUnit" -> "CurrentUSDollar", "PPP" -> "PPP"}]},
"EntityAssociation"]
Out[1]=
用 TakeSmallest 获取寿命期望最低的两个国家.
In[2]:=
c1 = TakeSmallest[data[[All, 1]], 2]
Out[2]=
TakeLargest 给出基于 PPP 的 GDP 最大的两个国家.
In[3]:=
c2 = TakeLargest[data[[All, 2]], 2]
Out[3]=
在地理地图上用颜色强调这两组国家.
In[4]:=
GeoGraphics[{Red, Polygon[Keys@c1], Green, Polygon[Keys@c2]}]
Out[4]=