无法从String生成输出差异

时间:2014-03-07 10:50:34

标签: python maya

首先,如果我的主题标题错误,我道歉,因为我不知道如何以更好的方式表达它。我试图在列表中打印出不包含单词Shape的项目,但我根本无法获得输出。它产生了一些东西,但它仍然是错误的。

在下面的代码中,我已经写出了输出,但正如您在最后一行中所看到的,它只是生成"group1", "locator1" and "pCube1"而不是生成pCubeShape1

有人可以就此提出建议吗?非常感谢提前。

import maya.cmds as cmds

newSel01 = cmds.ls(sl=True)
# [u'group1', u'locator1', u'locatorShape1', u'pCube1', u'pCubeShape1']

if "Shape" in str(newSel01):
    if item in newSel01:
        print item
        # pCubeShape1

4 个答案:

答案 0 :(得分:4)

根据我的理解,您想要过滤掉列表中不包含“Shape”一词的所有项目:

>>> x = [u'group1', u'locator1', u'locatorShape1', u'pCube1', u'pCubeShape1']
>>> filter(lambda s:  "Shape" not in s, x)                                                                                                                                     
[u'group1', u'locator1', u'pCube1']

答案 1 :(得分:4)

如果您尝试从选择中获取变换,则ls命令具有transforms参数。

ls(sl=True, transforms=True)

将过滤掉任何形状。

或者,shapes参数:

ls(sl=True, shapes=True)

会做反过来。


这样就不需要进行任何字符串比较或正则表达式搜索。这主要是一个FYI;所有其他答案都可以用来做你所要求的。

答案 2 :(得分:2)

>>> l = [u'group1', u'locator1', u'locatorShape1', u'pCube1', u'pCubeShape1']
>>> result = [ i for i in l if 'Shape' not in i ]
>>> result
[u'group1', u'locator1', u'pCube1']

答案 3 :(得分:1)

你快到了。尝试

for item in newSel01:
    if "Shape" not in item:
        print item

您将获取列表中的每个条目,并检查其中是否未出现字符串“Shape”并将其打印出来。这当然会分别打印每个条目。您可以将项目附加到列表或使用列表推导更简洁。

[x for x in newSel01 if "Shape" not in x]

您还可以使用filter方法

filter(lambda x: "Shape" not in x, newSel01)