Answer the question
In order to leave comments, you need to log in
Why is filter object not cast to str in python?
Actually, why do you have to fence this construction:
b = ''.join(list(filter(lambda c: c.islower() or c.isdigit(), a)))
b = str(filter(lambda c: c.islower() or c.isdigit(), a))
Answer the question
In order to leave comments, you need to log in
Actually, why do you have to fence this construction:
>>> b = ''.join(filter(None, 'abcde'))
>>> b
'abcde'
>>>
Why change anything at all?
>>> filter(lambda c: c.islower() or c.isdigit(), '[email protected]#')
'asd123'
The description of filter clearly states: If iterable is a string or a tuple, the result also has that type;
But this is if you have 'a' - a string. If 'a' is a list, then you first convert it to a string.
But this is true for the second python - in the third filter it is not a function, but an iterator that needs to be processed before you get a string:
>>> ''.join(filter(lambda c: c.islower() or c.isdigit(), '[email protected]#'))
'asd123'
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question