Last active
April 19, 2017 16:23
-
-
Save billykong/2c217572da8006ddd3b7f85a5c52a5bc to your computer and use it in GitHub Desktop.
python forloop vs map+lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for t in content.findAll(class_=re.compile('.*btn.*')): | |
t.decompose() # this works, undesired parts of content is decomposed | |
# ================================ | |
[t.decompose() for t in content.findAll(class_=re.compile('.*btn.*'))] # this works | |
# ================================ | |
# map uses generator, t.decompose() is not called until someone access t | |
# ================================ | |
try: | |
map(lambda t: t.decompose(), content.findAll(class_=re.compile('.*btn.*'))) # this does not work, the undesired parts are still here | |
except: | |
pass | |
# ================================ | |
def decompose_helper(tag): | |
tag.decompose() | |
try: | |
map(decompose_helper, content.findAll(class_=re.compile('.*btn.*'))) # this does not work, the undesired parts are still here | |
except: | |
pass | |
# ================================ | |
try: | |
tmp = map(lambda t: t.decompose(), content.findAll(class_=re.compile('.*btn.*'))) # this works | |
for t in tmp: | |
print(tmp) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment