Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You just made my weekend. I've been doing Python for a while and am well acquainted with the "item in list" idiom but did not know it could be used for find. That str.find syntax has always bothered me.

Still, I had to test just to be sure:

    test_cases = [
        # haystack, needle, expect
        ('abcdefg', 'a', True),
        ('abcdefg', 'b', True),
        ('abcdefg', 'bcd', True),
        ('abcdefg', 'h', False),
    ]
    
    for haystack,needle,expect in test_cases:
        # find version
        is_found = haystack.find(needle) != -1
    
        # in version
        is_in = needle in haystack
    
        # confirm
        print haystack, needle, expect, '-->', (is_found, is_in)
        assert is_found == expect
        assert is_found == is_in
Passed! Thanks.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: