Skip to content

Instantly share code, notes, and snippets.

@billykong
Last active April 19, 2017 16:23
Show Gist options
  • Save billykong/2c217572da8006ddd3b7f85a5c52a5bc to your computer and use it in GitHub Desktop.
Save billykong/2c217572da8006ddd3b7f85a5c52a5bc to your computer and use it in GitHub Desktop.
python forloop vs map+lambda
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