How Can I Find All Common Letters In A Set Of Strings?
I've written code to find all common letters of 3 given strings. Unfortunately, there are some errors, which I can't find. If someone of you could tell me how such a code could lo
Solution 1:
def commonLetters(*strings):
return set.intersection(*map(set,strings))
Demo:
>>> commonLetters('abcdef', 'defg', 'def', 'ef')
{'e', 'f'}
Solution 2:
Python is designed to make things simple to read, and to provide standard libraries for most functions--that's why it's "batteries included." The Pythonic way to do what you want to do is with set intersections.
>>> a = 'foobar'
>>> b = 'bar'
>>> c = 'barbaz'
>>> common_letters = set(a) & set(b) & set(c)
>>> print( list(common_letters) )
['a', 'r', 'b']
There are certainly other ways to construct the set, but the KISS principle definitely applies here.
Post a Comment for "How Can I Find All Common Letters In A Set Of Strings?"