Nexis Nexis - 1 year ago 39
Python Question

I wish to make sure items in a list don't have duplicate elements not present in inputted word

Right, so I need to make sure that each item in a list when compared to an inputted word don't contain duplicate elements that are not present in the inputted word. It's a bit difficult to explain so I'll just show what I've got so far:

listword = input("Inputted word: ")
listcomparison = ["su", "pici", "nope", "pics", "susu", "poici","pic","pooici"]

for line in listcomparison:
word = list(listword)
while len(word) >10:
word = input("Inputted word needs to be less than or 10 letters long: ")
imp_letter = word[4]
wordcount = len(line)

if wordcount >= 4:
chars = list(line)

if imp_letter in line and all(char in word for char in chars):

I didn't mention it at the beginning, but the "imp_word" in the code is there because one requirement in the exercise is that the 5th letter in the inputted word must be present for all words in the list.

The problem with this is that when I run this program, using the inputted word "suspicion" for example, it will return "pici
pooici". The problem then is that words like pooici (well, "words") that have duplicate elements (pooici for example has two O) not present in the inputted word are showing up, when I'm supposed to make it so they... don't. BASICALLY, the inputted word is the allowed alphapet, and the quantity of each character is all that's allowed.

I know I SUCK at explaining this stuff, but I've been looking for a solution for almost a whole day now. Help a newbie out?

Answer Source

Use collections.Counter.

Here are some examples of the collections.Counter in action:

>>> Counter("read")
Counter({'a': 1, 'r': 1, 'e': 1, 'd': 1})
>>> Counter("dad") - Counter("read") # two "d" in "dad" minus one "d" in "read" leaves one "d"
Counter({'d': 1})
>>> Counter("ad") - Counter("read") # letters "a" and "d" are in "read"
>>> bool(Counter()) # an empty Counter evaluates to False

Here's how to code a solution to your problem:

words = ["su", "pici", "nope", "pics", "susu", "poici","pic","pooici"]
letter_counts = Counter("suspicion")
found_words = [word for word in words if not Counter(word) - letter_counts]

found_words contains

['su', 'pici', 'pics', 'poici', 'pic']