0

I'm working on a program that plays "Guess Who?". I want a computer player to decide on the best question to ask.

I already have code which can determine the expected value for how many candidates will be eliminated for a certain guess, which I compute in separate variables and put into a list:

# blackHairExpect = expected number of candidates remaining
# by asking about black hair; etc.
compareExpect = [blackHairExpect, blondeHairExpect, ...]

Then I try to get the lowest value:

minExpect = min(compareExpect)

However, this only tells me how good the best guess is; it doesn't tell me which guess to use. How can I find that out? For example, if blackHairExpect was the lowest value, how can I know that this value results from asking about black hair?

I tried isolating the lowest value(s):

lowestExpect = [x for x in compareExpect if x == min(compareExpect)]

but this also does not solve the problem.

3
  • please provide a minimal reproducible example. There is no need for ambiguity here -- that's the whole point of programming languages. Don't be lazy - help us help you. Commented Jun 14, 2022 at 20:51
  • "I am looking for the lowest value. min(compareExpect) gives me the value, but I need to find the attribute that it belongs to." It is only possible to get information from the list that is actually in the list. Lists store values, not variables. Once you have created compareExpect = [blackHairExpect, blondeHairExpect, ...], there is no way to know that compareExpect[0] came from blackHairExpect any more. (After all, it is perfectly possible to fill the list with values that didn't come from any variable at all.) If you want to track this information, you must include it somehow. Commented Jun 14, 2022 at 20:54
  • @juanpa.arrivillaga I think there is more than enough information to understand the problem here - too much, if anything. Commented Jun 14, 2022 at 20:55

1 Answer 1

1

Use a list of tuples of the attribute name and value. Then you can minimize the value and return the whole tuple.

compareExpect = [('blackHair', blackHairExpect), ('blondeHair', blondHairExpect), ...]
minAttr, minValue = min(compareExpect, key=lambda e: e[1])
Sign up to request clarification or add additional context in comments.

3 Comments

Based on my reading (and subsequent editing) of the question, I don't think OP actually needs the lowestExpect calculation - it was just a failed prior attempt to solve the problem.
The question says "it doesn't tell me which guess to use" -- he wants to know which guess had the lowest value. And if there are ties, this returns a list of all of them.
Right, but my point is the minExpect is one of such guesses, and it doesn't appear that there's a requirement to find them all - if you can find an optimal guess, you can make an optimal guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.