python - 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?).
解决方案
推荐阅读
- windows - 复制到网络共享不适用于 powershell Copy-Item
- azure - 如何使用 Azure CLI 从 azure Active Directory 获取最近创建的 AD 用户列表
- javascript - jQuery - 复选框不更新文本
- java - 使用 BorderLayout 正确使用静态
- c# - 程序集依赖问题
- python - 我的代码有问题。它输出“无”
- c - 如何修复此代码,以便我可以计算 2 个分数并将总和作为一个浮点数返回?
- javascript - 如何使用 AR.js 改进移动浏览器上的标记检测
- string - squeak Smalltalk中的拆分功能
- c# - 使用多态 c# 的货币转换器