问题
I use the programming language: J.
I want to put all of the digit of a number in a list.
From:
12345
to:
1 2 3 4 5
What can I do?
回答1:
Another approach:
intToList =: 3 : '((>. 10 ^. y)#10) #: y'
This doesn't convert to string and back, which can be potentially costly, but counts the digits with a base-10 log, then uses anti-base (#:
) to get each digit.
EDIT:
Better, safer version based on Dan Bron's comment:
intToList =: 3 : '10 #.^:_1 y'
回答2:
The way I'd write this is
10&#.^:_1
which we can see in use with this sentence:
(10&#.^:_1) 123456789
1 2 3 4 5 6 7 8 9
That program relies on the reshaping built in to Base. It uses the (built-in) obverse of Base as a synonym for Antibase.
回答3:
I found the answer:
intToList =: (".@;"0@":)
来源:https://stackoverflow.com/questions/17677142/list-a-numbers-digits-in-j