-
Find the most frequent appearing value in a dictionary, if there are ties, just return any one among them
eg.
input: {"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}
output: 4.0 -
Given a list of numbers, return a list with the order of numbers reversed (challenging)
hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
then try to build a new list out of it
input: [1,2,3,4,5]
out: [5,4,3,2,1]
#๐ Can anyone help me with homework?
211 messages ยท Page 1 of 1 (latest)
@wraith void
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.
list = [1, 2, 3]
how can i write a list using input?
input("Write a list of numbers")
how do i get python to detect tht list of numbers?
anyone here?
Uhh
What do they mean by "given a list of numbers"? That can mean different things.
mind getting the 2nd one done first?
i know what .dict is, HOW DO I WRITE A LIST WITH .DICT
LIKE
If you mean a comma separated string representing a list, look j into the split method on strings.
list.dict?
list.dict = [1, 2, 3, 4, 5, 5] i have tried this
gives me an error
TypeError: cannot set 'dict' attribute of immutable type 'list'
wtf is tht supposed to mean
I don't think you're supposed to use list.dict
That line of code attempts to create a dict attribute on the list class, because you did an assignment.
What are you trying to do there?
.dict doesnt allow duplicats correct?
im tryna like get .dict to give me the duplicate
It doesn't allow duplicate keys, no
A dictionary can't have two of the same key:
!e
d = { 1: 'a', 1: 'b', 2: 'c', 3: 'd' }
print(d)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{1: 'b', 2: 'c', 3: 'd'}
Notice there's only a single 1 key.
thought it was number?
You thought what was a number?
I thought .dict removed duplicate numbers
like 1, 1, 2, 2, 3, 3, it would be 1, 2, 3, after
It did. Look at the printout #1357883763299848312 message
My code shows that d was created with two 1 keys, but only one remained after the dictionary was created.
how would i do 2. then
wait where was the dictionary-
The{ } and the part inside that
!e
d = { 1: 'a' }
print(d, type(d))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{1: 'a'} <class 'dict'>
How have you been creating dictionaries?
uh
here is the thing
i never did-
d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)
thissss
doesnt work
i tried replacing everything
AUGHHH
That code looks like it will execute fine
!e
d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{'john': '3.5', 'jane': '4.0', 'bob': '2.8', 'mary': '4.0'}
It created a dictionary and stored it in d.
You'd need to determine which value is occurring the most frequently.
tht would be 4.0
Which means you want to check every value in the dictionary.
and how do i do tht?
In this one case, ya
(im a total beginer)
You can use the values method on dictionaries to iterate over them:
!e
d = { 1: 'a', 2: 'b', 3: 'c' }
for value in d.values():
print(value)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | a
002 | b
003 | c
Reverted to Javascript for a second there lmao
whats a values
have not touched tht yet
The part after the : in the dictionary
brb
You're likely going to need to take a step back and study dictionaries. You won't be able to use them for things like this without having a good undertanding of them.
Dictionary are lists where you can access a item in the list with a key instead of a number.
for example
{"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}
Well, to be clear, they're not lists. They're comparable to lists in the sense that they can contain other things, and allow looking up specific elements by some key/index.
^
in the first item john is the key and 3.5 is the value associated with the key
yeah im trying to help you learn it
You can't use a dictionary without using "values" of the dictionary.
If they are requiring you to use a dictionary, they're expecting you to understand values.
oop
tell tht to the github website my teach is using
if u click on the .dict py one
there is no values
!e
d = {"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}
print(d["john"])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
3.5
ultimatepython/data_structures/dict.py line 14
# Let's create a dictionary with student keys and GPA values```
well the only problem with
d = { "john": '3.5', "jane": '4.0', "bob": '2.8', "mary": '4.0' }
print(d)
for value in d.values():
print(value)
Hey @wraith void!
Please edit your message to use a code block
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can access values in a dictionary using the key to the value, john is the key, d is the dictionary, d["john"] is the value of john.
d = {"john": 3.5, "jane": 4.0, "bob": 2.8, "mary": 4.0}
print(d["john"])
wdym
key?
gives the wrong number
wait now it doesnt
i changed it to mary
alr now
can anyone help w/ the 3.
Given a list of numbers, return a list with the order of numbers reversed (challenging)
hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
then try to build a new list out of it
input: [1,2,3,4,5]
out: [5,4,3,2,1]
!e
inputlist = [1,2,3,4,5]
newlist = [
for i in list[::-1]
newlist.append(i)
!e
inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]
newlist.append(i)
bruh
!e
inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]:
newlist.append(i)
help
trying
!e
inputlist = [1,2,3,4,5]
newlist = []
for i in list[::-1]:
newlist.append(i)
print(newlist)
i aint looking for answers, im looking for explanations
They asked to use range though
Ok so one way to do it would be this:
inputlist = [1,2,3,4,5]
newlist = []
for i in reversed(inputlist):
newlist.append(i)
print(newlist)
It iterates over the list reversed(inputlist) with a for loop. reversed() is a function that outputs a reversed version of a list, inside of the for loop it appends the current number to an empty list.
oh true
Do you understand what range does?
range is basically a shortcut for making a sequence of numbers
range(5) is roughly the same as [0, 1, 2, 3, 4]
ohh
alr now
Given a list of numbers, return a list with the order of numbers reversed (challenging)
hint: try to use the range function in a forloop, by counting the numbers in the list in reverse
then try to build a new list out of it
input: [1,2,3,4,5]
out: [5,4,3,2,1]
help/
do you understand what indexing is?
nope
Ok, I'm going to use an example but with letters instead
because I think the numbers actually make this more confusing
letters = ['a', 'b', 'c', 'd', 'e']
this is a list of letters
"indexing" lets us access something at a specific position in the list
so if I just wanted "c"
I would do letters[2]
mhm
now there's some useful relationships to keep in mind
the last position in the list will always be "the length of the list minus one"
and it turned blue
tf does it do
it reverses things
its giving me exit code 0
WAHAT
IHQEIOQWE
WAIT
WAIT
WHAT NOW
I NEED THIS THING
there's definitely easier ways to reverse things than using range
but they asked you to use range
learning programming isn't about just finding the easiest way to do things
it's about learning how to think logically
oof
me being excited for liek 1 second
does this make sense?
yep
ok
so something nice about range, is if we give it a range based on the length of a list, we get the correct index count
!e
letters = ['a', 'b', 'c', 'd', 'e']
for i in range(len(letters)):
print(letters[i])
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | a
002 | b
003 | c
004 | d
005 | e
Does this make sense to you?
yo wait
i got it workin
it js uh
has a lot of commas in it
result = reversed("1, 2, 3")
print(list(result))
This help channel has been closed. 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.