A slightly more complete (but guesstimated) code:
class K:
"""A class that has a name property"""
def __init__(self,name):
self.name = name
# a prefix to look for
name = "Fiat "
# a list of objects that have a name - some starting with our name
nlist = [K("Fiat 2"), K("Fiat 4"), K("Fiat 2711999"), K("Fiat Dupe 09"), K("DosntFly 0815")]
def is_number(text):
"""Test if the text is an integer."""
try:
int(text.strip())
except:
return False
return True
versions = [x.name[len(name):] for x in nlist
if (x.name.startswith(name) and is_number(x.name[len(name):]))]
print(versions)
Output:
['2', '4', '2711999']
Why?
The list comprehension test each element of nlist
.
It will let only things into versions
that statisfy the if (x.name.startswith(name) and is_number(x.name[len(name):]
.
Meaning: each classes instanse name
has to start with the variable name
's content and whatever comes after as many characters as name
got, has to check as True
if fed to is_number()
.
If this fits, it is taking the "number-part" and adds it as element into the resulting list.
"Fiat Dupe 09" # not in the list, because "Dupe 09" is not True
"DosntFly 0815" # not in the list, because not starting with "Fiat "