Question

Hi I have a variable that holds a dictionary that looks like this {'-0.1': 4, '0.9':...

Hi I have a variable that holds a dictionary that looks like this {'-0.1': 4, '0.9': 2, '1.9': 1, '2.9': 1, '3.9': 1, '4.9': 1}

The keys in this dictionary are generated by a value set using user input. In this instance, they vary by 1.

I would like to use ONLY this dictionary, and measure the value of the key (i.e. 0.9 - (-0.1) = 1). Trying different code, but non seem to work. Could you please help? I ve been trying to extract the first and second keys and then measure the difference, but cant seem to work out the code.

Homework Answers

Answer #1
CODE :

# defining the dictionary
c={'-0.1': 4, '0.9': 2, '1.9': 1, '2.9': 1, '3.9': 1, '4.9': 1}

# extracting the keys from the dictionary
# logic : Every dictionary has item pair(key:value)
# and for accessing the pair, we will iterate over the items which
# would return the key-value pair and from that, we can take keys and make a list
# as the keys are in the string, we may need to map them into the float.
# map function returns non-iterable object hence we will be casting them into list

keys = list(map(float,[a for a, b in c.items()]))

# difference between two keys values
diff=keys[1]-keys[0]

#printing the diff variable
print(diff)

CODE SCREENSHOT AND OUTPUT :

Please provide feedback and comment down if any problem persists.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT