将天数列表拆分为包含天数列表的月份列表

时间:2017-09-29 12:59:32

标签: elixir

我有一份包含日期的地图列表。我不知道该列表有多长(可以是多年的天数)。我所知道的是列表按日期排序,没有重复日期。

示例:

[
%{date: ~D[2017-01-01], y: ["x"]}, %{date: ~D[2017-01-02], y: ["x"]},
%{date: ~D[2017-01-03], y: ["x"]}, %{date: ~D[2017-01-04], y: ["x"]},
%{date: ~D[2017-01-05], y: ["x"]}, %{date: ~D[2017-01-06], y: ["x"]},
%{date: ~D[2017-01-07], y: ["x"]}, %{date: ~D[2017-01-08], y: ["x"]},
%{date: ~D[2017-01-09], y: ["x"]}, %{date: ~D[2017-01-10], y: ["x"]},
%{date: ~D[2017-01-11], y: ["x"]}, %{date: ~D[2017-01-12], y: ["x"]},
%{date: ~D[2017-01-13], y: ["x"]}, %{date: ~D[2017-01-14], y: ["x"]},
%{date: ~D[2017-01-15], y: ["x"]}, %{date: ~D[2017-01-16], y: ["x"]},
%{date: ~D[2017-01-17], y: ["x"]}, %{date: ~D[2017-01-18], y: ["x"]},
%{date: ~D[2017-01-19], y: ["x"]}, %{date: ~D[2017-01-20], y: ["x"]},
%{date: ~D[2017-01-21], y: ["x"]}, %{date: ~D[2017-01-22], y: ["x"]},
%{date: ~D[2017-01-23], y: ["x"]}, %{date: ~D[2017-01-24], y: ["x"]},
%{date: ~D[2017-01-25], y: ["x"]}, %{date: ~D[2017-01-26], y: ["x"]},
%{date: ~D[2017-01-27], y: ["x"]}, %{date: ~D[2017-01-28], y: ["x"]},
%{date: ~D[2017-01-29], y: ["x"]}, %{date: ~D[2017-01-30], y: ["x"]},
%{date: ~D[2017-01-31], y: ["x"]}, %{date: ~D[2017-02-01], y: ["x"]},
%{date: ~D[2017-02-02], y: ["x"]}, %{date: ~D[2017-02-03], y: ["x"]}
]

我想要一个包含日期的月份列表:

[
[
%{date: ~D[2017-01-01], y: ["x"]}, %{date: ~D[2017-01-02], y: ["x"]},
%{date: ~D[2017-01-03], y: ["x"]}, %{date: ~D[2017-01-04], y: ["x"]},
%{date: ~D[2017-01-05], y: ["x"]}, %{date: ~D[2017-01-06], y: ["x"]},
%{date: ~D[2017-01-07], y: ["x"]}, %{date: ~D[2017-01-08], y: ["x"]},
%{date: ~D[2017-01-09], y: ["x"]}, %{date: ~D[2017-01-10], y: ["x"]},
%{date: ~D[2017-01-11], y: ["x"]}, %{date: ~D[2017-01-12], y: ["x"]},
%{date: ~D[2017-01-13], y: ["x"]}, %{date: ~D[2017-01-14], y: ["x"]},
%{date: ~D[2017-01-15], y: ["x"]}, %{date: ~D[2017-01-16], y: ["x"]},
%{date: ~D[2017-01-17], y: ["x"]}, %{date: ~D[2017-01-18], y: ["x"]},
%{date: ~D[2017-01-19], y: ["x"]}, %{date: ~D[2017-01-20], y: ["x"]},
%{date: ~D[2017-01-21], y: ["x"]}, %{date: ~D[2017-01-22], y: ["x"]},
%{date: ~D[2017-01-23], y: ["x"]}, %{date: ~D[2017-01-24], y: ["x"]},
%{date: ~D[2017-01-25], y: ["x"]}, %{date: ~D[2017-01-26], y: ["x"]},
%{date: ~D[2017-01-27], y: ["x"]}, %{date: ~D[2017-01-28], y: ["x"]},
%{date: ~D[2017-01-29], y: ["x"]}, %{date: ~D[2017-01-30], y: ["x"]},
%{date: ~D[2017-01-31], y: ["x"]}
],
[
%{date: ~D[2017-02-01], y: ["x"]},
%{date: ~D[2017-02-02], y: ["x"]}, %{date: ~D[2017-02-03], y: ["x"]}
]
]

因为每个月都有不同的长度(加上闰年问题),我不知道如何拆分列表。可以这样做吗?

1 个答案:

答案 0 :(得分:2)

我使用Enum.chunk_by/2,返回回调函数中每个元素的年份和日期的元组。这将在每年或每月更改时拆分列表。 (如果您的列表包含例如2016-01-01,后跟2017-01-01,则只需month无效。)

list = [
  %{date: ~D[2017-01-01], y: ["x"]}, %{date: ~D[2017-01-02], y: ["x"]},
  %{date: ~D[2017-01-03], y: ["x"]}, %{date: ~D[2017-01-04], y: ["x"]},
  %{date: ~D[2017-01-05], y: ["x"]}, %{date: ~D[2017-01-06], y: ["x"]},
  %{date: ~D[2017-01-07], y: ["x"]}, %{date: ~D[2017-01-08], y: ["x"]},
  %{date: ~D[2017-01-09], y: ["x"]}, %{date: ~D[2017-01-10], y: ["x"]},
  %{date: ~D[2017-01-11], y: ["x"]}, %{date: ~D[2017-01-12], y: ["x"]},
  %{date: ~D[2017-01-13], y: ["x"]}, %{date: ~D[2017-01-14], y: ["x"]},
  %{date: ~D[2017-01-15], y: ["x"]}, %{date: ~D[2017-01-16], y: ["x"]},
  %{date: ~D[2017-01-17], y: ["x"]}, %{date: ~D[2017-01-18], y: ["x"]},
  %{date: ~D[2017-01-19], y: ["x"]}, %{date: ~D[2017-01-20], y: ["x"]},
  %{date: ~D[2017-01-21], y: ["x"]}, %{date: ~D[2017-01-22], y: ["x"]},
  %{date: ~D[2017-01-23], y: ["x"]}, %{date: ~D[2017-01-24], y: ["x"]},
  %{date: ~D[2017-01-25], y: ["x"]}, %{date: ~D[2017-01-26], y: ["x"]},
  %{date: ~D[2017-01-27], y: ["x"]}, %{date: ~D[2017-01-28], y: ["x"]},
  %{date: ~D[2017-01-29], y: ["x"]}, %{date: ~D[2017-01-30], y: ["x"]},
  %{date: ~D[2017-01-31], y: ["x"]}, %{date: ~D[2017-02-01], y: ["x"]},
  %{date: ~D[2017-02-02], y: ["x"]}, %{date: ~D[2017-02-03], y: ["x"]}
]

list
|> Enum.chunk_by(fn %{date: %{year: year, month: month}} -> {year, month} end)
|> IO.inspect

输出:

[[%{date: ~D[2017-01-01], y: ["x"]}, %{date: ~D[2017-01-02], y: ["x"]},
  %{date: ~D[2017-01-03], y: ["x"]}, %{date: ~D[2017-01-04], y: ["x"]},
  %{date: ~D[2017-01-05], y: ["x"]}, %{date: ~D[2017-01-06], y: ["x"]},
  %{date: ~D[2017-01-07], y: ["x"]}, %{date: ~D[2017-01-08], y: ["x"]},
  %{date: ~D[2017-01-09], y: ["x"]}, %{date: ~D[2017-01-10], y: ["x"]},
  %{date: ~D[2017-01-11], y: ["x"]}, %{date: ~D[2017-01-12], y: ["x"]},
  %{date: ~D[2017-01-13], y: ["x"]}, %{date: ~D[2017-01-14], y: ["x"]},
  %{date: ~D[2017-01-15], y: ["x"]}, %{date: ~D[2017-01-16], y: ["x"]},
  %{date: ~D[2017-01-17], y: ["x"]}, %{date: ~D[2017-01-18], y: ["x"]},
  %{date: ~D[2017-01-19], y: ["x"]}, %{date: ~D[2017-01-20], y: ["x"]},
  %{date: ~D[2017-01-21], y: ["x"]}, %{date: ~D[2017-01-22], y: ["x"]},
  %{date: ~D[2017-01-23], y: ["x"]}, %{date: ~D[2017-01-24], y: ["x"]},
  %{date: ~D[2017-01-25], y: ["x"]}, %{date: ~D[2017-01-26], y: ["x"]},
  %{date: ~D[2017-01-27], y: ["x"]}, %{date: ~D[2017-01-28], y: ["x"]},
  %{date: ~D[2017-01-29], y: ["x"]}, %{date: ~D[2017-01-30], y: ["x"]},
  %{date: ~D[2017-01-31], y: ["x"]}],
 [%{date: ~D[2017-02-01], y: ["x"]}, %{date: ~D[2017-02-02], y: ["x"]},
  %{date: ~D[2017-02-03], y: ["x"]}]]