2014-03-07 29 views
1

首先,我很抱歉如果我的主題標題是錯誤的,因爲我不知道如何以任何更好的方式對其進行描述。我試圖打印出列表中不包含單詞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

從我的理解是要篩選出不包含單詞「形狀」列表中的所有項目:

>>> 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'] 
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'] 
1

你幾乎沒有。嘗試

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

你拿起列表中的每個條目,如果字符串「形狀」不發生它並打印檢查。這當然會分別打印每個條目。您可以將這些項目追加到列表中,或者使用列表理解來更加簡潔。

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

您還可以使用filter方法

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

如果你想從選擇ls命令有一個transforms說法得到了轉換。

ls(sl=True, transforms=True) 

將過濾掉任何形狀。

另外,在shapes說法:

ls(sl=True, shapes=True) 

會做反。


這樣就不需要任何字符串比較或正則表達式搜索。這主要是一個供參考;所有其他答案都非常適合你的要求。

+0

謝謝!這對我幫助很大! – dissidia