1

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

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You only match unsigned number. You may start with (-|+)?.
@ Ashwini, thanks for that. I tested it and the problem remains. If I want to find the index of a percentage string in the list (Matrix), your code will give me index 0 & 1 instead of 1 alone. I guess its because index 0 contains numbers too. Ideally, I want to run a single function to get the index of count, percent and significance.
@Boosted_d16 No it won't, for '444' my regex will return False. bool(re.search(r'^[0-9]+%$', '444')) --> False. You're doing something wrong.
@Ashwini, your correct. I dont know what I was doing wrong before but yes, it works. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.