Message Boards Message Boards

0
|
2932 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Convert integers to list?

Posted 5 years ago

I'm trying to get a list output, but I get integer results.

Is there a way to convert integers to list?

Here is my code:

list = {12, 37, 44, 96};
    list2 = IntegerDigits[list];
    Do[
     list3 = Total[list2[[i]]];
     Print[list3];
     , {i, 1, Length[list]}]
POSTED BY: Nagon Stewart
4 Replies

You can find this trick in Documentation of Do.

list = {12, 37, 44, 96};
list2 = IntegerDigits[list];
Reap[Do[list3 = Total[list2[[i]]];
   Sow[list3];, {i, 1, Length[list]}]][[2, 1]]

(* {3, 10, 8, 15} *)

list = {12, 37, 44, 96};
list2 = IntegerDigits[list];
t = {}; Do[list3 = Total[list2[[i]]];
 AppendTo[t, list3];, {i, 1, Length[list]}]; t

(* {3, 10, 8, 15} *)
POSTED BY: Mariusz Iwaniuk
Posted 5 years ago

Thank you Mariusz, This trick helped.

POSTED BY: Nagon Stewart

Do you mean this?

list3 = Total /@ list2
POSTED BY: Henrik Schachner
Posted 5 years ago

Yes, I meant that, thank you.

POSTED BY: Nagon Stewart
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract