首页 > 解决方案 > Why list comprehension with many interactions is much slower than multiple list comprehensions?

问题描述

When I try to run a list comprehension with a large number of interactions, the command takes too long and does not output an answer. However, if I break it in small lists, I can get the job done.

Example: This first one takes "infinitely":

cl = [bool(sum([x in c_groups.index[k] for x in bb_sorted.columns[5:11]])) for k in range(len(c_groups))]

While this works just fine in a reasonable time:

cl1 = [bool(sum([x in c_groups.index[k] for x in bb_sorted.columns[5:11]])) for k in range(1000)]
cl2 = [bool(sum([x in c_groups.index[k] for x in bb_sorted.columns[5:11]])) for k in range(1000,2000)]
cl3 = [bool(sum([x in c_groups.index[k] for x in bb_sorted.columns[5:11]])) for k in range(2000,3000)]
cl4 = [bool(sum([x in c_groups.index[k] for x in bb_sorted.columns[5:11]])) for k in range(3000,len(c_groups))]
cl = cl1 + cl2 + cl3 + cl4

I copied this example from something exactly as I wrote it, but I it happened also in other situations. I have no idea of what happens here, and it does not feel right to avoid problems breaking list comprehensions down like this.

Why does this happen, and how to avoid it (without breaking sentences like that)?

EDIT:

I tried the same script again, and I found out that the error happens only when using Spyder (running in shell works fine). Thus, it must be an issue with the IDE, I suppose (makes sense?).

标签: pythonpython-3.xlistspyder

解决方案


推荐阅读