3
$\begingroup$

How to remove 0 that represent hours, minutes, and seconds from a date list say a

 a = {{{2007, 1, 3, 0, 0, 0.}, 10.}, {{2007, 1, 4, 0, 0, 0.},20.}, {{2007, 1, 5, 0, 0, 0.}, 30.}}

Result should be:

{{{2007, 1, 3}, 10.}, {{2007, 1, 4},20.}, {{2007, 1, 5}, 30.}}
$\endgroup$
  • $\begingroup$ One possible solution: b = a /. {x_, y_, z_, i_, j_, k_} -> {x, y, z} though you would need to be careful when you have exactly 6 temporal datum $\endgroup$ – Jonie Dec 3 '14 at 10:30
  • 1
    $\begingroup$ {#[[1, 1 ;; 3]], #[[2]]} & /@ a $\endgroup$ – Yves Klett Dec 3 '14 at 10:59
  • $\begingroup$ This should delete all "0" and "0." elements from your List: b = Delete[a, Position[a, n_ /; Or[n==0,n==0.]]]. $\endgroup$ – Boson Dec 3 '14 at 11:29
4
$\begingroup$
a = {{{2007, 1, 3, 0, 0, 0.}, 10.}, {{2007, 1, 4, 0, 0, 0.},  20.},
     {{2007, 1, 5, 0, 0, 0.}, 30.}};

{#[[;; 3]], #2} & @@@ a
(* {{{2007,1,3},10.}, {{2007,1,4},20.}, {{2007,1,5},3 0.}} *)
| improve this answer | |
$\endgroup$
3
$\begingroup$
Cases[a, {x_, y_} :> {x[[;; 3]], y}]
| improve this answer | |
$\endgroup$
3
$\begingroup$

Here is an alternative aiming at maximal robustness: it does only change those entries of the list for which the change makes sense (i.e. the first part looks like a date list). Of course other approaches are much more efficient, so if efficiency is relevant and you know that all entries look the same, this approach is probably not what you want to use...

Replace[a, 
  {date : {Repeated[_?NumericQ, {6}]}, value_} :> {Take[date, 3],value}, 
  {1}
]

note that by restricting the pattern to exactly 6 repetitions we don't have a problem if there are cases where the date lists have only 1 or 2 elements. By restricting Replace to only work on the 1st level we also don't run into the problem that the list itself could be matched (or any subexpression at a deeper level)...

| improve this answer | |
$\endgroup$
1
$\begingroup$

I find Part the easist way of doing this sort of operation:

a[[All, 1]] = a[[All, 1, 1 ;; 3]];
a

(* {{{2007, 1, 3}, 10.}, {{2007, 1, 4}, 20.}, {{2007, 1, 5}, 30.}} *)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.