What is exact use of creating own function here?

Hello all,
Right now I am working on the 8.Negative character classes in Regular Expression Basics. What is exact use of creating own function here as we are still calling str.contains(pattern). How do I know which one we have called?
Screen Link:

https://app.dataquest.io/m/354/regular-expression-basics/8/negative-character-classes

My Code:

# def first_10_matches(pattern):
#     """
#     Return the first 10 story titles that match
#     the provided regular expression
#     """
#     all_matches = titles[titles.str.contains(pattern)]
#     first_10 = all_matches.head(10)
#     return first_10
#java_titles=titles.first_10_matches(r'[Jj]ava[^Ss]')

java_titles=titles[titles.str.contains(r'[Jj]ava[^Ss]')].head(10)
def first_10_matches(pattern):
    """
    Return the first 10 story titles that match
    the provided regular expression
    """
    all_matches = titles[titles.str.contains(pattern)]
    first_10 = all_matches.head(10)
    return first_10

java_titles=titles.first_10_matches(r'[Jj]ava[^Ss]').head(10)
AttributeError: 'Series' object has no attribute 'first_10_matches'

hey @aditya1257

not sure what exactly you are trying here.

But have you tried using the function first_10_matches by simply passing the pattern?
like - first_10_matches(r’[Jj]ava[^Ss]’)

the purpose of this function is to show us what kind of results we can expect from this pattern. this is like a trial and run of the pattern - and for ease of understanding only 10 rows which show a match are returned.

once the testing is done, we can then use a finalized pattern on all the rows from the dataset.

hope that helps.

Hi, They want me to define a function first_10_matches to match the pattern. As in dataquest answer we are not calling any first_10_matches function and whereas this below code will give the desired output. So I want to know is it a technical glitch or I am missing something?

java_titles=titles[titles.str.contains(r'[Jj]ava[^Ss]')].head(10)

DataQuest answer:

def first_10_matches(pattern):
    """
    Return the first 10 story titles that match
    the provided regular expression
    """
    all_matches = titles[titles.str.contains(pattern)]
    first_10 = all_matches.head(10)
    return first_10
pattern = r"[Jj]ava[^Ss]"
java_titles = titles[titles.str.contains(pattern)]

hey @aditya1257

the first_10_matches here only plays as a helper function - it’s like we want to check if the pattern we have written will fetch us the correct result or not. so we put use of this function and for result checking, we extract only the first 10 rows where pattern matched.

this is same as first_10_matches, however this will not be accepted as answer because this assigns only the first 10 rows of where the pattern matched and we need to extract all the rows/values where pattern matched.

first_10_matches is only to help you test the pattern, not to exactly extract the output. the mission is explaining to us that before we apply our pattern to an entire series/ dataframe, we should test it on the first 10/ 15 or 5 rows from the given dataset, in case we get the wrong results and still continue our analysis with them.

Okay. I think I got it. Thank you

1 Like