0

In the process of writing an app, I've come across a bizarre python code piece that I can't understand:

The Code:

msg = [108878, [[314.06, 2, 4.744018], [314.03, 1, 15.9059], [314.02, 2, 79.8338531], [314, 1, 54.90047253], [313.56, 2, 1.75392219], [313.53, 2, 15.61132219], [313.5, 1, 0.554316], [313.42, 1, 1.5976], [313.27, 1, 0.43344], [313.26, 1, 62.724], [313.25, 1, 2.57518855], [313.24, 1, 0.04], [313.09, 2, 22.51784808], [312.9, 1, 40], [312.82, 1, 26.65592034], [312.7, 1, 35.53791008], [312.62, 1, 0.46912], [312.61, 1, 100], [312.6, 1, 48.33502123], [312.57, 1, 4.24547326], [312.56, 2, 0.2], [312.5, 2, 109.76863639], [312.43, 1, 100], [312.42, 1, 0.11142352], [312.4, 1, 7.815571], [314.09, 2, -3.01187461], [314.14, 1, -1.27056771], [314.39, 2, -9.31898324], [314.46, 1, -0.01930229], [314.49, 1, -0.40344], [314.5, 1, -3.40637161], [314.53, 1, -0.2], [314.54, 2, -0.46432889], [314.57, 1, -0.04200538], [314.71, 1, -0.050949], [314.84, 1, -0.02153813], [314.88, 1, -0.04200538], [314.93, 1, -68.439], [314.94, 2, -7.477782], [314.95, 1, -5], [315.1, 1, -5.97], [315.12, 1, -0.01], [315.16, 1, -40], [315.2, 1, -0.04200538], [315.22, 1, -25.7525], [315.23, 1, -78.54523718], [315.38, 1, -80], [315.42, 1, -6.65060488], [315.47, 1, -20], [315.48, 1, -5.36]]]

bids = asks = {}
lenbids = lenasks = 0
for order in msg[1]:
    if float(order[2])>0:
        lenbids +=1
        bids[order[0]]=order[2]
    elif float(order[2])<0:
        lenasks+=1
        asks[order[0]]=-order[2]

print(len(bids),len(asks),lenbids,lenasks)

Output:

50 50 25 25

It seems to me as though python is behaving properly with regards to the lenbids/lenasks actions but is executing the second part of the if statement regardless of whether the condition is met or not?

I'm running Pycharm with Anaconda3 if that makes any difference.

Any help greatly appreciated!

1
  • 1
    If statements work fine... The dictionaries are the exact same object. Commented Oct 18, 2017 at 0:41

1 Answer 1

2

You have set bids and asks to be the same dictionary:

bids = asks = {}

You want them to be different dictionaries

bids, asks = {}, {}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks donkopotamus! I often do things like a=b=0 to initialise variables and never had any issues with that....didn't know you couldn't do that with objects
Everything in Python is an object, including integers. But integers are not mutable objects (modifiable in-place), while dictionaries are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.