2013-07-20 34 views
-1

如果我然後嘗試刪除重複我得到一個TypeError ...爲什麼?刪除重複的字符串列表類型錯誤python

tempList =列表(集(tempList))

錯誤:類型錯誤:文件行29: 'STR' 對象不是可調用#

這纔是真正的代碼:

# Lists all UI elements 
allUI=pm.lsUI()[24:28] 

#Main Window Name 
win='searchElementsUI' 
#Lists for UI Hierachy 
allSplitUI=[] 
maxLenUI=[] 
parentDict={} 

#Splits UI Elements 
for ui in allUI: 
    allSplitUI.append(ui.split('|')) 

#Max length of UISplit 
for ui in allSplitUI: 
    maxLenUI.append(len(ui)) 
maxLenUI=max(maxLenUI) 

#Adds main Parents to list 
tempList=[] 
for i in range(maxLenUI): 
    tempList=[] 
    for ui in allSplitUI: 
     try: 
      tempList.append(ui[i]) 

     except:pass 
    tempList=list(set(tempList)) 
    parentDict['list%s'%i]=tempList 

這裏從maya完整回溯:

# Lists all UI elements 
allUI=pm.lsUI() 

#Main Window Name 
win='searchElementsUI' 
#Lists for UI Hierachy 
allSplitUI=[] 
maxLenUI=[] 
parentDict={} 

#Splits UI Elements 
for ui in allUI: 
    allSplitUI.append(ui.split('|')) 

#Max length of UISplit 
for ui in allSplitUI: 
    maxLenUI.append(len(ui)) 
maxLenUI=max(maxLenUI) 

#Adds main Parents to list 
tempList=[] 
for i in range(maxLenUI): 
    tempList=[] 
    for ui in allSplitUI: 
     try: 
      tempList.append(ui[i]) 

     except:pass 
    tempList=list(set(tempList)) 
    parentDict['list%s'%i]=tempList 
# Error: 'str' object is not callable 
# Traceback (most recent call last): 
# File "<maya console>", line 29, in <module> 
# TypeError: 'str' object is not callable # 
+1

這不是你真正的代碼。你不會從那個代碼中得到這個錯誤。 – Marcin

+0

我添加了真實的代碼。也許你可以幫忙 – arvidurs

+1

添加整個真實的堆棧跟蹤。此外,這仍然不是你真正的代碼,因爲第29行不會有這個錯誤。 – Marcin

回答

1

如上所述,您要麼沒有發佈您的真實代碼,要麼發生了一些非常奇怪的事情。

但是,你可以用下面的行替換幾乎你的整個腳本:

import itertools 
alluis = set(itertools.chain.from_iterable(ui.split('|') for ui in pm.lsUI())) 

這種分裂,變平,並使用set到uniquify。

+0

感謝Marcin, 我真的很陌生,所以任何幫助都非常感謝。 我實際上試着做的是使用cmds.treeView創建一個簡單的層次佈局。它應該創建一個maya中的所有UI元素的層次結構。 因此,我可以將按鈕或海關控制添加到mayas ui的任何部分。 也許你有一個更簡單的方法。 itertools,已經幫了很多!謝謝 – arvidurs

+0

@arvidurs我不熟悉maya。我假設你正在使用3d渲染程序,而不是其他一些maya。 – Marcin

+0

是的3D軟件。 – arvidurs

0

您可以試試這個代碼:

#Convert to a set 
a =set(tempList) 

seen = set() 
result = [] 
for item in a: 
    if item not in seen: 
     seen.add(item) 
     result.append(item)