'Given a list of strings, return characters that appear in more than one string
I'm trying to implement a function which receives a variable number of strings and returns characters that appear at least in two strings:
test_strings = ["hello", "world", "python", ]
print(test(*strings))
{'h', 'l', 'o'}
Solution 1:[1]
Remove duplicates from the strings (by making sets of the characters of each string), then create a Counter
that counts the number of inputs strings that each character appeared in
from collections import Counter
from itertools import chain
def test(*strings, n=2):
sets = (set(string) for string in strings)
counter = Counter(chain.from_iterable(sets))
return {char for char, count in counter.items() if count >= n}
print(test("hello", "world", "python")) # {'o', 'h', 'l'}
Solution 2:[2]
A one-liner using sets and collections.Counter
:
from collections import Counter
test_strings = ["hello", "world", "python"]
letters = {k for k, v in Counter([l for x in test_strings for l in set(x)]).items() if v > 1}
Output:
>>> letters
{'o', 'l', 'h'}
Solution 3:[3]
The following function find_duplicates
should do the work
def find_duplicates(string_list):
"""
Given a list of strings, return characters that appear in more than one string
"""
# Create a dictionary to store the characters and their counts
char_count = {}
# Iterate over the string and add the characters to the dictionary
for string in string_list:
for char in string:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
# Create a list to store the characters that appear at least twice
duplicates = []
# Iterate over the dictionary and add the characters to the list
for char, count in char_count.items():
if count > 1:
duplicates.append(char)
# Return the list of duplicates
return duplicates
Now let's test using the list of strings you give as an example
test_strings = ["hello", "world", "python"]
print(find_duplicates(test_strings))
[Out]: ['h', 'l', 'o']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Patrick Haugh |
Solution 2 | CDJB |
Solution 3 |