简体   繁体   中英

Why does Jupyter notebook have problems with my code that Pycharm does not?

I'm using PyCharm to write my code in and then with Jupyter I have to pass some tests required.

However, the same piece of code that provides a perfect working solution in Pycharm produces a keyerror in Juptyer. Is there any reason why? Numpy seems to be working fine...the problem is with my dictionary. The following is the error I am getting:

    ipython-input-34-35752ede55c7> in myfunction(input)
         56     mydict = {}
         57     for s in d:
    ---> 58         vals = set(sum(units[s],[]))-{s}
         59         mydict[s] = vals
         60 

    KeyError: 'A1'

Edit: in PyCharm you can clearly see A1 definitely exists, yet in jupyter apparently A1 does not, the code is identical in both: (please see image of pycharm debugger)

在此处输入图像描述

Hard to say without more data/code, but it seems that 'A1' doesn't exist in whatever you're running in Jupyter. Are you absolutely sure that both the data and the code is the same?

Key error usually occurs when python is not able to find the key, from your code it is hard to judge without dictionary what the problem may be about how you have mapped the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM