我有一个类似于这样的python列表,例如:
[Product(parent=tube,child=spokes), Product(parent=bicycle, child=wheel), Product(parent=wheel,child=tube)]
其中Product
是一个python类,有两个成员parent
和child
。你知道吗
Products
可以以任何顺序出现在列表中。你知道吗
实现以下目标最有效的方法是什么:
Given an input spokes for example , return the `root of the tree` , bicycle in this case.
到目前为止,我尝试过的是:当产品每次出现的顺序不同时,循环不能给出正确的结果。你知道吗
您不需要写下您可以应用于数据的假设有多强(如果它始终是正确的树)。所以我的代码检查了一些条件,使其不在无限循环中。你知道吗
和更短的版本
以下是针对您的问题的伪代码:
可以使用lambda定义在一行中实现它,如下所示:
相关问题 更多 >
编程相关推荐