Python Forum

Full Version: If a set element has digits in the element
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening!
I'm trying to find if an element of a set has a digit/digits as a part of an element.
I'm using ".isdigit" but it does not work...
I have to test the set if it has just one element or it is multiple element's set.
vid_set = set(("apple", "banana",  "banana","cherry","cher123ry")) 
if len(vid_set) > 1 :
    for e_vid in vid_set :                    
        print(' SET Element ',e_vid)
        if e_vid.isdigit():
            print(f" MIxED ELEMENTs SET - > {e_vid}")
Thank you.
Tester_V
I don't know what is wrong with me...
Sorry for taking your time!
I fixed it.
import re
if len(vid_set) > 1 :
    for e_vid in vid_set : 
        if re.findall(r'\d+',e_vid) :
            print(e_vid)
Thank you!
Tester_V
(Mar-25-2024, 05:10 AM)tester_V Wrote: [ -> ]I fixed it.
Better use if re.search(r'\d', e_vid) than if re.findall(r'\d+', e_vid). It does a little less useless work.
import re
vid_set = set(("apple", "apple2e", "banana",  "banana2", "cherry", "cherry2"))
print({x for x in vid_set if re.search(r"\d", x)})
Output:
{'banana2', 'apple2e', 'cherry2'}