嗨,我有一个字符串数组
["Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1", "Facebook_16_2013/03/17_1", "Facebook_15_2013/03/17_1", "Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1", "Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1", "Facebook_16_2014/03/17_1", "Facebook_15_2014/03/17_1", "Facebook_14_2014/03/17_1", "Facebook_13_2014/03/17_1", "Facebook_18_2015/03/17_1", "Facebook_17_2015/03/17_1", "Facebook_16_2015/03/17_1", "Facebook_15_2015/03/17_1", "Facebook_14_2015/03/17_1", "Facebook_13_2015/03/17_1","Facebook_18_2013/04/12_1", "Facebook_17_2013/05/11_1", "Facebook_16_2013/05/01_1", "Facebook_15_2013/04/03_1", "Facebook_14_2013/06/03_1", "Facebook_13_2013/06/15_1"]
从这个数组中,我想将字符串与日期、年、月分组。
预期输出 {"2013/03"=>["Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1", "Facebook_16_2013/03/17_1", "Facebook_15_2013/03/17_1", "Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1"],"2013/04"=>["Facebook_18_2013/04/17_1", "Facebook_17_2013/04/17_1", "Facebook_16_2013/04/17_1", "Facebook_15_2013/04/17_1", "Facebook_14_2013/04/17_1", "Facebook_13_2013/04/17_1"], "2014/03"=>["Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1", "Facebook_16_2014/03/17_1", "Facebook_15_2014/03/17_1", "Facebook_14_2014/03/17_1", "Facebook_13_2014/03/17_1"], "2015/03"=>["Facebook_18_2015/03/17_1", "Facebook_17_2015/03/17_1", "Facebook_16_2015/03/17_1", "Facebook_15_2015/03/17_1", "Facebook_14_2015/03/17_1", "Facebook_13_2015/03/17_1"]}
我使用以下ruby代码根据年份
进行分组a.flatten.uniq.group_by{|x| x.split("_")[3].split("/")[0]}
那么我该如何做有蛾的年份(例如:2013/04)?
因为这里只关心年、/和月,而且d
有点贵,所以解决方案也可以这样写:
a = [
"Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1",
"Facebook_16_2013/03/17_1", "Facebook_15_2013/03/17_1",
"Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1",
"Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1",
"Facebook_16_2014/03/17_1", "Facebook_15_2014/03/17_1",
"Facebook_14_2014/03/17_1", "Facebook_13_2014/03/17_1",
"Facebook_18_2015/03/17_1", "Facebook_17_2015/03/17_1",
"Facebook_16_2015/03/17_1", "Facebook_15_2015/03/17_1",
"Facebook_14_2015/03/17_1", "Facebook_13_2015/03/17_1",
"Facebook_18_2013/04/12_1", "Facebook_17_2013/05/11_1",
"Facebook_16_2013/05/01_1", "Facebook_15_2013/04/03_1",
"Facebook_14_2013/06/03_1", "Facebook_13_2013/06/15_1"
]
a.group_by { |s| s[/([0-9]{4}/[0-9]{2})/,1] }
#{
# "2013/03"=>["Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1", "Facebook_16_2013/03/17_1", "Facebook_15_2013/03/17_1", "Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1"],
# "2014/03"=>["Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1", "Facebook_16_2014/03/17_1", "Facebook_15_2014/03/17_1", "Facebook_14_2014/03/17_1", "Facebook_13_2014/03/17_1"],
# "2015/03"=>["Facebook_18_2015/03/17_1", "Facebook_17_2015/03/17_1", "Facebook_16_2015/03/17_1", "Facebook_15_2015/03/17_1", "Facebook_14_2015/03/17_1", "Facebook_13_2015/03/17_1"],
# "2013/04"=>["Facebook_18_2013/04/12_1", "Facebook_15_2013/04/03_1"],
# "2013/05"=>["Facebook_17_2013/05/11_1", "Facebook_16_2013/05/01_1"],
# "2013/06"=>["Facebook_14_2013/06/03_1", "Facebook_13_2013/06/15_1"]
#}
这个更快一些
按如下操作:
a = [
"Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1",
"Facebook_16_2013/03/17_1","Facebook_15_2013/03/17_1",
"Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1",
"Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1",
"Facebook_16_2014/03/17_1"
]
a.group_by { |s| s[/Facebook_d+_(d+/d+)/d+_d+/,1] }
# => {"2013/03"=>["Facebook_18_2013/03/17_1", "Facebook_17_2013/03/17_1",
# "Facebook_16_2013/03/17_1", "Facebook_15_2013/03/17_1",
# "Facebook_14_2013/03/17_1", "Facebook_13_2013/03/17_1"],
# "2014/03"=>["Facebook_18_2014/03/17_1", "Facebook_17_2014/03/17_1"
# , "Facebook_16_2014/03/17_1"]}