Boosted_d16
Boosted_d16

Reputation: 14062

Locating index of item via REGEX in Python

I've hit a wall and need some help.

Context: I have a list of lists of strings.

 Matrix = [['444'],
          ['23%'],
          ['A']]

For the moment, we have a list of Counts at position 0, Percentage at 1 and Significance test at 2.

The number of lists within the list changes from 3 to 2 to 1 from time to time. And the type of string within the list changes position. So I cannot assume that index 0 will always be Counts and percentage will be 1 etc.

Hence I wanted to find a way of dynamically finding out where percentage, for example, is located in the Matrix, and if its even in there.

Here is what I've written so far:

for i, row in enumerate(Matrix):
    for cell in row:
        if "%" in cell:
            percent = i
            print percent
for i, row in enumerate(Matrix):
    for cell in row:            
        if re.search('[a-zA-Z]', cell):
            sigtest = i
            print sigtest
for i, row in enumerate(Matrix):
    for cell in row:
        if re.search('[0-9]', cell):
            count = i
            print count

Output:

1
2
0
1

Problem: As you can see, the problem is located with the last for loop which records the fact that index 0 and 1 contain numbers.

Whats needed: A way for the last for loop to search for numbers and numbers only, a item contains numbers and a symbol, that index should not be included.

Hope that makes sense!

thanks

Upvotes: 1

Views: 68

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

To check whether the string contains only digits you can use :

>>> bool(re.search(r'^[0-9]+$', '4444'))
True
>>> '4444'.isdigit()    #Doesn't work for floats
True
# This will work for floats as well
>>> bool(re.search(r'^[0-9]*(\.[0-9]+)?$', '4444.123'))
True
>>> bool(re.search(r'^[0-9]*(\.[0-9]+)?$', '.123'))
True

For percentage you can use this regex

>>> bool(re.search(r'^[0-9]+%$', '23%'))
True
#To handle floats as well use this:
>>> bool(re.search(r'^[0-9]*(\.[0-9]+)?%$', '23.15%'))
True
>>> bool(re.search(r'^[0-9]*(\.[0-9]+)?%$', '.150%'))
True

For sigtest:

>>> bool(re.search(r'^[a-z]$', 'a',re.I))
True
>>> bool(re.search(r'^[a-z]$', 'B',re.I))
True

Here ^ and $ are meta-characters:

^ matches the start of the string and $ matches the end of the string.

Upvotes: 1

Related Questions