#๐Ÿ”’ help with dictionaries

205 messages ยท Page 1 of 1 (latest)

ember knotBOT
#

@rustic wasp

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.

south mauve
#

!e

dic = {}

key = 'a'
val = 3

if dic.get(key):
    print("Key exists")
else:
    print("Added to dic")
    dic[key] = val

print(dic)
ember knotBOT
#

@south mauve :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Added to dic
002 | {'a': 3}
rustic wasp
south mauve
#

yes

rustic wasp
#

and if it is already created then you add an element

south mauve
#

perhaps I should show a more extensive example

#

!e

dic = {}

key = 'a'
val = 3

if dic.get(key):
    print("Key exists")
else:
    print("Added to dic")
    dic[key] = val

# Now, the key has been created

if dic.get(key):
    print("The key finally exists")
    dic[key] = dic[key] + 1
else:
    print("The key doesn't exist...")

print(dic)
ember knotBOT
#

@south mauve :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Added to dic
002 | The key finally exists
003 | {'a': 4}
south mauve
#

hope that's a bit more helpful

rustic wasp
#

what i ve understood is i check if my key (1,1) exists if it does than i add my value. if it doesn't i creat (1,1) and add my value to it

south mauve
#

It checks if the key exists, if it does you can add to the value
If it doesn't, you can create the key with a default value

rustic wasp
#

ok thanks a lot

rustic wasp
#

never mind

south mauve
rustic wasp
#

xd

#

i checked in a list if i had a key๐Ÿ˜‚

#

hmmm i seem to have a problem

#

if L.get(ligne+1,s+1):
[ligne+1,s+1].append(carte[ligne][e])
else:
L[ligne+1,s+1]=carte[ligne][e]

#

i wrote this with the key that i want to check if it exists beeing: L[ligne+1,s+1]

#

but the console feeds bacl {} so it did not creat any key

south mauve
#

if L.get((ligne+1, s+1)):

rustic wasp
#

ooooh

#

sorry for beeing this noobie

south mauve
#

it's alright, this server is to help people learn python :)

rustic wasp
#

how do i add a list to a key in dictionary python do i use append or is that only for lists

#

and can a key have multiple lists?

south mauve
#

wdym

rustic wasp
#

like (1, 3): [4, 'purple'],[3,'red']

south mauve
#

sure, that exists

#

!e

key = (1, 3)
value = ([4, 'purple'], [3, 'red'])

dic = {}
dic[key] = value

print(dic)
ember knotBOT
#

@south mauve :white_check_mark: Your 3.12 eval job has completed with return code 0.

{(1, 3): ([4, 'purple'], [3, 'red'])}
south mauve
#

just like any other value

#

note the parens here: ([4, 'purple'], [3, 'red']) which show that both of these lists are the value of that key

rustic wasp
#

i see

#

but here in your exemple : value = ([4, 'purple'], [3, 'red']) value is already equal to all this whereas i have two values : value 1= [4, 'purple'] and value 2 = [3, 'red'] that i want to add to one single key

#

to make (1, 3): ([4, 'purple'], [3, 'red'])

south mauve
#

are you saying you want to make it so that dic[1] gives [4, 'purple']

rustic wasp
#

not really

#

i want to add another value to my dic

#

but in you example value is 2 elements already

south mauve
#

ok so you have a dic, it has 2 key-value pairs, you want to join them?

rustic wasp
#

i have a dic with 1 key 1 pair inside and i want to add another pair to that key

south mauve
#

ah

#

ok

#

Not sure if you can edit the key...

#

give me a second

#

!e though I would say it sounds like you're getting tunnel vision.

to_add_key = 3
to_add_value = [3, 'red']
key_to_add_to = 1

dic = {1: [4, 'purple']}
print("before: ", dic)

prev_val = dic.pop(key_to_add_to)

dic[(key_to_add_to, to_add_key)] = prev_val, to_add_value

print("after: ", dic)
ember knotBOT
#

@south mauve :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | before:  {1: [4, 'purple']}
002 | after:  {(1, 3): ([4, 'purple'], [3, 'red'])}
south mauve
#

whatever you hope to achieve with this, there's probably a better (and less complicated) way of achieving it

rustic wasp
#

should i explain what i m trying to do?

south mauve
#

go for it, but I have to run soon

#

it's almost 4am

rustic wasp
#

oh shoot

south mauve
#

I'll help if I can, but if I can't, I'm sure someone else will

rustic wasp
#

the aim is to creat a game where you go from 1 colored number to another

#

its on a board

#

and the only way you can move is vertically and horizontally and on either a same colored or same numbered piece

#

i m trying to find every possible move on the board

#

[2, 'bleu'], [6, 'jaune'], [5, 'rose'], [4, 'rose'], [1, 'blanc'], [4, 'vert']

#ย [4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']

[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']

[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']

[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']

#ย [3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']

#

here for example you can go from [5, 'rose'] to [4, 'rose'] because they have rose in common

#

but you can also move from [5, 'rose'] to [6, 'rose'] because they are in the same collumn

#

so i need to make a dictionnary where all the combos are possible

tame sail
#

Why a dictionary?

rustic wasp
#

cuz then i have to make the fastes path between one random to another

south mauve
#

there's algorithms for that

rustic wasp
#

and by having a dic of all the combos i can find the path more easly?

#

i could use something else than a dic but then i have to change my code for the quickest path

south mauve
#

though I am slightly confused by your board, that might just be because of how tired I am

#

I should probably go

rustic wasp
#

the board form the actual game is this

#

so in my dic i want to have (1,1) the first square on the top left : ([6,'pink'], [2,'pink],[4,pink],[5,'red])

#

all the possible moves from 5 pink

#

on python my board is a list with in it a list for each line containing in them another list with the number and the color in it

#

like this (its in french) : carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

rustic wasp
tame sail
#

how do i add a value to a key but if this key does not existe i have to create it

#

you can use a default dict for this

#

!e

from collections import defaultdict

moves = defaultdict(list)

for a in range(5):
    for b in range(5):
        moves[a].append(b)

print(moves)```
ember knotBOT
#

@tame sail :white_check_mark: Your 3.12 eval job has completed with return code 0.

defaultdict(<class 'list'>, {0: [0, 1, 2, 3, 4], 1: [0, 1, 2, 3, 4], 2: [0, 1, 2, 3, 4], 3: [0, 1, 2, 3, 4], 4: [0, 1, 2, 3, 4]})
rustic wasp
#

what i have done rn is this (its made by me i dont have a lot of experiance): L={}
for ligne in range (0,6):
for s in range (0,6):
for e in range (0,6):
for i in range (0,2):
if carte[ligne][s][i] == carte[ligne][e][i] and e!=s:
if L.get((ligne+1,s+1)):
[(ligne+1,s+1)].append(carte[ligne][e])
else:
L[(ligne+1,s+1)]=carte[ligne][e]
C={}
for colonne in range (0,6):
for compare in range (0,6):
for ligne in range (0,6):
for i in range (0,2):
if carte[ligne][colonne][i] == carte[compare][colonne][i] and ligne!= compare:
C[ligne+1,colonne+1]=carte[compare][colonne]

#

first part is to find where i can move in the ligne second one is in the column

tame sail
#

it's a start

rustic wasp
#

xd it is what it is

rustic wasp
tame sail
#

I can stay for a bit but I have to leave soon

#

what part is broken?

rustic wasp
#

if carte[ligne][s][i] == carte[ligne][e][i] and e!=s:
if L.get((ligne+1,s+1)):
[(ligne+1,s+1)].append(carte[ligne][e])
else:
L[(ligne+1,s+1)]=carte[ligne][e]

ember knotBOT
#

Hey @rustic wasp!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
rustic wasp
#

i can t manage to have a dic with multiple values in my keys

tame sail
#

why do you use 1 based indexing that's making it hard on yourself

rustic wasp
#

1 based indexing?

tame sail
#

you are adding +1 to your indexes

rustic wasp
#

hmm what should i do?

tame sail
#

L[(ligne+1,s+1)]=carte[ligne][e]
You need an extra [] around the carte[ligne][e]

rustic wasp
#

hmm okkk let me try

tame sail
#

I would recommend the defaultdict solution I talked about earlier instead though

rustic wasp
#

!e

ember knotBOT
#
Missing required argument

code

tame sail
#

[(ligne+1,s+1)].append(carte[ligne][e])
also you forgot to put the L in front of here

rustic wasp
#

oh

tame sail
#

It's easy to miss if you use non descriptive 1 letter variable names like that

rustic wasp
#

xddd yeah

rustic wasp
#

now with this code carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

dic_Voisin={}
a={}
for ligne in range (0,6):
for case in range (0,6):
for voisin in range (0,6):
for i in range (0,2):
if carte[ligne][case][i] == carte[ligne][voisin][i] and voisin!=case:
if dic_Voisin.get((ligne,case)):
dic_Voisin[(ligne,case)].append(carte[ligne][voisin])
else:
dic_Voisin[(ligne,case)]=[carte[ligne][voisin]]
if carte[voisin][ligne][i] == carte[case][ligne][i] and voisin!= case:

                    if a.get((ligne,case)):
                        a[(ligne,case)].append(carte[voisin][ligne])
                    else:
                        a[(ligne,case)]=[carte[voisin][ligne]]
#

when i have 2 seperate dico it works

#

but when i put them in one it doesn t work anymore

wise furnace
#

!code You should format your code when you paste it here otherwise the indentation gets weird

ember knotBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

rustic wasp
#

ok

#

!code carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

dic_Voisin={}
a={}
for ligne in range (0,6):
for case in range (0,6):
for voisin in range (0,6):
for i in range (0,2):
if carte[ligne][case][i] == carte[ligne][voisin][i] and voisin!=case:
if dic_Voisin.get((ligne,case)):
dic_Voisin[(ligne,case)].append(carte[ligne][voisin])
else:
dic_Voisin[(ligne,case)]=[carte[ligne][voisin]]
if carte[voisin][ligne][i] == carte[case][ligne][i] and voisin!= case:

                    if a.get((ligne,case)):
                        a[(ligne,case)].append(carte[voisin][ligne])
                    else:
                        a[(ligne,case)]=[carte[voisin][ligne]]
#

!e

ember knotBOT
#
Missing required argument

code

rustic wasp
#

/home

#

!e carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

dic_Voisin={}
a={}
for ligne in range (0,6):
for case in range (0,6):
for voisin in range (0,6):
for i in range (0,2):
if carte[ligne][case][i] == carte[ligne][voisin][i] and voisin!=case:
if dic_Voisin.get((ligne,case)):
dic_Voisin[(ligne,case)].append(carte[ligne][voisin])
else:
dic_Voisin[(ligne,case)]=[carte[ligne][voisin]]
if carte[voisin][ligne][i] == carte[case][ligne][i] and voisin!= case:

                    if a.get((ligne,case)):
                        a[(ligne,case)].append(carte[voisin][ligne])
                    else:
                        a[(ligne,case)]=[carte[voisin][ligne]]
ember knotBOT
#

@rustic wasp :warning: Your 3.12 eval job has completed with return code 0.

[No output]
rustic wasp
#

hmmm

rustic wasp
wise furnace
#

You format code by surrounding it with ```py on top and ``` on the bottom of it.

#

typing !code <your code> doesn't do anything except make the bot tell you how to format code.

#

backticks, not apostrophe

#

If you have a US qwerty keyboard it's probably to the left of the 1 key on top.

ember knotBOT
#

Hey @rustic wasp!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
rustic wasp
#

is this better?

#

'''py '''

#
carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

dic_Voisin={}
a={}
for ligne in range (0,6):    
        for case in range (0,6):
            for voisin in range (0,6):                 
                for i in range (0,2):
                    if carte[ligne][case][i] == carte[ligne][voisin][i] and voisin!=case:
                        if dic_Voisin.get((ligne,case)):
                            dic_Voisin[(ligne,case)].append(carte[ligne][voisin])
                        else:
                            dic_Voisin[(ligne,case)]=[carte[ligne][voisin]]
                    if carte[voisin][ligne][i] ==  carte[case][ligne][i] and voisin!= case:
                        
                        if a.get((ligne,case)):
                            a[(ligne,case)].append(carte[voisin][ligne])
                        else:
                            a[(ligne,case)]=[carte[voisin][ligne]]

wise furnace
#

excellent!

#

That's some deeply-nested for looping going on.

rustic wasp
#

so here i get two dic that are what i want

wise furnace
#

You see how case is a different color there?

rustic wasp
#
carte=[[[2, 'bleu'],[6, 'jaune'],[5, 'rose'],[4, 'rose'], [1, 'blanc'],[4, 'vert']],[[4, 'bleu'], [6, 'blanc'], [6, 'rose'], [3, 'vert'], [5, 'blanc'], [5, 'vert']],[[3, 'bleu'], [4, 'jaune'], [1, 'bleu'], [2, 'rose'], [4, 'blanc'], [1, 'rose']],[[5, 'rouge'], [6, 'vert'], [1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],[[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [3, 'rouge'], [2, 'jaune'], [6, 'bleu']],[[3, 'blanc'], [4, 'vert'], [3, 'jaune'], [2, 'rouge'], [6, 'rouge'], [3, 'rose']]]

dic_Voisin={}
a={}
for ligne in range (0,6):    
        for case in range (0,6):
            for voisin in range (0,6):                 
                for i in range (0,2):
                    if carte[ligne][case][i] == carte[ligne][voisin][i] and voisin!=case:
                        if dic_Voisin.get((ligne,case)):
                            dic_Voisin[(ligne,case)].append(carte[ligne][voisin])
                        else:
                            dic_Voisin[(ligne,case)]=[carte[ligne][voisin]]
                    if carte[voisin][ligne][i] ==  carte[case][ligne][i] and voisin!= case:
                        
                        if dic_Voisin.get((ligne,case)):
                            dic_Voisin[(ligne,case)].append(carte[voisin][ligne])
                        else:
                            dic_Voisin[(ligne,case)]=[carte[voisin][ligne]]

#

but here i get 1 dic and every thing is broken somehow

rustic wasp
wise furnace
#

case is used in Python for switch/case statements.

rustic wasp
#

o

#

should i switch it

wise furnace
#

It's not the source of your problem but you should definitely call it something else in your code.

#

I don't see any dict here.

#

carte looks like a list of lists

rustic wasp
#

if i call it Case

rustic wasp
wise furnace
#

oh dic_Voisin

rustic wasp
wise furnace
#

What is it that you're trying to do?

rustic wasp
#

basically making a dic of all the possible movement

#

for every spot

wise furnace
#

If you need 3.10 why did you install 3.12?

rustic wasp
#

?

#

i figured out my mistake i was dumb

#

i inverted lines and columns

#

just what does 'dict' object is not callable

#

mean

wise furnace
#

It means you're trying to call a dict as a function, like with parentheses.

rustic wasp
#

oh ok

#

ty

#

i have solved most of my problems for now

wise furnace
#

Fantastic.

rustic wasp
#

my end result is this : {(1, 1): [[4, 'bleu'], [3, 'bleu'], [2, 'vert']],
(2, 1): [[2, 'bleu'], [3, 'bleu']],
(3, 1): [[2, 'bleu'], [4, 'bleu'], [3, 'blanc'], [1, 'bleu']],
(1, 3): [[4, 'rose'], [6, 'rose']],
(1, 4): [[5, 'rose'], [4, 'vert'], [2, 'rose'], [4, 'rouge']],
(5, 1): [[2, 'bleu'], [2, 'blanc'], [2, 'jaune']],
(6, 1): [[3, 'bleu'], [3, 'jaune'], [3, 'rose']],
(1, 6): [[4, 'rose'], [5, 'vert']],
(1, 2): [[6, 'blanc'], [4, 'jaune'], [6, 'vert']],
(2, 2): [[6, 'jaune'], [6, 'rose'], [6, 'vert'], [5, 'blanc'], [2, 'blanc']],
(3, 2): [[6, 'jaune'], [4, 'vert'], [4, 'blanc']],
(2, 3): [[6, 'blanc'], [5, 'rose']],
(4, 2): [[6, 'jaune'], [6, 'blanc'], [4, 'vert']],
(2, 4): [[5, 'vert'], [3, 'rouge']],
(2, 5): [[6, 'blanc'], [5, 'vert'], [1, 'blanc'], [4, 'blanc'], [5, 'jaune']],
(5, 2): [[6, 'blanc'], [2, 'vert'], [2, 'jaune']],
(6, 2): [[4, 'jaune'], [6, 'vert']],
(2, 6): [[3, 'vert'], [5, 'blanc'], [4, 'vert'], [5, 'bleu']],
(3, 3): [[3, 'bleu'], [1, 'rouge'], [1, 'jaune'], [1, 'rose']],
(4, 3): [[1, 'bleu'], [1, 'jaune'], [5, 'rouge'], [4, 'rouge']],
(3, 4): [[1, 'rose'], [4, 'rose'], [2, 'rouge']],
(3, 5): [[4, 'jaune'], [1, 'blanc'], [5, 'blanc']],
(5, 3): [[1, 'bleu'], [1, 'rouge'], [3, 'jaune'], [2, 'jaune']],
(3, 6): [[1, 'bleu'], [2, 'rose'], [3, 'rose']],
(6, 3): [[1, 'jaune'], [3, 'blanc'], [3, 'rose']],
(4, 1): [[1, 'rouge'], [4, 'rouge'], [5, 'jaune'], [5, 'bleu']],
(4, 4): [[4, 'rose'], [5, 'rouge'], [1, 'rouge'], [3, 'rouge'], [2, 'rouge']],
(4, 5): [[5, 'rouge'], [5, 'bleu'], [5, 'blanc'], [2, 'jaune']],
(5, 4): [[3, 'vert'], [4, 'rouge'], [2, 'rouge']],
(4, 6): [[5, 'rouge'], [5, 'jaune'], [5, 'vert'], [6, 'bleu']],
(6, 4): [[2, 'rose'], [4, 'rouge'], [3, 'rouge'], [6, 'rouge']],
(1, 5): [[5, 'blanc'], [4, 'blanc']],
(5, 5): [[2, 'vert'], [2, 'blanc'], [1, 'jaune'], [5, 'jaune']],
(6, 5): [[2, 'rouge']],
(5, 6): [[5, 'bleu']],
(6, 6): [[3, 'blanc'], [3, 'jaune'], [1, 'rose']]}

#

can i get rid of the inside brackets?

#

(2, 1): [[2, 'bleu'], [3, 'bleu']], -> (2, 1): [2, 'bleu', 3, 'bleu']

tame sail
#

That would be harder to work with than without

rustic wasp
#

true

#

i think ill modifie the second code so that it works better with

rustic wasp
tame sail
#

You cannot use a list as the key, only as a value.

rustic wasp
#
END=(1,1)
arriver=(2,5)
chemin={}
chemin[arriver]=[arriver]
sommet_marque=[]; sommet_ferme=[]
sommet_marque.append(arriver)
while len(sommet_marque)!=0:
    S=sommet_marque.pop(0)
    sommet_ferme.append(S)
    for voisin in dic_Voisin[S]:
      
        if  (voisin not in sommet_ferme) and (voisin not in sommet_marque):
                sommet_marque.append(voisin)
                chemin[voisin]=chemin[S]+[voisin]               
                if voisin==END:
                    print(chemin[voisin])          
``` i ve added this (1,1) is not a list is it?
wise furnace
#

that's a tuple

rustic wasp
#

oh you can't put a tuple?

#

but my tuple is the name of my key though

wise furnace
#

You asked if it was a list and I said it wasn't. You can use a tuple for a key

rustic wasp
#

hmmm

#

where does the list come from

#

cuz the error the consol says is still the same

wise furnace
#

for voisin in dic_Voisin[S]: <- voisin is probably going to be a list right?

tame sail
#

What is the full error? It should show a line where the error is happening

rustic wasp
#

yeeee

wise furnace
#

because dic_Voisin is filled with lists

#

I think you're confusing yourself.

rustic wasp
rustic wasp
#

i am

tame sail
#

voisin seems to be a list

#

or S

#

S is

rustic wasp
#
END=6
G = {0:[1,2,3],1:[0,7,2],2:[0,1,7],3:[0,2,6],6:[3,7],7:[6,2,1]}
sommet=0
chemin={}
chemin[sommet]=[sommet]
sommet_marque=[]; sommet_ferme=[]
sommet_marque.append(sommet)
while len(sommet_marque)!=0:
   S=sommet_marque.pop(0)
   sommet_ferme.append(S)
   for voisin in G[S]:
      
       if  (voisin not in sommet_ferme) and (voisin not in sommet_marque):
               sommet_marque.append(voisin)
               chemin[voisin]=chemin[S]+[voisin]               
               if voisin==END:
                   print(chemin[voisin])

#

program was made for this at first

#

there were no lists

#

wanted to use the same one for the dic i just made

#

but in this new dic its made out of lists

#

!e END=6
G = {0:[1,2,3],1:[0,7,2],2:[0,1,7],3:[0,2,6],6:[3,7],7:[6,2,1]}
sommet=0
chemin={}
chemin[sommet]=[sommet]
sommet_marque=[]; sommet_ferme=[]
sommet_marque.append(sommet)
while len(sommet_marque)!=0:
S=sommet_marque.pop(0)
sommet_ferme.append(S)
for voisin in G[S]:

   if  (voisin not in sommet_ferme) and (voisin not in sommet_marque):
           sommet_marque.append(voisin)
           chemin[voisin]=chemin[S]+[voisin]               
           if voisin==END:
               print(chemin[voisin])
ember knotBOT
#

@rustic wasp :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, 3, 6]
rustic wasp
#

see it was the fastest path from sommet to END

#

now i want to go from any key to another expl: (1,1) to (2,5)

tame sail
#

I think you should store the coordinates of the places you can go to then, not the number/color

rustic wasp
#

hmmm yes

#

good idea

tame sail
#

Anyway good luck with this, I got to get some sleep.

rustic wasp
#

'tuple' object has no attribute 'append'

ember knotBOT
#
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.

#

๐Ÿ”’ help with dictionaries