0
list = [ "A", "B" ,"C","T"]

I want to make a word from letters in a string. If the user inputs "cat" then that's correct, but if it's "CATT" then that's wrong. How would I detect that? The computer just sees if "A,B,C,T" is used but not how many times.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
zxz
  • 11
  • 1
  • Are you aware of the `count()` method? You could use that to make sure the count of each character in the input matches the count of the output. Does that help? – Random Davis Nov 19 '20 at 23:14
  • Is this really a duplicate of marked question? I see this as checking a string to see if the characters from a list is repeated. Linked question is about seeing if a list has duplicate elements in itself. – Hamza Nov 20 '20 at 01:44

3 Answers3

4

Well, you could use len(word) to get the length of the word, right?

Also, a set is a container of unique values. That is, you can’t have the same value in a set more than once.

You can also pass a string into a set to fill the set with unique characters in the string.

Finally, you can take len(set) to get the number of unique items in the set.

So, if you add that all together:

>>> word = “CATT”
>>> len(word)
4
>>> my_set = set(word)
>>> my_set  # Sets aren’t ordered, so it will probably come back in a different order
{‘T’, ‘A’, ‘C’}
>>> len(my_set)
3

Huh, 3 != 4, right? It looks like in taking the unique values in “CATT”, you had to throw one of them away because it was duplicated. Hey, convenient! So the short version is:

word = “CATT”
if len(word) != len(set(word)):
    print(word, “has duplicate letters”)

Oh, and because this might answer your next question:

If you have two sets A and B, A <= B is True if and only if every item in A is also in B. For example:

>>> {1,2,3} <= {1,2,3,4}
True
>>> {1,2,3,4} <= {1,2,3}
False

Well, that’s handy, because you can spell a word with a list of letters if and only if every letter in the word is also in the list of letters, right? And that looks a whole lot like the definition of A <= B! And indeed, it is:

>>> letters = [‘a’, ‘b’, ‘c’, ‘t’]
>>> set('cat') <= set(letters)
True
>>> set('dog') <= set(letters)
False

Convenient, isn’t it?

Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
1

given:

lst = [ "A", "B" ,"C","T"]

word = "CAT"

there are many ways to accomplish this.

compare lengths

print(len([i for i in word if word.count(i.upper()) <= lst.count(i.upper())]) == len(word))

all method

print(all([word.count(i.upper()) <= lst.count(i.upper()) for i in word]))

an expanded variation on the all method

word = word.upper()

print(all([word.count(i) <= lst.count(i) for i in word]))
Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • the list of letters randomly generates 10 letters. Sometimes you will have multiple letters so 2 Fs so you can make a word that contains 2 Fs, but how would I detect how many duplicate letters are there every time the list generates? – zxz Nov 19 '20 at 23:27
  • `detect how many duplicate letters` is very vague. that could mean the number of duplicates, the inclusive number of items that have a duplicate, or other things... – Ironkey Nov 19 '20 at 23:31
  • my apologies, I didn't realize I hadn't updated the operators after testing! Let me update with the working code – Ironkey Nov 19 '20 at 23:34
  • hopefully, that should be your solution – Ironkey Nov 19 '20 at 23:37
  • Yeah, it did thank you! – zxz Nov 19 '20 at 23:48
0

Simplest way would be to utilize any() function:

any([user_input.count(letter) > 1 for letter in your_list])

Would return True if any of the letters in your_list is repeated else False

Hamza
  • 5,373
  • 3
  • 28
  • 43