Keeping Track of the Produce: Why does the output order runs from orange?


#1

Excuse me.
I went through this test, but wonder to know " Why does the order of output run from orange?" as follows,

orange
price: 1.5
stock: 1.5
pear
price: 3
stock: 3
banana
price: 4
stock: 4
apple
price: 2
stock: 2

#2

dictionaries are unorderd, if you truly wish to understand this, you need to learn to implement hastables in the C programming language


#3

There is a library for working with ordered dictionaries but I have not yet explored that. We can order the output without a library by iterating over a sorted list of keys…

>>> for key in sorted(stock):
    print (key)
    print ("stock: %s" % stock[key])
    print ("price: %s" % prices[key])

    
apple
stock: 20
price: 3.0
orange
stock: 50
price: 1.5
pear
stock: 15
price: 2.5
>>> 

#4

You can’t have a data structure that supports everything. It’s like wanting a tractor to be a can of soda.

If you have a tractor, don’t be surprised if it doesn’t taste sweet. You shouldn’t expect it to taste sweet. You shouldn’t expect to be able to taste it, it may not make sense to do so.

You might want to learn how a tractor operates. (But that won’t answer why it doesn’t taste sweet, that question is broken, or at least the answer will be something along the lines of that you can’t taste it, or maybe that it would be a bad idea to try)

Tractors are great at pulling things, so that is what you should rely on, that is what it promises and provides.

And if you want something sweet, get a soda. Or perhaps there’s some tractor out there with a soda dispenser in it. There are also other things that are sweet, those might also be suitable.


As mentioned by others, you could look up what a hash table is, because a dict is likely some form of that. You could look directly at dict’s implementation too, but that is hardly a textbook example.

But it’s entirely reasonable to not dive into that, it isn’t required to reason about dictionaries, all you need for that is to know what it promises.

You might also look into data structures in general (arrays, linked lists, growable arrays, various tree structures) to get a better sense of what tradeoffs they make. It’s rather similar to tractor vs soda though, they simply do something else, are something else.

In python 3.7 dicts keep insertion order. cpython 3.6 already does so, but 3.7 adds it to the language specification (it becomes something that dict promises and provides)


#5

Good to know. Time to update my version (3.5.1).


#6

Thank you very much. I’ll try to learn the hashtables.


#7

Thank you for the sorted method. I exercised it again and keep that in mind.


#8

As advised, Python support having changed in version 3 may be the reason my above code works. If it doesn’t work in Python 2, then change stock to stock.keys() in,

for key in sorted(stock):

#9

Thank you for the patient reply.

As a found regular output of " always start from orange, then pear, banana, apple", that seems to be some principle order to sart from the 3rd one, then the 4th one, the 1st one, the 2nd one. So I confused why not to be the other random order?

I really need to look into the advanced data structures and help me to improve my concept. Really appreciate tremendously.


#10

I tried “stock” and it works. :smile:

for key in sorted(stock):
print (key)
print (“stock: %s” % stock[key])
print (“price: %s” % prices[key])

output as:
apple
stock: 0
price: 2
banana
stock: 6
price: 4
orange
stock: 32
price: 1.5
pear
stock: 15
price: 3


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.