我在c ++中有一个for,我想在python中编写它,但我不知道如何处理for中的条件。任何人都可以帮助我吗?
for (int b = (i - a) / 2; a + b <= i; b++);
答案 0 :(得分:0)
直接回答您的问题:
for b in range((i-a)/2,a+b-1):
print (b)
如果由于某种原因,您需要终止值:
terminatingValue = 0
for b in range((i-a)/2,a+b-1):
terminatingValue = b
print (b)
答案 1 :(得分:0)
使用while
a=something
i=something
b=(i-a)/2
while(a+b<=i):
###do whatever you want in the loop###
b+=1
答案 2 :(得分:0)
您可以使用for循环和 /**
* Builds treeChilds from the treeRoots
* @param navigationTree The whole navigation Tree containing the treeRoots and their treeChilds
* @param navigableRoutes The remaining navigableRoutes that are to be linked to their respectives treeRoots
* @returns {RouteDisplay[]} The remaining navigableRoutes after some childs have been taken care of
*
* @todo : Might need to update the way the childs are filtered : // .filter(routes => !routes.loadChildren && routes.path.includes(child.path))
* @see Doesn't manage duplicate sub route paths (yet) : see @todo
*/
buildTreeChilds(navigationTree: RouteTreeDisplay[], navigableRoutes: RouteDisplay[]): RouteDisplay[] {
// Build treeChilds
navigationTree.forEach((treeRoot: RouteTreeDisplay) => {
let treeChilds: RouteTreeDisplay[] = [];
if (!this.isFirstLevelDone) {
treeChilds =
navigableRoutes
.filter(routes => !routes.loadChildren && routes.parent === treeRoot.path)
.map(element => {
// Reduce the properties to display
return {
englishName: element.englishName,
frenchName: element.frenchName,
materialIcon: element.materialIcon,
path: element.path
} as RouteTreeDisplay;
});
if (treeChilds.length > 0) {
treeRoot.children = treeRoot.children.concat(treeChilds);
// Clear treeChilds from the navigableRoutes
navigableRoutes = navigableRoutes.filter(baseRoute => treeChilds.find(treeChild => treeChild.path === baseRoute.path) ? false : true);
treeChilds = [];
}
} else {
// Improve this later on when needed
treeRoot.children.forEach(child => {
treeChilds = treeChilds.concat(
navigableRoutes
.filter(routes => !routes.loadChildren && routes.path.includes(child.path))
.map(element => {
// Reduce the properties to display
return {
englishName: element.englishName,
frenchName: element.frenchName,
materialIcon: element.materialIcon,
path: element.path
} as RouteTreeDisplay;
})
);
if (treeChilds.length > 0) {
treeRoot.children = treeRoot.children.concat(treeChilds);
// Clear treeChilds from the navigableRoutes
navigableRoutes = navigableRoutes.filter(baseRoute => treeChilds.find(treeChild => treeChild.path === baseRoute.path) ? false : true);
treeChilds = [];
}
});
}
});
this.isFirstLevelDone = true;
return navigableRoutes;
}
函数。例如:
range()
但是,由于您的代码,我建议使用while循环:
for i in range(15):
#code to execute 15 times
答案 3 :(得分:0)
你的困惑源于 C 的迭代结构的过于笼统。 Python坚持认为所有循环参数都隐式引用循环索引。对于终止条件,用 b 和没有平等来解决你的不平等: b&lt;我 - a + 1
现在我们得到 for 循环
for b in range((i-a)/2, (i-a)+1):
一种解释是你的范围从0到i-a,你想要处理它的上半部分。
答案 4 :(得分:0)
终止条件a + b <= i
相当于b <= i - a
,后者(因为Python for
使用少于或不等于)b < i - a + 1
。
这意味着在Python中你可以写:
for b in range((i - a) / 2, i - a + 1):
...
但是从C / C ++转换到Python时,你经常不想像写一样。您可能最好完全重构代码。