#๐ Need help with understanding how dictionaries work!
64 messages ยท Page 1 of 1 (latest)
@acoustic orchid
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.
do you know how lists work?
yeah
!e
l = ['cat', 'dog']
print(l[0])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
cat
yeah i get it
dicts are the same; they just don't use integers as the indexer.
yeah i understand dictionaries i just dont get it how this code works and how it uses then
them*
!e
d = {"animal": "cat", "sound": "meow"}
print(d["sound"])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
meow
dicts also have a special method called .get()
ah get is kinda like [] but it gives you the chance to substitute something if it's not found.
can you tell me exacly whats going on here? mostly in the second last line
: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 | print(d["weight"])
004 | ~^^^^^^^^^^
005 | KeyError: 'weight'
so that's what happens if it's not found.
the name is quite apt here, you can think of the emoticon (like :) or :D as the word, and the actual emoji ๐ and ๐ as the definition. python is essentially searching a dictionary to find the definition of a word
!e
d = {"animal": "cat", "sound": "meow"}
print(d.get("weight", "I dunno how much it weighs!!"))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
I dunno how much it weighs!!
what means the (word, word)?
and that's how get works
so get(word, word) means "look up word; if it's not there, just return word itself"
and what does the += mean in this case? it adds the text to the empty string?
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | wat
002 | wat; I said WAT
i dont understand it
try it and see
what does the word represent?
some text from somewhere
from what ive typed?
one of the words in the message
it says words = Messsage.split(' '), right?
That's what "words" is
!e
Message = "It's like a jungle sometimes"
print(Message)
print(Message.split(' '))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | It's like a jungle sometimes
002 | ["It's", 'like', 'a', 'jungle', 'sometimes']
it goes thru the dictionary and compares the word with all the strings in the dictionary
and if it finds a match it converts it, and if it doesnt it returns the word?
yep
hmm
we call it a "dict", not a "dictionary"
alright
okay so
the second word in get(word, word) makes it so if no match found, the word returns?
Some of us are happy to call it a dictionary, because that's what it is. The type of a dictionary is dict.
hmph
because OP said "dictionary" I started scanning the code for a reference to /usr/share/dict/words, and was about to tell them they're mistaken, the code isn't looking in the dictionary, &c &c
I don't care what it "is"; I care what other people think of when I use the word.
^
Yes.
and the first word?
that's the word from the message that we're trying to convert into an emoji.
You should really play with the code; it'll be easier than asking us.
i guess?
Aha.
The dict.get method is specified here, FYI: https://docs.python.org/3/library/stdtypes.html#dict.get
The description is short and to the point.
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.