I have many lists like this
synonyms = ["3,2'-DIHYDROXYCHALCONE", '36574-83-1', '36574831', "2',3-Dihydroxychalcone", '(E)-1-(2-hydroxyphenyl)-3-(3-hydroxyphenyl)prop-2-en-1-one', MLS002693861]
from which I need to remove all elements that are comprised of only digits. I can't figure out how to remove element [1]
because it's only digits but has random intervening dashes.
Of course, this doesn't work since the dashes make the element not a digit:
synonym_subset = [x for x in synonym_subset if not (x.isdigit())]
And I can't just remove the dashes because I want the dashes in the other elements to be retained:
synonym_subset = [x.replace('-','') for x in synonym_subset]
I could run the above to find the index of the elements to be removed, then remove them that way, but I was hoping for a one-liner.
Thanks.
Try:
synonyms = [
"3,2'-DIHYDROXYCHALCONE",
"36574-83-1",
"36574831",
"2',3-Dihydroxychalcone",
"(E)-1-(2-hydroxyphenyl)-3-(3-hydroxyphenyl)prop-2-en-1-one",
"MLS002693861",
]
out = [s for s in synonyms if not all(ch in "0123456789-_" for ch in s)]
print(out)
Prints:
[
"3,2'-DIHYDROXYCHALCONE",
"2',3-Dihydroxychalcone",
"(E)-1-(2-hydroxyphenyl)-3-(3-hydroxyphenyl)prop-2-en-1-one",
"MLS002693861",
]