简体   繁体   中英

Debug list comprehension inside for loop in Pycharm

I'm trying to debug a code snippet includes list comprehension inside loop.

list_a = ['a', 'b', 'c']
list_b = ['a', 'b']
for x in list_a:
    print([i for i in list_b if i == x])

The code run okay when I run the entire script. However when I located a breakpoint before the for loop and try to run the for loop interactively I got the following error:

Traceback (most recent call last): File "/root/pycharm/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec exec(exp, global_vars, local_vars) File "", line 2, in File "", line 2, in NameError: name 'x' is not defined

I'm using pycharm for debugging.

best way to debug a list comprehension is to use a simple for loop :

from:

[i for i in list_b if i == x]

use:

for x in list_a:
    costum_list = []
    for i in list_b:
        if i == x:
            costum_list.append(i)
    print(costum_list)

but I just run your code and seems to be all fine

I got the same error few days back and what I did was just click Remove All Watches from debug console thanks to this PyCharm referencing older, removed variable question. Right click on your Variables section and click Remove All Watches and re-run your debug process.

在此处输入图片说明

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