python 3.3.2+ python支持创建生成器函数的新语法
yield from <expression>
Run Code Online (Sandbox Code Playgroud)
我已经快速尝试了这个
>>> def g():
... yield from [1,2,3,4]
...
>>> for i in g():
... print(i)
...
1
2
3
4
>>>
Run Code Online (Sandbox Code Playgroud)
它似乎很简单,但PEP文件很复杂.我的问题是,与之前的收益率声明相比,还有其他区别吗?谢谢.
身份列表包含大约57000 张图像的大数组。现在,我正在借助itertools.product(). 这将整个列表存储在内存中,这非常昂贵,并且我的系统在 4 分钟后挂起。
如何优化以下代码并避免节省内存?`
for i in range(0, len(idendities) - 1):
for j in range(i + 1, len(idendities)):
cross_product = itertools.product(samples_list[i], samples_list[j])
cross_product = list(cross_product)
for cross_sample in cross_product:
negative = []
negative.append(cross_sample[0])
negative.append(cross_sample[1])
negatives.append(negative)
print(len(negatives))
negatives = pd.DataFrame(negatives, columns=["file_x", "file_y"])
negatives["decision"] = "No"
negatives = negatives.sample(positives.shape[0])
Run Code Online (Sandbox Code Playgroud)
内存9.30会越来越高,有一点系统已经完全挂了。
我还根据他的回答实现了以下答案并修改了代码。
for i in range(0, len(idendities) - 1):
for j in range(i + 1, len(idendities)):
for cross_sample in itertools.product(samples_list[i], samples_list[j]):
negative = [cross_sample[0], cross_sample[1]]
negatives.append(negative)
print(len(negatives)) …Run Code Online (Sandbox Code Playgroud)