0

Python: How should we interpret 'lambda x:not any(p in x for p in ['
#x27;,'%'])'?

def fs(lst,fil): zzz = lambda x:not any(p in x for p in fil) # How should we interpret this? return list(filter(zzz,lst)) lst=['$aaa','@bbb','%ccc'] f=['

#x27;,'%'] print(fs(lst,f)) #outputs ['@bbb'] From my understanding, ' p in x for p in fil ' is equivalent to: ' p in x for p in ['
#x27;,'%'] '. Specifically, I don't understand what the x means. From the output, I inferred that the second line meant "does not contain any of the items (ie. strings) in list f." So why/how then does ' p in x for p in ' technically mean "items (ie. strings) in...?" I'm a bit weak on iterables.

17th Sep 2020, 6:57 PM
Solus
Solus - avatar
1 Respuesta
0
There x is assigned a value each time from iterable lst. So there p in x works like $ in $aaa true, next $ in @bbb false, $ in %ccc false, next % in $aaa is false , % in @bbb is false, % in %ccc is true So not any is needed all false so return @bbb For just any(p in x for p in fil) returns $aaa, %ccc Iterables are worked by taking each value at a time into temporary variable. Just like almost similar to for loop.....
17th Sep 2020, 7:51 PM
Jayakrishna 🇮🇳