[ad_1]
This tutorial demonstrates how you can discover palindromes and anagrams in a listing of strings using Python lambda. You’ll see absolutely working coding snippets and ought to be capable to execute with Python 3.
We will use a Lambda expression contained in the filter() perform to seek out all of the palindromes and anagrams in the string listing. In Python, we additionally communicate of lambda as the nameless perform, i.e., a perform which doesn’t have a title.
Python Program – Find Palindromes and Anagrams
Let’s first write a Python program which takes a listing of strings as enter, discover the palindromes, and print the outcome.
Use lambda to seek out palindromes in a listing of strings
We’ll apply the nameless perform (Python lambda) to determine palindromes in the listing of strings. First, let’s perceive what’s a palindrome.
A palindrome is a phrase, digits, or a sequence of phrases that comes the identical while studying backward.
See the total logic in the beneath coding snippet.
""" Program: Python program to seek out palindromes in a listing of strings """ def get_palindromes(input_string_list): print("*******************") print("input_string_list = ", input_string_list) print("*******************n") # Find the listing of palindromes in the strings out_string_list = listing(filter(lambda x: (x.decrease() == "".be part of(reversed(x.decrease()))), input_string_list)) # Print the listing of palindromes in the strings print("*******************") print("out_string_list = ", out_string_list) print("*******************n") def Driver(): input_string_list = ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go'] get_palindromes(input_string_list) if __name__=="__main__": Driver() # name Driver() perform
The outcome of the above Python code snippet is as follows:
******************* input_string_list = ['Python', 'Radar', 'CSharp', 'Madam', 'Programmer', 'Noon', 'Refer', 'Php', 'Go'] ******************* ******************* out_string_list = ['Radar', 'Madam', 'Noon', 'Refer', 'Php'] *******************
Sometimes, you may also must convert a list to string, so have your self go over it.
Use lambda to seek out anagrams in a listing of strings
We’ll once more apply the nameless perform to determine anagrams in the listing of strings. First, let’s perceive what’s an anagram.
An anagram is a phrase or phrase constituted by ordering the characters of a totally different phrase or phrase.
See the total logic in the beneath coding snippet.
""" Program: Python program to seek out anagrams in a listing of strings """ from collections import Counter def get_anagrams(input_string_list, test_string): print("*******************") print("input_string_list = ", input_string_list) print("*******************n") # Find the listing of anagrams in the strings out_string_list = listing(filter(lambda x: (Counter(test_string) == Counter(x)), input_string_list)) # Print the listing of anagrams in the strings print("*******************") print("out_string_list = ", out_string_list) print("*******************n") def Driver(): input_string_list = ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning'] test_string = "ntoyPh" get_anagrams(input_string_list, test_string) if __name__=="__main__": Driver() # name Driver() perform
The outcome of the above Python code snippet is as follows:
******************* input_string_list = ['Python', 'Program', 'Machine', 'yPtnoh', 'Learning'] ******************* ******************* out_string_list = ['Python', 'yPtnoh'] *******************
To study extra, learn our flagship Python tutorial for freshmen and superior learners.
Python Recommended Post
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data , POP , popitem , update method
- Python Dictionary | update() method
- Delete statement , Looping in list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
[ad_2]