Reverse digits of a list of numbers in python

Viewed 389
for i in range(len(primo)):
    reversed_num = 0
    while i > 0:
       digit = i % 10
       reversed_num = reversed_num * 10 + digit
       i //= 10
       reversed.append (reversed_num)
    
for r in range(len(reversed)):
    print (r)

I'm writing a program to reverse a series of numbers from a list. For example:

Input: 1234,124,654

Output: 4321,421,456

The script works correctly with one number, but with a list it doesn't work properly.

5 Answers

Nice try, but what about a function that reverses the numbers? You should be happy to know that reversed already exists in Python, and you can use it this way:

>>> list(reversed(str(0.1223142131)))
['1', '3', '1', '2', '4', '1', '3', '2', '2', '1', '.', '0']
>>> float(''.join(reversed(str(0.1223142131))))
1312413221.0

so you can implement your own function:

def myReverser(n: float) -> float:
    return float(''.join(reversed(str(n))))

and then use it over the whole list:

reversedList = [myReverser(i) for i in [1234, 124, 654]]

If you don't want the .0 at the end of all the numbers, this means that you don't want floating point numbers, but integers, so you can just do this:

reversedList = [int(i) for i in reversedList]

Well, you are only printing the range of the length, so I don't know how this would print your numbers at all. Here is a brief method that does what you are after, I believe.

In [1]: [int(str(x)[::-1]) for x in [1234, 124, 654]]
Out[1]: [4321, 421, 456]

or:

def reverse_items(l: list[int]) -> list[int]:
    return [int(str(x)[::-1]) for x in l]


if __name__ == "__main__":
    print(reverse_items([4321, 421, 456]))

output:

[1234, 124, 654]

This creates a list of integer values generated from reversing the string values of given list items.

You can reverse a list in Python using the built-in reverse() or reversed() methods. These methods will reverse the list without creating a new list. Python reverse() and reversed() will reverse the elements in the original list object.

A string-free approach based on the old polynomial representation of numbers. Compute the quotients and remainders by a recursive long division and keeping track of them as pair in a list. Once the breaking condition is hit travel the list with a decreasing powers of 10.

def digit_reverser(n):
    
    def base_10_representation(n):
        d = []
        q, r = divmod(n, 10) # quotient, remainder
        d.append((q, r)) 
        if q < 10:
            return d
        d.extend(base_10_representation(q))
        return d

    # reversing the digits    
    d = base_10_representation(n)
    n_digits = len(d)
    return sum(r*10**(n_digits-i) for i, (_, r) in enumerate(d)) + d[-1][0]

n = 123456789
d = digit_reverser(n)

print(d)

Output

987654321

For an extension to list of numbers:

nrs = [1234, 124, 654]
nrs_rev = [digit_reverser(n) for n in nrs]

Output

[4321, 421, 456]

code:

l = input().split(',')
l = list(map(lambda x: list(map(lambda d: int(d), x))[::-1], l))
print(l)

input:

123,456,789

output:

[[3, 2, 1], [6, 5, 4], [9, 8, 7]]
Related