Search code examples
pythondigits

Huge Numpy list - How to get n digit?


Currently i have a huge database of randomly generated numbers in numpy.

array([62051180209, 87882444506, 49821030805, ..., 54840854303,
       21222836608, 24070750502])

Now i want to check how many nunmbers have for ex. nunmber 05, 15 digits on position 3 and 4. (ex. 62-05-1180209, like first on my list)

I would like to check how many numberx have other digits in other position. Like position 5, 6. 1st on my list have number 11 for example.


Solution

  • As Random Davis already suggested, this might work:

    import numpy as np
    mylist = np.array([62011180209, 87882444506, 49821030805, 54840854303,21222836608, 24070750502])
    
    def get_matches(mylist, start, end, value):
        value = str(value)
        return [str(i)[start:end+1]==value  for i in mylist]
    
    get_matches(mylist, start=3, end=4, value=11)
    

    For that list this delivers the following result:

    [True, False, False, False, False, False]
    

    If multiple choices should be considered, then with a naive approach, the above function can be rewritten as follows:

    def get_matches_multichoice(mylist, start, end, valuelist):
        valuelist = [str(value) for value in valuelist]
        return [str(i)[start:end+1] in valuelist  for i in mylist]
    

    Calling is for the above data example:

    print (get_matches_multichoice(mylist, start=3, end=5, valuelist=np.array([111, 824, 408])) )
    

    then returns:

    [True, True, False, True, False, False]