删除字符串列表中的Duplicate类型错误python

时间:2013-07-20 20:55:18

标签: python maya

如果我然后尝试删除重复项,我会收到TypeError ...为什么?

tempList =列表(集(tempList))

错误:TypeError:文件第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

这里是玛雅的完整追溯:

# 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 # 

2 个答案:

答案 0 :(得分:1)

如上所述,您要么没有发布您的真实代码,要么发生了非常奇怪的事情。

但是,您可以使用以下行替换几乎整个脚本:

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

这会分裂,展平并使用set进行统一。

答案 1 :(得分: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)