#๐Ÿ”’ Need help with understanding how dictionaries work!

64 messages ยท Page 1 of 1 (latest)

acoustic orchid
#

Hello, i dont really understand how the code converts the emojis.. i got this script from a tutorial.

raven joltBOT
#

@acoustic orchid

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.

leaden grotto
#

do you know how lists work?

acoustic orchid
#

yeah

leaden grotto
#

!e

l = ['cat', 'dog']
print(l[0])
raven joltBOT
acoustic orchid
#

yeah i get it

leaden grotto
#

dicts are the same; they just don't use integers as the indexer.

acoustic orchid
#

yeah i understand dictionaries i just dont get it how this code works and how it uses then

#

them*

leaden grotto
#

!e

d = {"animal": "cat", "sound": "meow"}
print(d["sound"])
raven joltBOT
hollow patrol
#

dicts also have a special method called .get()

leaden grotto
#

ah get is kinda like [] but it gives you the chance to substitute something if it's not found.

acoustic orchid
#

can you tell me exacly whats going on here? mostly in the second last line

leaden grotto
#

watch

#

!e

d = {"animal": "cat", "sound": "meow"}
print(d["weight"])
raven joltBOT
leaden grotto
#

so that's what happens if it's not found.

wooden ocean
#

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

leaden grotto
#

!e

d = {"animal": "cat", "sound": "meow"}
print(d.get("weight", "I dunno how much it weighs!!"))
raven joltBOT
acoustic orchid
#

what means the (word, word)?

leaden grotto
#

and that's how get works

#

so get(word, word) means "look up word; if it's not there, just return word itself"

acoustic orchid
#

and what does the += mean in this case? it adds the text to the empty string?

leaden grotto
#

ya

#

!e

s = "wat"
print(s)
s += "; I said WAT"
print(s)
raven joltBOT
leaden grotto
#

try it and see

acoustic orchid
#

what does the word represent?

leaden grotto
#

some text from somewhere

acoustic orchid
#

from what ive typed?

leaden grotto
#

one of the words in the message

#

it says words = Messsage.split(' '), right?

#

That's what "words" is

acoustic orchid
#

oh ok yea

#

and uh

#

so every word

leaden grotto
#

!e

Message = "It's like a jungle sometimes"
print(Message)
print(Message.split(' '))
raven joltBOT
acoustic orchid
#

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?

leaden grotto
#

yep

acoustic orchid
#

hmm

leaden grotto
#

we call it a "dict", not a "dictionary"

acoustic orchid
#

alright

#

okay so

#

the second word in get(word, word) makes it so if no match found, the word returns?

sudden arch
leaden grotto
#

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.

acoustic orchid
#

and the first word?

leaden grotto
#

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.

acoustic orchid
#

i guess?

sudden arch
#

Aha.

sudden arch
raven joltBOT
#
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.