Skip to content

Instantly share code, notes, and snippets.

@dbuteau
Created February 20, 2020 13:20
Show Gist options
  • Save dbuteau/3ab303dc9438148e3a0716360ebbadcc to your computer and use it in GitHub Desktop.
Save dbuteau/3ab303dc9438148e3a0716360ebbadcc to your computer and use it in GitHub Desktop.
tricky python functions
#!/usr/bin/env python3
# encoding: utf-8
"""
count the number of objects containing attributes values (regex) in list of objects
made regex case insensitive
"""
import re
class user:
def __init__(self, name, age):
self.name = name
self.age = age
users_list = [user('alex',20),user('nico',20)]
sum(bool(re.search('.*le.*', getattr(user,'name'),re.I) ) for user in users_list)
# 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment