#๐Ÿ”’ getting a certain uplet in a dictionary

98 messages ยท Page 1 of 1 (latest)

void saffron
#

I have a dictionary in which are lists composed of tuples and i want to check if the tuples i ask for is in the dictionary. heres an example of what i m trying to do:
i have a coordinate (1,1) ->one of the keys of my dictionnary from here you can move to [(1,2),(2,1),(5,1)] -> values of the key (1,1). so i want to ask the person through the console where they want to move. To do that i want to check if its in the values of the key (1,1) and then i want to repeat the whole process until i reach a certain coordinate that i told them to reach.

real thornBOT
#

@void saffron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

barren ferry
void saffron
#

tuples xd idk why i always say that

#

it has become a common joke with my friend now

barren ferry
#

Can you show an example dictionary structure?

void saffron
#

i want to basicly check if the move they want to do is a possible move

#

i know i should get the list of tuples from the previous coordinate and see if the one they want to move to is in this list

#

i just don t know how to do it

barren ferry
#

Which list in that dictionary are you wanting to check for a example input?

void saffron
#

if my start is (0,0) and the input is (1,1) i want to check if (1,1) is in the key (0,0)

barren ferry
#

Oh, this sounds pretty simple. You should just need to get the value of the 0,0 key, and then check that list.

void saffron
#

if

#

if value in key[(0,0)] like this?

barren ferry
#

Assuming the dictionary is called key, then yes, that looks correct.

void saffron
#

ok nice

#

how do i ask for

#

a tuple through my console

#

i used int(input())

barren ferry
#

!e

key = {(0, 0): [(1, 0), (2, 0), (4, 0)], (1, 0): [(0, 0), (2, 0)], (2, 0): [(0, 0), (1, 0), (5, 0), (2, 2)], (0, 2): [(0, 3), (1, 2)], (0, 3): [(0, 2), (0, 5), (2, 3), (3, 3)], (4, 0): [(0, 0), (4, 1), (4, 4)], (5, 0): [(2, 0), (5, 2), (5, 5)], (0, 5): [(0, 3), (1, 5)], (0, 1): [(1, 1), (2, 1), (3, 1)], (1, 1): [(0, 1), (1, 2), (3, 1), (1, 4), (4, 1)], (2, 1): [(0, 1), (5, 1), (2, 4)], (1, 2): [(1, 1), (0, 2)], (3, 1): [(0, 1), (1, 1), (5, 1)], (1, 3): [(1, 5), (4, 3)], (1, 4): [(1, 1), (1, 5), (0, 4), (2, 4), (3, 4)], (4, 1): [(1, 1), (4, 0), (4, 4)], (5, 1): [(2, 1), (3, 1)], (1, 5): [(1, 3), (1, 4), (0, 5), (3, 5)], (2, 2): [(2, 0), (3, 2), (4, 2), (2, 5)], (3, 2): [(2, 2), (4, 2), (3, 0), (3, 3)], (2, 3): [(2, 5), (0, 3), (5, 3)], (2, 4): [(2, 1), (0, 4), (1, 4)], (4, 2): [(2, 2), (3, 2), (5, 2), (4, 4)], (2, 5): [(2, 2), (2, 3), (5, 5)], (5, 2): [(4, 2), (5, 0), (5, 5)], (3, 0): [(3, 2), (3, 3), (3, 4), (3, 5)], (3, 3): [(0, 3), (3, 0), (3, 2), (4, 3), (5, 3)], (3, 4): [(3, 0), (3, 5), (1, 4), (4, 4)], (4, 3): [(1, 3), (3, 3), (5, 3)], (3, 5): [(3, 0), (3, 4), (1, 5), (4, 5)], (5, 3): [(2, 3), (3, 3), (4, 3), (5, 4)], (0, 4): [(1, 4), (2, 4)], (4, 4): [(4, 0), (4, 1), (4, 2), (3, 4)], (5, 4): [(5, 3)], (4, 5): [(3, 5)], (5, 5): [(5, 0), (5, 2), (2, 5)]}
print( (2, 0) in key[0,0] )
real thornBOT
barren ferry
void saffron
#

but tuples aren t int

#

what are they?

barren ferry
#

Tuples

void saffron
#

tuple(input()) ?

#

this seams wrong ๐Ÿคฃ

barren ferry
#

No, accepting a tuple will be slightly harder than a single int. What exactly are you wanting the user to type?

void saffron
#

the next coordinate like (1,1)

barren ferry
#

If you want them to include the () and comma, ast.literal_eval would be the cleanest way.

void saffron
#

and what other alternative are there?

barren ferry
#

Accepting it as a string and parsing the string yourself. Or changing the format you expect the user to enter the data (like 1 1 instead of (1, 1)).

void saffron
#

hmmm

#

so technically i could accept it as a string but if they make a typo its going to count it as wrong?

barren ferry
void saffron
#

i see

void saffron
#

ive done this but it says key error: ```py
dic_Voisin= {(0, 0): [(1, 0), (2, 0), (4, 0)], (1, 0): [(0, 0), (2, 0)], (2, 0): [(0, 0), (1, 0), (5, 0), (2, 2)], (0, 2): [(0, 3), (1, 2)], (0, 3): [(0, 2), (0, 5), (2, 3), (3, 3)], (4, 0): [(0, 0), (4, 1), (4, 4)], (5, 0): [(2, 0), (5, 2), (5, 5)], (0, 5): [(0, 3), (1, 5)], (0, 1): [(1, 1), (2, 1), (3, 1)], (1, 1): [(0, 1), (1, 2), (3, 1), (1, 4), (4, 1)], (2, 1): [(0, 1), (5, 1), (2, 4)], (1, 2): [(1, 1), (0, 2)], (3, 1): [(0, 1), (1, 1), (5, 1)], (1, 3): [(1, 5), (4, 3)], (1, 4): [(1, 1), (1, 5), (0, 4), (2, 4), (3, 4)], (4, 1): [(1, 1), (4, 0), (4, 4)], (5, 1): [(2, 1), (3, 1)], (1, 5): [(1, 3), (1, 4), (0, 5), (3, 5)], (2, 2): [(2, 0), (3, 2), (4, 2), (2, 5)], (3, 2): [(2, 2), (4, 2), (3, 0), (3, 3)], (2, 3): [(2, 5), (0, 3), (5, 3)], (2, 4): [(2, 1), (0, 4), (1, 4)], (4, 2): [(2, 2), (3, 2), (5, 2), (4, 4)], (2, 5): [(2, 2), (2, 3), (5, 5)], (5, 2): [(4, 2), (5, 0), (5, 5)], (3, 0): [(3, 2), (3, 3), (3, 4), (3, 5)], (3, 3): [(0, 3), (3, 0), (3, 2), (4, 3), (5, 3)], (3, 4): [(3, 0), (3, 5), (1, 4), (4, 4)], (4, 3): [(1, 3), (3, 3), (5, 3)], (3, 5): [(3, 0), (3, 4), (1, 5), (4, 5)], (5, 3): [(2, 3), (3, 3), (4, 3), (5, 4)], (0, 4): [(1, 4), (2, 4)], (4, 4): [(4, 0), (4, 1), (4, 2), (3, 4)], (5, 4): [(5, 3)], (4, 5): [(3, 5)], (5, 5): [(5, 0), (5, 2), (2, 5)]}
fin= (random.randint(0,5),random.randint(0, 5))
debut=(random.randint(0,5),random.randint(0, 5))
while debut!= fin:
chemin = str(input("chosit quel case ou aller (ligne,colonne): "))
if chemin in dic_Voisin[debut]:
debut = chemin
print ("tu es maintenat en:",chemin)
else:
print("tu ne peux pas aller la")

#

getting an error in a while loop

void saffron
barren ferry
#

Sorry, my Dad come over to repot a cacti and we got talking.

void saffron
#

np

#

hope you did not sting urself

barren ferry
#

No, he grows thousands of cacti, so he's well prepared. He's currently growing Saguaros in Canada.

void saffron
#

wow cool

barren ferry
#

Ya, he has so many he needs to offload them on us because their house is full.

barren ferry
#

He grows them from seeds, and they take decades to grow, so they're all very tiny still.

void saffron
#

oh okk baby cacti

#

!e dic_Voisin= {(0, 0): [(1, 0), (2, 0), (4, 0)], (1, 0): [(0, 0), (2, 0)], (2, 0): [(0, 0), (1, 0), (5, 0), (2, 2)], (0, 2): [(0, 3), (1, 2)], (0, 3): [(0, 2), (0, 5), (2, 3), (3, 3)], (4, 0): [(0, 0), (4, 1), (4, 4)], (5, 0): [(2, 0), (5, 2), (5, 5)], (0, 5): [(0, 3), (1, 5)], (0, 1): [(1, 1), (2, 1), (3, 1)], (1, 1): [(0, 1), (1, 2), (3, 1), (1, 4), (4, 1)], (2, 1): [(0, 1), (5, 1), (2, 4)], (1, 2): [(1, 1), (0, 2)], (3, 1): [(0, 1), (1, 1), (5, 1)], (1, 3): [(1, 5), (4, 3)], (1, 4): [(1, 1), (1, 5), (0, 4), (2, 4), (3, 4)], (4, 1): [(1, 1), (4, 0), (4, 4)], (5, 1): [(2, 1), (3, 1)], (1, 5): [(1, 3), (1, 4), (0, 5), (3, 5)], (2, 2): [(2, 0), (3, 2), (4, 2), (2, 5)], (3, 2): [(2, 2), (4, 2), (3, 0), (3, 3)], (2, 3): [(2, 5), (0, 3), (5, 3)], (2, 4): [(2, 1), (0, 4), (1, 4)], (4, 2): [(2, 2), (3, 2), (5, 2), (4, 4)], (2, 5): [(2, 2), (2, 3), (5, 5)], (5, 2): [(4, 2), (5, 0), (5, 5)], (3, 0): [(3, 2), (3, 3), (3, 4), (3, 5)], (3, 3): [(0, 3), (3, 0), (3, 2), (4, 3), (5, 3)], (3, 4): [(3, 0), (3, 5), (1, 4), (4, 4)], (4, 3): [(1, 3), (3, 3), (5, 3)], (3, 5): [(3, 0), (3, 4), (1, 5), (4, 5)], (5, 3): [(2, 3), (3, 3), (4, 3), (5, 4)], (0, 4): [(1, 4), (2, 4)], (4, 4): [(4, 0), (4, 1), (4, 2), (3, 4)], (5, 4): [(5, 3)], (4, 5): [(3, 5)], (5, 5): [(5, 0), (5, 2), (2, 5)]}
fin= (random.randint(0,5),random.randint(0, 5))
debut=(random.randint(0,5),random.randint(0, 5))
while debut!= fin:
chemin = str(input("chosit quel case ou aller (ligne,colonne): "))
if chemin in dic_Voisin[debut]:
debut = chemin
print ("tu es maintenat en:",chemin)
else:
print("tu ne peux pas aller la")

real thornBOT
void saffron
#

!e
import random
dic_Voisin= {(0, 0): [(1, 0), (2, 0), (4, 0)], (1, 0): [(0, 0), (2, 0)], (2, 0): [(0, 0), (1, 0), (5, 0), (2, 2)], (0, 2): [(0, 3), (1, 2)], (0, 3): [(0, 2), (0, 5), (2, 3), (3, 3)], (4, 0): [(0, 0), (4, 1), (4, 4)], (5, 0): [(2, 0), (5, 2), (5, 5)], (0, 5): [(0, 3), (1, 5)], (0, 1): [(1, 1), (2, 1), (3, 1)], (1, 1): [(0, 1), (1, 2), (3, 1), (1, 4), (4, 1)], (2, 1): [(0, 1), (5, 1), (2, 4)], (1, 2): [(1, 1), (0, 2)], (3, 1): [(0, 1), (1, 1), (5, 1)], (1, 3): [(1, 5), (4, 3)], (1, 4): [(1, 1), (1, 5), (0, 4), (2, 4), (3, 4)], (4, 1): [(1, 1), (4, 0), (4, 4)], (5, 1): [(2, 1), (3, 1)], (1, 5): [(1, 3), (1, 4), (0, 5), (3, 5)], (2, 2): [(2, 0), (3, 2), (4, 2), (2, 5)], (3, 2): [(2, 2), (4, 2), (3, 0), (3, 3)], (2, 3): [(2, 5), (0, 3), (5, 3)], (2, 4): [(2, 1), (0, 4), (1, 4)], (4, 2): [(2, 2), (3, 2), (5, 2), (4, 4)], (2, 5): [(2, 2), (2, 3), (5, 5)], (5, 2): [(4, 2), (5, 0), (5, 5)], (3, 0): [(3, 2), (3, 3), (3, 4), (3, 5)], (3, 3): [(0, 3), (3, 0), (3, 2), (4, 3), (5, 3)], (3, 4): [(3, 0), (3, 5), (1, 4), (4, 4)], (4, 3): [(1, 3), (3, 3), (5, 3)], (3, 5): [(3, 0), (3, 4), (1, 5), (4, 5)], (5, 3): [(2, 3), (3, 3), (4, 3), (5, 4)], (0, 4): [(1, 4), (2, 4)], (4, 4): [(4, 0), (4, 1), (4, 2), (3, 4)], (5, 4): [(5, 3)], (4, 5): [(3, 5)], (5, 5): [(5, 0), (5, 2), (2, 5)]}
fin= (random.randint(0,5),random.randint(0, 5))
debut=(random.randint(0,5),random.randint(0, 5))
while debut!= fin:
chemin = str(input("chosit quel case ou aller (ligne,colonne): "))
if chemin in dic_Voisin[debut]:
debut = chemin
print ("tu es maintenat en:",chemin)
else:
print("tu ne peux pas aller la")

real thornBOT
# void saffron !e import random dic_Voisin= {(0, 0): [(1, 0), (2, 0), (4, 0)], (1, 0): [(0, 0...

:x: Your 3.12 eval job has completed with return code 1.

:warning: Note: input is not supported by the bot :warning:

001 | chosit quel case ou aller (ligne,colonne): Traceback (most recent call last):
002 |   File "/home/main.py", line 6, in <module>
003 |     chemin = str(input("chosit quel case ou aller (ligne,colonne): "))
004 |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
005 | EOFError: EOF when reading a line
void saffron
#

oh

barren ferry
#

Can't use input with !e.

void saffron
#

i though i could show you on here

barren ferry
#

Just paste the traceback you got on your machine.

void saffron
#

but here no matter what i do it doesn t recognise the coordinates i put in

#

it says in a loop : "tu ne peux pas aller la" which what i ask him to print if its not a coordinate i can go to

barren ferry
#
chemin = str(input("chosit quel case ou aller (ligne,colonne): "))
if chemin in dic_Voisin[debut]:

chemin is a string, so that will never be in the list.

#

THe list contains tuples.

void saffron
#

oh

barren ferry
#

!e

print('(1,1)' in [(1, 1)])
void saffron
#

so how do i see if its a tuple in the list

real thornBOT
barren ferry
#

You need to convert chemin to a tuple like I described above.

void saffron
#

what is input validation?

barren ferry
#

Ensuring the user gave you the correct data in the correct format.

void saffron
#

but how will that tell me if that tuple is in the list in the dictionary?

#

if chemin in dic_Voisin[debut]:

#

do i need to do : if chemin in dic_Voisin[[debut]]: this?

barren ferry
#

I mentioned input validation because that's a pretty standard part of taking input from the user. Eventually, you'll be expected to handle that.

The real thing you need to handle is turning chemin into a tuple. I mentioned ast.literal_eval, and just parsing the string yourself if you don't want literal_eval.

void saffron
#

if i do this i end up with unhashable type: 'list'

#

how is ast.literal_eval used?

#

instead of str

#

?

barren ferry
#

!e

import ast

string_tup = '(1, 2)'
tup = ast.literal_eval(string_tup)
print(tup, type(tup))
real thornBOT
barren ferry
#

Or, just do your own parsing with functions like str.split.

#

Split the string on the comma, then remove the () to get the numbers, and make the tuple yourself.

void saffron
#

how can i make a tuple myself?

barren ferry
#
num1 = 1
num2 = 2
tup = num1, num2 
void saffron
#

oh ok

barren ferry
#

Whoops, fubared that example hard

void saffron
#

xdd

#

so i can ask for 2 number then make then a tuple than see if that tuple is in the list of the key in the dic?

#

thats a lot of in of in haha

barren ferry
#

I gotta make lunch now though. Good luck.

void saffron
#

ty!

real thornBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ getting an error in a while loop

real thornBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.