'How to get smart guess from words list for target words list so the target must be guessed correctly within 3.5 turns on average

I am trying to make a Lingo game in python but i am not getting quick output. I use random.choice() to short the list of target words but still the average turn for 6 letters word takes more than average of 3.5 turns,it should be within 3.5 turns on average.

def load_words(file):
    result = set()
    with open(file) as f:
        for line in f.readlines():
            word = line.strip().lower()
            if word.isalpha() and word.isascii():
                result.add(word)
    return sorted(result)


def compare(guess, target):
    result = ['-']*len(guess)
    L = []
    for i in range(len(guess)):
        if guess[i] == target[i]:
            result[i] = 'X'
        else:
            L.append(target[i])

    for i in range(len(guess)):
        if guess[i] in L and result[i] != 'X':
            result[i] = 'O'
            L.remove(guess[i])
    return "".join(result)


def filter_targets(targets, guess_results):
    result = []
    for target in targets:
        if all(compare(guess, target) == guess_results[guess] for guess in guess_results):
            result.append(target)
    return result


def distribution(guess, targets):
    d = {}
    for target in targets:
        res = compare(guess,target)
        if res not in d:
            d[res] = 0
        d[res] += 1
    return d


def simulate_game(target, wordlist):
    n = len(target)
    wordlist = [w for w in wordlist if len(w) == n and w[0] == target[0]]
    if target not in wordlist:
        raise ValueError("Target is not in wordlist, thus impossible to guess.")
    targets = wordlist.copy()
    turns = 0
    while True:
        num_words = len(targets)
        print(f"There {'is' if num_words==1 else 'are'} {num_words} possible"
            f" target{'s' if num_words!=1 else ''} left.")
        turns += 1
        guess = smart_guess(wordlist, targets)
        print("My guess is: ", guess.upper())
        result = compare(guess, target)
        print("Correctness: ", result)
        if result == n * "X":
            print(f"Target was guessed in {turns} "
            f"turn{'s' if turns!=1 else ''}.")
            print("ok", turns)
            break
        else:
            targets = filter_targets(targets, {guess: result})




def smart_guess(wordlist, targets):
    return random.choice(targets)```


```# english_words = load_words("words.txt")
dutch_words = load_words("wordlist.txt")
#
# english_6 = [word for word in english_words if len(word) == 6]
# dutch_5j = [word for word in dutch_words if len(word) == 5 and
# word.startswith("j")]
d6 = [word for word in dutch_words if len(word) == 6]

d6q = [word for word in d6 if word.startswith("q")]
dw5s = [word for word in d6 if word.startswith("s")]
#print(smart_guess(dw5s, dw5s))
#distribution("quizje", d6q)44
simulate_game("aniline", dutch_words)```

I need quick way to get smart guess so it takes minimal turns to guess target word from targets(list) based on words list



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source