打印 Grid with list in list 作为 Wolfram Alpha 的输出

print Grid with list in list as output from Wolfram Alpha

我在 Wolfram Mathematica 中执行了这条指令作为 Wolfram Alpha 查询

a = WolframAlpha[
  "italy vs england coffee consumption", \
    {{"History:AgricultureConsumption:AgricultureData", 1}, 
   "ComputableData"}]

'a' 中存储了我感兴趣的数据。

问题是: 我怎样才能输出一个只有重要数据的网格,比如每年的 'date' 和 'tone /year'?

一个简单的表格,按日期比较意大利和英国的咖啡消费量。

date | italy | england
----------------------
1961 | 11111 | 2222222
1962 | 11112 | 2222223
....
...
..
.
a = WolframAlpha["italy coffee consumption",
{{"History:AgricultureConsumption:AgricultureData", 1},
    "ComputableData"}];

b = WolframAlpha["england coffee consumption",
{{"History:AgricultureConsumption:AgricultureData", 1},
    "ComputableData"}];

(* select common dates *)
dates = Intersection[First /@ a, First /@ b];

Labeled[DateListPlot[Transpose[c = Flatten[{
       Cases[a, {#, _}],
       Cases[b, {#, _}]}, 1] & /@ dates],
  PlotLegends -> {"Italy", "England"}], "t/yr", {{Top, Left}}]

Table输出

TableForm[{#1[[1, 1]], #1[[2, 1]], #2[[2, 1]]} & @@@ c,
 TableHeadings -> {None, {"Year", "Italy", "England"}}]

使用 WolframAlpha 函数的替代方法是使用 EntityValue:

DateListPlot@EntityValue[
    {Entity["Country","Italy"],Entity["Country","UnitedKingdom"]},
    EntityProperty["Country","AgricultureConsumption", {"AgricultureProduct"->"Coffee","Date"->All}]
]

为了完整性,这是直接使用问题结果的方法:

a = WolframAlpha[
  "italy vs england coffee consumption", \
{{"History:AgricultureConsumption:AgricultureData", 1}, 
   "ComputableData"}]

注意DateListPlot可以直接使用:

DateListPlot[a]

确认两个数据集的日期相同:

a[[1, All, 1]] == a[[2, All, 1]]

True

现在 table:

TableForm[
  MapThread[{DateString[#1[[1]], "Year"], 
    Sequence @@ ((Round@QuantityMagnitude[#[[2]]]) & /@ {##})} &, a], 
    TableHeadings -> {None, {"Year", "Italy", "England"}}]

请注意,这依赖于一些信念,即 alpha 按问题中提出的顺序提供结果。其他方法可能更可靠。