如何使这个片段成为Pythonic?

2024-09-26 22:50:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道Python程序员将如何编写以下代码段:

for i in range(10):
    indexVector[i] = empAvg[i] + upperBound(t, pullCount[i])

这里t是一个常数。正如你所看到的,我习惯于C/C++风格的代码,但希望正确使用Python。你知道吗


Tags: 代码infor风格代码段常数range程序员
2条回答

如果要使用列表理解来创建indexVector(假设它在代码段中输入的10个索引之外没有任何其他值),可以使用-

indexVector = [empAvg[i] + upperBound(t, pullCount[i]) for i in range(10)]

您可以zip列表empAvgpullCount以元素方式遍历它们,而不需要i计数器,然后使用列表理解来创建indexVector

indexVector = [emp + upperBound(t, pull) for emp, pull in zip(empAvg, pullCount)]

相关问题 更多 >

    热门问题