1

I am trying to filter data in an array with the help of a pattern \d\.\d. The elements in the array might sometimes consist of strings as well. I try to use the re.findall function to get a list of the decimal numbers within my strings in the array but my code doesn't recognise all decimal numbers.

My code is as below -

import re 
import itertools
str1 = "2.7"
str2 = ".3"
str3 = "."
str4 = "2"
str5 = "sushruth" 
x = [str1,str2,str3,str4,str5]
y = []
for a in x:
    z = re.findall(r'\d\.\d',a)
    if z:
        print(z)

The output is only [2.7] whereas I need to also get [.3]. What change is required in my code

0

1 Answer 1

2

You can use:

z = re.findall(r'\d?\.\d',a)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The output i get looks somewhat like [2.7] [.3] is there any way to get all the detected patterns in a list or array like [2.7,.3] ?
@Sushruth [e for e in x if re.match(r'\d?.\d', e)]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.