0

EDIT

No longer getting list error after making suggested changes but still not returning any matches. Code now:

# all ingredients, represented by numbers: 0= empty selection 1=rice 2=spice 3=vegetable 
allIng = [0,1,2,3]

#Each individual recipe(r)

# Veggie Rice Balls
r1 = (0,1,3)

# Curry Rice
r2 =(0,1,2)

# Herb Sauté
r3 = (0,2,3)

# Vegetable Curry
r4 = (1,2,3)


# all recipes on one list 

allRec = [r1,r2,r3,r4]


#ingredients picked
iP = []
#ingredient count
iC = 1

#User given option to pick up to 3 ingredients
while iC <= 3:
    pitem = int (input ("Pick up to 3 items "))

    if pitem in allIng:
        iP.append(pitem)
        print(iP)
        iC += 1
    else:
        print ("Incorrect entry, please pick again")

#sort list
iP.sort()
tuple(iP)

#compare iP to allRec looking for matches
if iP in allRec:

    matches = set (iP) & set(allRec)
    print ("Matches:",matches)

Trying to get it print out which recipe matched and if possible tag the name of the recipe itself.

3
  • set(allRec) is set([r1,r2,r3,r4]) which is set([[0,1,3], ...]). It is reading r1 as [0, 1, 3], and that's a list which isn't hashable and therefore not a valid member of a set. Commented Jan 19, 2020 at 11:52
  • So I'm definitely going to blame this on my lack of understanding. But why does it work if I use set (iP) & set (r1)? Is r1 then being seen as a individual elements vs a list as a whole? Commented Jan 19, 2020 at 12:04
  • Because set(r1) is set([0, 1, 3]), which is making a set containing integers. Integers are immutable, hashable, valid set members. Commented Jan 19, 2020 at 12:05

2 Answers 2

2

Lists are unhashable since they can be modified during runtime. So instead of lists, try using (non-mutable) tuples - you can define r1 through r4 using parentheses instead of brackets, and convert iP to a tuple after sorting. You can then use sets of tuples with no issues.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for looking at this! When I make those changes, I no longer get the error but it is no longer returning match or showing any match. Editing my main post with these changes.
Two things to make it work: (1) tuple() does not work in-place, so you need to use iP = tuple(iP); (2) to match the format of allRec (set of tuples), within the loop you need to substitute set(iP) with e.g. set([iP]). As pointed out by @keval-dave below, you can make it work using lists only, but yours is an excellent practice example to highlight some common issues of mutable/immutable objects in Python and handling sets.
Sorry for the delay but just wanted to say thank you! That did it!
1

This is because of allRes is a list of the list. Which cannot be converted to the Set.

To get the index of the recipe or recipe you can use the following snippet.

index = allRec.index(iP)
recipe = allRec[index]

You will need no change all the recipes to the set; if ingredients are entered in a different order.

Comments

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.