在提取重复项时遇到问题

2024-10-01 19:31:03 发布

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

我被这个问题难住了,不管我如何解决它,它仍然给我同样的结果。你知道吗

基本上,假设我有两个组-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

Tags: 代码nameingtforifresultnull
3条回答

如果我理解的话,这可能就是你要找的,它似乎是比较不同节点的子层次结构,看看它们是否是相同的名称。你知道吗

import maya.cmds as cmds

def child_nodes(node):
    ''' returns a set with the relative paths of all <node>'s children'''
    root = cmds.ls(node, l=True)[0]
    children = cmds.listRelatives(node, ad=True, f=True)
    return set( [k[len(root):] for k in children])

child_nodes('group1')
# Result: set([u'|pCube1|pCubeShape1', u'|pSphere1', u'|pSphere1|pSphereShape1', u'|pCube1']) # 

# note the returns are NOT valid maya paths, since i've removed the root <node>, 
# you'd need to add it back in to actually access a real shape here:

all_kids = child_nodes('group1')
real_children  = ['group1' + n for n in all_kids ] 

因为返回值是集合,所以您可以测试它们是否相等,看看其中一个是另一个的子集还是超集,看看它们有什么共同点,依此类推:

# compare children
child_nodes('group1') == child_nodes('group2')

#one is subset:
child_nodes('group1').issuperset(child_nodes('group2'))

迭代一组节点很容易:

# collect all the child sets of a bunch of nodes:
kids =   dict ( (k, child_nodes(k)) for k in ls(*nodes)) 

我正准备写与bitsplit相同的评论,他已经写了。你知道吗

所以我现在给你一个代码,我认为它和你的完全一样,基于这些注释和get字典的方法:

from collections import defaultdict

def extract_Duplicate_BoxList(self, inputs):
    result = defaultdict()

    for i,A in enumerate(inputs):
        print '<<< i is : %s' %i
        name  = A.getShortName() # Result: brick_geo
        Lname = A.getLongName()  # Result: |GrpA_null|concrete_geo

        for n in (j for j,B in enumerate(inputs)
                  if j!=i and B.getShortName()==name):
            print '<<< n is %s' %n
            if A not in result.get(name,[])):
                result[name].append(A)

    return result

是的。你知道吗

其次,正如bitsplit所说,我觉得你的问题无法理解 你能提供更多关于投入要素的信息吗?
你对GrpA\u null和GrpB\u null以及名称和网格的解释不清楚。你知道吗

是的。你知道吗

编辑:
如果我的归约/简化是正确的,检查一下,我发现您实际上是比较AB元素的inputs(使用A!=B),如果AB具有相同的简称shortname,那么您将A记录在字典result的键shortname(仅一次);
我认为这个代码仍然可以简化为:

def extract_Duplicate_BoxList(inputs):
    result = defaultdict()

    for i,A in enumerate(inputs):
        print '<<< i is : %s' %i
        result[B.getShortName()].append(A)

    return result

有几件事你可能需要注意。首先,缩进在Python中很重要。我不知道代码的缩进是否如预期的那样,但是函数代码应该比函数定义缩进得更远。你知道吗

第二,我觉得你的问题有点难以理解。但是有几件事可以改进你的代码。你知道吗

在collections模块中,有(或应该有)一个名为defaultdict的类型。此类型类似于dict,只是它具有指定类型的默认值。因此,当您得到一个键时,defaultdict(int)的默认值为0,即使该键以前不存在。这允许计数器的实现,例如在不排序的情况下查找重复项。你知道吗

from collections import defaultdict

counter = defaultdict(int)

for item in items:
    counter[item] += 1

这让我想到另一点。Python for循环为每个结构实现一个for。你几乎不需要列举你的项目,然后才能访问它们。所以

for i in range(0,len(inputs)):

你想用

for input in inputs:

如果你真的需要列举你的输入

for i,input in enumerate(inputs):

最后,您可以使用列表理解、dict理解或生成器表达式迭代和过滤iterable对象。它们非常强大。见Create a dictionary with list comprehension in Python

试试这个代码,玩一下。看看对你有用吗。你知道吗

from collections import defaultdict

def extractDuplicateBoxList(self, inputs):
    counts = defaultdict(int)

    for input in inputs:
        counts[input.getShortName()] += 1
    dup_shns = set([k for k,v in counts.items() if v > 1])
    dups = [i for i in inputs if input.getShortName() in dup_shns]

    return dups

相关问题 更多 >

    热门问题