Checking if first digit in each element in list are the same

John Smith

How would I check if the first digits in each element in a list are the same?

for i in range(0,len(lst)-1):
     if lst[i] == lst[i+1]:
          return True

I know that this checks for if the number before is equal to the next number in the list, but I just want to focus on the first digit.

jpp

You can use math.log10 and floor division to calculate the first digit. Then use all with a generator expression and zip to test adjacent elements sequentially:

from math import log10

def get_first(x):
    return x // 10**int(log10(x))

L = [12341, 1765, 1342534, 176845, 1]

res = all(get_first(i) == get_first(j) for i, j in zip(L, L[1:]))  # True

For an explanation of how this construct works, see this related answer. You can apply the same logic via a regular for loop:

def check_first(L):
    for i, j in zip(L, L[1:]):
        if get_first(i) != get_first(j):
            return False
    return True

res = check_first(L)  # True

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Retrieve last element of a column from each group and use it as first element of the same column in next group

分類Dev

R, how to use for loop to make each element in the list executed by the same command

分類Dev

Loop through each element of the list

分類Dev

How to skip first element in each line of matrix?

分類Dev

Checking if 2 list have any equal element Haskell

分類Dev

Finding if any element in a list is in another list and return the first element found

分類Dev

Storing the first word in each line of a string into a list

分類Dev

Capitalize the first letter in each string in a list

分類Dev

how to hide first and last element from the three same element

分類Dev

Check if each element in a numpy array is in a separate list

分類Dev

Converting each element of a "Large list" to a matrix in R

分類Dev

Using the last element from each list in Python

分類Dev

apply calculation on multiple columns of each element in a list

分類Dev

execute some code for each element of a list in batch

分類Dev

remove double quotes from first element of the list

分類Dev

Accessing a range of the first element of a tuple in a list of tuples

分類Dev

First element incorrect in python list comprehension

分類Dev

Calculate perc of each element in a list for each value in column in pandas dataframe

分類Dev

For each element in a list, choose that element and 3 random nonrepeating elements

分類Dev

Group list-of-tuples by second element, take average of first element

分類Dev

Make an array from list where first element of the array is the first element in list

分類Dev

jQuery count each first li element with class name

分類Dev

Python subtract first element from each respective row of matrix

分類Dev

checking list<> items and if it contains same item copy them and remove it from list<>

分類Dev

Convert nested list to dict where first element of list is key for dict

分類Dev

How to compare duplicate element in list of list and give IF condition on another element of same list of list in python

分類Dev

Capture first digit after decimal

分類Dev

DITA DTD, allow same element after each other

分類Dev

Javascript: each element of a array of objects having same value

Related 関連記事

  1. 1

    Retrieve last element of a column from each group and use it as first element of the same column in next group

  2. 2

    R, how to use for loop to make each element in the list executed by the same command

  3. 3

    Loop through each element of the list

  4. 4

    How to skip first element in each line of matrix?

  5. 5

    Checking if 2 list have any equal element Haskell

  6. 6

    Finding if any element in a list is in another list and return the first element found

  7. 7

    Storing the first word in each line of a string into a list

  8. 8

    Capitalize the first letter in each string in a list

  9. 9

    how to hide first and last element from the three same element

  10. 10

    Check if each element in a numpy array is in a separate list

  11. 11

    Converting each element of a "Large list" to a matrix in R

  12. 12

    Using the last element from each list in Python

  13. 13

    apply calculation on multiple columns of each element in a list

  14. 14

    execute some code for each element of a list in batch

  15. 15

    remove double quotes from first element of the list

  16. 16

    Accessing a range of the first element of a tuple in a list of tuples

  17. 17

    First element incorrect in python list comprehension

  18. 18

    Calculate perc of each element in a list for each value in column in pandas dataframe

  19. 19

    For each element in a list, choose that element and 3 random nonrepeating elements

  20. 20

    Group list-of-tuples by second element, take average of first element

  21. 21

    Make an array from list where first element of the array is the first element in list

  22. 22

    jQuery count each first li element with class name

  23. 23

    Python subtract first element from each respective row of matrix

  24. 24

    checking list<> items and if it contains same item copy them and remove it from list<>

  25. 25

    Convert nested list to dict where first element of list is key for dict

  26. 26

    How to compare duplicate element in list of list and give IF condition on another element of same list of list in python

  27. 27

    Capture first digit after decimal

  28. 28

    DITA DTD, allow same element after each other

  29. 29

    Javascript: each element of a array of objects having same value

ホットタグ

アーカイブ