我被这个问题难住了,不管我如何解决它,它仍然给我同样的结果。你知道吗
基本上,假设我有两个组-GrpA\u null和GrpB\u null,每个组有两个网格,名称完全相同,brick\u geo和bars\u geo -结果:GrpA\u null-->;brick\u geo,bars\u geo
但出于某种原因,在下面的代码中,我认为是给我带来问题的代码,当它运行时,程序声明GrpA\u null与GrpB\u null有相同的副本,可能它们引用了brick\u geo和bars\u geo。代码一运行,我的孩子们就会有一个数值, -结果:GrpA\u null-->;brick\u geo0,bars\u geo0,GrpB\u null1-->;brick\u geo,bars\u geo1
因此,我试图修改代码,只要父级(GrpA\u null和GrpB\u null)不同,它就不会“接触”子级。你知道吗
有人能给我一些建议吗?你知道吗
def extractDuplicateBoxList(self, inputs):
result = {}
for i in range(0, len(inputs)):
print '<<< i is : %s' %i
for n in range(0, len(inputs)):
print '<<< n is %s' %n
if i != n:
name = inputs[i].getShortName()
# Result: brick_geo
Lname = inputs[i].getLongName()
# Result: |GrpA_null|concrete_geo
if name == inputs[n].getShortName():
# If list already created as result.
if result.has_key(name):
# Make sure its not already in the list and add it.
alreadyAdded = False
for box in result[name]:
if box == inputs[i]:
alreadyAdded = True
if alreadyAdded == False:
result[name].append(inputs[i])
# Otherwise create a new list and add it.
else:
result[name] = []
result[name].append(inputs[i])
return result
如果我理解的话,这可能就是你要找的,它似乎是比较不同节点的子层次结构,看看它们是否是相同的名称。你知道吗
因为返回值是集合,所以您可以测试它们是否相等,看看其中一个是另一个的子集还是超集,看看它们有什么共同点,依此类推:
迭代一组节点很容易:
我正准备写与bitsplit相同的评论,他已经写了。你知道吗
所以我现在给你一个代码,我认为它和你的完全一样,基于这些注释和
get
字典的方法:是的。你知道吗
其次,正如bitsplit所说,我觉得你的问题无法理解 你能提供更多关于投入要素的信息吗?
你对GrpA\u null和GrpB\u null以及名称和网格的解释不清楚。你知道吗
是的。你知道吗
编辑:
如果我的归约/简化是正确的,检查一下,我发现您实际上是比较
A
和B
元素的inputs
(使用A!=B
),如果A
和B
具有相同的简称shortname
,那么您将A
记录在字典result
的键shortname
(仅一次);我认为这个代码仍然可以简化为:
有几件事你可能需要注意。首先,缩进在Python中很重要。我不知道代码的缩进是否如预期的那样,但是函数代码应该比函数定义缩进得更远。你知道吗
第二,我觉得你的问题有点难以理解。但是有几件事可以改进你的代码。你知道吗
在collections模块中,有(或应该有)一个名为defaultdict的类型。此类型类似于dict,只是它具有指定类型的默认值。因此,当您得到一个键时,defaultdict(int)的默认值为0,即使该键以前不存在。这允许计数器的实现,例如在不排序的情况下查找重复项。你知道吗
这让我想到另一点。Python for循环为每个结构实现一个for。你几乎不需要列举你的项目,然后才能访问它们。所以
你想用
如果你真的需要列举你的输入
最后,您可以使用列表理解、dict理解或生成器表达式迭代和过滤iterable对象。它们非常强大。见Create a dictionary with list comprehension in Python
试试这个代码,玩一下。看看对你有用吗。你知道吗
相关问题 更多 >
编程相关推荐