In a python program I want to filter string which are mostly numbers, such as the results of experiments in a table etc.
For example, a string like "year 2004 2005 2006" or "dimension 2.343m 3.85m 343.5m" must be filtered.
I know how to check if string contains number by any(i.isdigit() for i in str)
but how to count them? I can do that with a loop, but I look for a similar function to do it inline.
You can count
digits in string and check that count of digit for example bigger than len()//2
or not like below:
>>> st = "year 2004 2005 2006"
>>> sum(s.isdigit() for s in st) > len(st)//2
True
>>> st2 = "year year year 2006"
>>> sum(s.isdigit() for s in st2) > len(st2)//2
False
# for more explanation
>>> [s.isdigit() for s in st2]
[False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
True,
True,
True,
True]
>>> sum(s.isdigit() for s in st2)
4