jeudi 27 juillet 2017

Python Check if list item does (not) contain any of other list items

I have this problem where I want to remove a list element if it contains 'illegal' characters. The legal characters are specified in multiple lists. They are formed like this, where alpha stands for the alphabet (a-z + A-Z), digit stands for digits (0-9) and punct stands for punctuation (sort of).

alpha = list(string.ascii_letters)
digit = list(string.digits)
punct = list(string.punctuation)

This way I can specify something as an illegal character if it doesn't appear in one of these lists.

After that I have a list containing elements:

Input = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]

I want to filter out the elements containing illegal characters. So this is the result I want to get (doesn't need to be ordered):

var = ["Amuu2", "Q1BFt", "mgF)`", "Y9^^M", "W0PD7"]

I don't really have an idea on how to achieve this, and I have already tried various things.

Aucun commentaire:

Enregistrer un commentaire