简体   繁体   中英

Python remove only alphabet elements from a list

I have a very messy data, I am trying to remove elements that contains alphabets or words. I am trying to capture the elements that have alphanumerical and numerical values. I tried .isalpha() but it not working. How do I remove this?

lista = ['A8817-2938-228','12421','12323-12928-A','12323-12928',
             '-','A','YDDEWE','hello','world','testing_purpose','testing purpose',
        'A8232-2938-228','N7261-8271']
lista

Tried:

[i.isalnum() for i in lista] # gives boolean, but opposite of what I need. 

Output:

['A8817-2938-228','12421','12323-12928-A','12323-12928','-','A8232-2938-228','N7261-8271']

Thanks!

You can add conditional checks in list comprehensions, so this is what you want:

new_list = [i for i in lista if not i.isalnum()]
print(new_list)

Output:

['A8817-2938-228', '12323-12928-A', '12323-12928', '-', 'testing_purpose', 'testing purpose', 'A8232-2938-228', 'N7261-8271']

Note that isalnum won't say True if the string contains spaces or underscores. One option is to remove them before checking: (You also need to use isalpha instead of isalnum )

new_list_2 = [i for i in lista if not i.replace(" ", "").replace("_", "").isalpha()]
print(new_list_2)

Output:

['A8817-2938-228', '12421', '12323-12928-A', '12323-12928', '-', 'A8232-2938-228', 'N7261-8271']

What type your data in the list?

You can try to do this:

[str(i).isalnum() for i in lista] 

It seems you can just test at least one character is a digit or equality with '-' :

res = [i for i in lista if any(ch.isdigit() for ch in i) or i == '-']

print(res)

['A8817-2938-228', '12421', '12323-12928-A', '12323-12928',
 '-', 'A8232-2938-228', 'N7261-8271']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM