Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find "n" in a list full of strings

I’m looking to go through a list and find any element with a number.

This is what i got so far

list = ['Alvarez, S', 'Crawford, B', 'Fury, 8', 'Mayweather, F', 'Lopez, 44']

num = '8'

for s in home_pitchers:
    if num in s:
        print(s)

print(ex)
>>> Fury, 8

What I’m looking to do is to have num be 0 – 9. I thought about using ‘[^0-9]’ but that didn’t work.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ultimately I’m looking to print out this

print
>>> Fury, 8
>>> Lopez, 44

Just a heads up, I’m pretty new to coding so some concept might go over my head

>Solution :

You can use isdigit method with any function. The isdigit method return True if the string is a digit string, False otherwise.

>>> lst = ['Alvarez, S', 'Crawford, B', 'Fury, 8', 'Mayweather, F', 'Lopez, 44']
>>>
>>> for s in lst:
...     if any(char.isdigit() for char in s):
...             print(s)
...
Fury, 8
Lopez, 44
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading