By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
digital news today : Latest News digital news today : Latest News
Notification Show More
Latest News
How to Use GitHub Actions to Automate Open-Source Projects | digital daily
freecodecamp
Speicherdauer für Einträge zu Privatinsolvenz verkürzt
tech
Le prix du casque Bose QC SE chute de 26 % grâce aux ventes flash du printemps
android
The Gathering Card Keeps Breaking Records
Gaming
Just a moment…
Nomad trip
Aa
  • Marketing
    • Crypto
    • news nft tech frensh
    • Technology
  • ahref
    • android
    • blogdumoderateur
    • creativetrends
    • Crypto
    • css tricks daily
  • Daily sécurité
    • dailydev
  • digitaltrends
    • Featured
    • Food
    • for blogger
    • google
    • gpldload
  • Technology
    • It-Connect
      • Laravel tricks
      • Marketing
      • news nft tech frensh
      • nft
      • thehackernews
      • web3
        • webrankinfo
Reading: Python RegEx Tutorial – How to use RegEx inside lambda Expression | digital daily
Share
digital news today : Latest News digital news today : Latest News
Aa
  • Marketing
  • ahref
  • Daily sécurité
  • digitaltrends
  • Technology
Search
  • Marketing
    • Crypto
    • news nft tech frensh
    • Technology
  • ahref
    • android
    • blogdumoderateur
    • creativetrends
    • Crypto
    • css tricks daily
  • Daily sécurité
    • dailydev
  • digitaltrends
    • Featured
    • Food
    • for blogger
    • google
    • gpldload
  • Technology
    • It-Connect
Follow US
© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.
digital news today : Latest News > freecodecamp > Python RegEx Tutorial – How to use RegEx inside lambda Expression | digital daily
freecodecamp

Python RegEx Tutorial – How to use RegEx inside lambda Expression | digital daily

Mike
Last updated: 2023/03/17 at 11:04
Mike
Share
SHARE


Contents
What We’ll CoverHow to use RegEx inside the Expression of a Lambda FunctionHow to use RegEx inside the Expression of a Lambda Function with the filter() FunctionHow to use RegEx inside the Expression of a Lambda Function with the map() FunctionHow to use RegEx inside the Expression of a Lambda Function with the sort() MethodConclusion

It’s possible to use RegEx inside a lambda function in Python. You can apply this to any Python method or function that takes a function as a parameter. Such functions and methods include filter(), map(), any(), sort(), and more.

Keep reading as I show you how to use regular expressions inside a lambda function.

What We’ll Cover

How to use RegEx inside the Expression of a Lambda Function

The syntax with which a lambda function can take a RegEx as its expression looks like this:

lambda x: re.method(pattern, x)

Be aware that you have to use the lambda function on something. And that’s where the likes of map(), sort(), filter(), and others come in.

How to use RegEx inside the Expression of a Lambda Function with the filter() Function

The first example I will show you use the filter() function:

import re

fruits = ['apple', 'mango', 'banana', 'cherry', 'apricot', 'raspberry', 'avocado']
filtered_fruits = filter(lambda fruit: re.match('^a', fruit), fruits)

# convert the new fruits to another list and print it
print(list(filtered_fruits)) # ['apple', 'apricot', 'avocado']

In the code above:

  • the filter() takes the lambda function as the function to execute and the fruits list as the iterable
  • for the expression of the lambda function, it uses the re.match() method of Python RegEx and uses the pattern ^a on the argument fruit
  • the last thing I did was convert all items on the list that matches the pattern into a list

How to use RegEx inside the Expression of a Lambda Function with the map() Function

To use RegEx inside a lambda function with another function like map(), the syntax is similar:

import re

fruits2 = ['opple', 'bonono', 'cherry', 'dote', 'berry']
modified_fruits = map(lambda fruit: re.sub('o', 'a', fruit), fruits2)

# convert the new fruits to another list and print it
print(list(modified_fruits)) # ['apple', 'banana', 'cherry', 'date', 'berry']

In the code above:

  • the modified_fruits is looping through the fruits2 list with a map() function
  • uses the re.sub() method of Python RegEx as the expression of the lambda function.

The re.sub method lets you replace the first value with the second one. In the example, it switched all occurrences of o to a.

How to use RegEx inside the Expression of a Lambda Function with the sort() Method

The last example I will show you uses the sort() method of lists:

import re

fruits = [ 'banana', 'fig', 'grapefruit']

# sort fruits based on the number of vowels
fruits.sort(key=lambda x: len(re.findall('[aeiou]', x)))

print(fruits) #['fig', 'banana', 'grapefruit']

In the code, the lambda function sorts the list based on the number of vowels. It does it with the combination of the len() method, the findall() method of Python RegEx, and the pattern [aeiou].

The word fruit with the lowest number of vowels comes first. If you use reverse=True, it arranges the fruits based on those with the highest number of vowels – descending order:

import re

fruits = [ 'banana', 'fig', 'grapefruit']

# sort fruits based on the number of vowels
fruits.sort(key=lambda x: len(re.findall('[aeiou]', x)), reverse=True)

print(fruits) # ['grapefruit', 'banana', 'fig']

Conclusion

In this article, we looked at how you can pass in RegEx to a lambda function by showing you examples using the filter(), map() functions, and the sort() method.

I hope this article gives you the knowledge you need to use RegEx inside a lambda function.

Keep coding!



Source link

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.

By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Mike mars 17, 2023
Share this Article
Facebook Twitter Copy Link Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Verstößt die Schufa mit ihrem Scoring gegen EU-Recht?
Next Article SEJ’s Weekly News Recap For Search, Social, And AI
lapressecrypto.com
lapressecrypto.com
Follow US

© 2022 Foxiz News Network. Ruby Design Company. All Rights Reserved.

Removed from reading list

Undo
Go to mobile version
Welcome Back!

Sign in to your account

Register Lost your password?