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.
#๐ getting a certain uplet in a dictionary
98 messages ยท Page 1 of 1 (latest)
@void saffron
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.
Closes after a period of inactivity, or when you send !close.
"Uplet": I'm assuming you mean "tuple"?
Can you show an example dictionary structure?
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
Which list in that dictionary are you wanting to check for a example input?
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)
Oh, this sounds pretty simple. You should just need to get the value of the 0,0 key, and then check that list.
Assuming the dictionary is called key, then yes, that looks correct.
!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] )
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
int won't work there because a tuple isn't an int.
Tuples
No, accepting a tuple will be slightly harder than a single int. What exactly are you wanting the user to type?
the next coordinate like (1,1)
If you want them to include the () and comma, ast.literal_eval would be the cleanest way.
and what other alternative are there?
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)).
hmmm
so technically i could accept it as a string but if they make a typo its going to count it as wrong?
You would need to do the input validation yourself and re-ask for input or something in that case.
i see
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
idk if you know what ive done wrong here
Sorry, my Dad come over to repot a cacti and we got talking.
No, he grows thousands of cacti, so he's well prepared. He's currently growing Saguaros in Canada.
wow cool
Ya, he has so many he needs to offload them on us because their house is full.
how tall is it?
xdd
He grows them from seeds, and they take decades to grow, so they're all very tiny still.
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")
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 2, in <module>
003 | fin= (random.randint(0,5),random.randint(0, 5))
004 | ^^^^^^
005 | NameError: name 'random' is not defined. Did you forget to import 'random'?
!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")
: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
oh
Can't use input with !e.
i though i could show you on here
Just paste the traceback you got on your machine.
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
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.
oh
!e
print('(1,1)' in [(1, 1)])
so how do i see if its a tuple in the list
:white_check_mark: Your 3.12 eval job has completed with return code 0.
False
You need to convert chemin to a tuple like I described above.
Here and the comments under.
what is input validation?
Making sure (validating) that data is what you expect it to be.
Ensuring the user gave you the correct data in the correct format.
yes but i don t even need to check that i can just trust he does
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?
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.
if i do this i end up with unhashable type: 'list'
how is ast.literal_eval used?
instead of str
?
!e
import ast
string_tup = '(1, 2)'
tup = ast.literal_eval(string_tup)
print(tup, type(tup))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
(1, 2) <class 'tuple'>
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.
how can i make a tuple myself?
num1 = 1
num2 = 2
tup = num1, num2
oh ok
Whoops, fubared that example hard
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
Yes, that sounds about right.
I gotta make lunch now though. Good luck.
ty!
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
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.