-2

Here I'm asking for a word, and generating a list of all combinations for the letters in word variable, in theory (in my head) the for loop's if statement should work.

from itertools import product
from string import ascii_lowercase

word = input("Enter a word, preferably short")


PossibleWithWordLetters = [''.join(i) for i in product(word, repeat = len(word))]

for a in PossibleWithWordLetters:
    if word.count(word[i]) [for i in range(len(word))] == 1 and a in PossibleWithWordLetters:
        print(a)

The aim of the program is to list all valid anagrams from a word inputted - and checked where it only contains one of each of the letters in word - then i'll check this against a word file of real words - which I haven't got round to yet.

Any help is appreciated on getting the for loop to work.

CryptoCo
  • 79
  • 1
  • 1
  • 7
  • 3
    Don't generate all permutations of the word if you are going to match it against a dictionary. There are much less words in any dictionary than permutations of a 11-12 letter word. Instead, go through you rictionary and check if any word is an anagram of yours – Tamas Hegedus Dec 18 '15 at 12:44
  • I am unable to understand the title (what is this "with to"?). Can you fix it? – adl Dec 18 '15 at 12:44
  • @adl - yes sorry - missed out a word. – CryptoCo Dec 18 '15 at 13:04
  • @hege_hegedus ok that's a good way to think about it, I will try this later today. – CryptoCo Dec 18 '15 at 13:05
  • Think more about what data you actually _need_, and how you would store and retrieve it. If you're allowed to use the standard library, look at the things [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) can do. If that's not allowed, you can do the same things with a dictionary and judicious use of [`dict.get`](https://docs.python.org/3/library/stdtypes.html#dict.get) and [`dict.setdefault`](https://docs.python.org/3/library/stdtypes.html#dict.setdefault). – Kevin J. Chase Dec 18 '15 at 14:10

1 Answers1

0

To be honest I didn't quite understood your question, but it looks like you are trying to generate anagrams of a word, which are permutations of characters. There is no need to count the letters, as python can generate the permutations directly:

from itertools import permutations
from string import ascii_lowercase

word = input("Enter a word, preferably short: ")

PossibleWithWordLetters = [''.join(i) for i in permutations(word)]

for a in PossibleWithWordLetters:
    print(a)

If you want to check if two worstrings are anagrams of each other, here are some examples:

# using string.count
def check_anagram1(a, b):
    return all(a.count(c) == b.count(c) for c in set(a) | set(b))

# using collections.Counter
from collections import Counter
def check_anagram2(a, b):
    return Counter(a) == Counter(b)
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • yeah - sorry I didn't make it very clear - creating the permutations is ok - that looks like a simpler way to do the same thing, it's just then checking to see if `word` letters can be reordered to make another or any word in `PossibleWithWordLetters` - which is what I tried to do with the `for` loop. – CryptoCo Dec 18 '15 at 12:58
  • @RPiPasswordGen With permutations you don't need that check. But just in case, I updated the answer, so you have functions to check if two strings are anagrams – Tamas Hegedus Dec 18 '15 at 13:11