Get most significant digit in python

Nicky Feller

Say I have list [34523, 55, 65, 2]

What is the most efficient way to get [3,5,6,2] which are the most significant digits. If possible without changing changing each to str()?

senshin

Assuming you're only dealing with positive numbers, you can divide each number by the largest power of 10 smaller than the number, and then take the floor of the result.

>>> from math import log10, floor
>>> lst = [34523, 55, 65, 2]
>>> [floor(x / (10**floor(log10(x)))) for x in lst]
[3, 5, 6, 2]

If you're using Python 3, instead of flooring the result, you can use the integer division operator //:

>>> [x // (10**floor(log10(x))) for x in lst]
[3, 5, 6, 2]

However, I have no idea whether this is more efficient than just converting to a string and slicing the first character. (Note that you'll need to be a bit more sophisticated if you have to deal with numbers between 0 and 1.)

>>> [int(str(x)[0]) for x in lst]
[3, 5, 6, 2]

If this is in a performance-critical piece of code, you should measure the two options and see which is faster. If it's not in a performance-critical piece of code, use whichever one is most readable to you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In Python how to find the least significant digit

From Dev

How to extract digits from a number in C? Begining from the most significant digit?

From Dev

Get the most significant bit from an 8-bit value

From Dev

How to get most significant n bits from int in java

From Dev

Get the most significant bit from an 8-bit value

From Dev

The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

From Dev

Most Significant Byte Calculation

From Dev

Hex of most significant nibble

From Dev

rounding through rule of significant digit

From Dev

Most efficient way to get digit count of arbitrarily big number

From Dev

set most significant bit in C

From Dev

How can you get the j first most significant bits of an integer in C++?

From Dev

Rounding decimal when the last significant digit is 5

From Dev

Position of first significant digit after the decimal point

From Dev

Least significant non-zero digit of a factorial

From Dev

Add 1 to the least significant digit of a number in MATLAB

From Dev

Radix sort most significant first or least significant, which is faster?

From Dev

most significant v.s. least significant radix sort

From Dev

Storing data in the most significant bits of a pointer

From Dev

How to clear most significant bit in byte?

From Dev

Find most significant set bit in a long

From Dev

Extract Most Significant Nibble using Swift

From Dev

Comparing the Most Significant Bit of two numbers: ==, <, <=

From Dev

ToString abstract operation: least significant digit not uniquely determined

From Dev

ToString abstract operation: least significant digit not uniquely determined

From Dev

Shift least significant digit out of hex sequence in JavaScript

From Dev

How to format Excel numbers so that >= 0.1 has 1 significant digit, but < 0.1 has two significant digits?

From Dev

How to compare two arrays in python, and get the boolean output based on a significant value

From Java

How to get hour format in two digit (00.00.00) in python?

Related Related

  1. 1

    In Python how to find the least significant digit

  2. 2

    How to extract digits from a number in C? Begining from the most significant digit?

  3. 3

    Get the most significant bit from an 8-bit value

  4. 4

    How to get most significant n bits from int in java

  5. 5

    Get the most significant bit from an 8-bit value

  6. 6

    The MSB (most significant bit ) in pyserial python sent to arduino uno in damged

  7. 7

    Most Significant Byte Calculation

  8. 8

    Hex of most significant nibble

  9. 9

    rounding through rule of significant digit

  10. 10

    Most efficient way to get digit count of arbitrarily big number

  11. 11

    set most significant bit in C

  12. 12

    How can you get the j first most significant bits of an integer in C++?

  13. 13

    Rounding decimal when the last significant digit is 5

  14. 14

    Position of first significant digit after the decimal point

  15. 15

    Least significant non-zero digit of a factorial

  16. 16

    Add 1 to the least significant digit of a number in MATLAB

  17. 17

    Radix sort most significant first or least significant, which is faster?

  18. 18

    most significant v.s. least significant radix sort

  19. 19

    Storing data in the most significant bits of a pointer

  20. 20

    How to clear most significant bit in byte?

  21. 21

    Find most significant set bit in a long

  22. 22

    Extract Most Significant Nibble using Swift

  23. 23

    Comparing the Most Significant Bit of two numbers: ==, <, <=

  24. 24

    ToString abstract operation: least significant digit not uniquely determined

  25. 25

    ToString abstract operation: least significant digit not uniquely determined

  26. 26

    Shift least significant digit out of hex sequence in JavaScript

  27. 27

    How to format Excel numbers so that >= 0.1 has 1 significant digit, but < 0.1 has two significant digits?

  28. 28

    How to compare two arrays in python, and get the boolean output based on a significant value

  29. 29

    How to get hour format in two digit (00.00.00) in python?

HotTag

Archive