比较嵌套在列表中的类似项目,并删除它们

时间:2012-06-08 01:26:17

标签: python list list-comprehension maya

我试图在关系中找到重复项并从列表中省略它们。我已经尝试过嵌套的if语句,但我还没有得到正确的解决方案。

list = [[u'polySurface5.e[4]', u'polySurface5.e[1]', .5], [u'polySurface5.e[8]', u'polySurface5.e[2]', .9], [u'polySurface5.e[1]', u'polySurface5.e[4]', .5], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2], [u'polySurface5.e[2]', u'polySurface5.e[2]', 0] ]

在下面的列表中,我想删除“双打”,以及最后一项为0的值。我想最终得到这样的结果:

newlist = [[u'polySurface5.e[4]', u'polySurface5.e[1]', .5], [u'polySurface5.e[8]', u'polySurface5.e[2]', .9], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2] ]

以哪种方式比较[u'polySurface5.e [4]',u'polySurface5.e [1]',。5]和[u'polySurface5.e [1]',u'polySurface5.e [4]',。5]并删除第一个之后的任何事件?

非常感谢!

3 个答案:

答案 0 :(得分:2)

my_list = [[u'polySurface5.e[4]', u'polySurface5.e[1]', .5], [u'polySurface5.e[8]', u'polySurface5.e[2]', .9], [u'polySurface5.e[1]', u'polySurface5.e[4]', .5], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2], [u'polySurface5.e[2]', u'polySurface5.e[2]', 0] ]

new_list = []
my_set = set()
for sublist in my_list:
    if sublist[2] != 0:
        frozen_sub = frozenset(sublist)
        if not frozen_sub in my_set:
            new_list.append(sublist)
            my_set.add(frozen_sub)

结果:

>>> new_list
[[u'polySurface5.e[4]', u'polySurface5.e[1]', 0.5], [u'polySurface5.e[8]', u'polySurface5.e[2]', 0.9], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2]]

请注意,我将原始列表list的名称更改为my_list。我之所以这样做,是因为list是Python中的关键字,但是通过调用object list,您就会将其隐藏起来。看看这个:

>>> list((1,2,3))
[1, 2, 3]
>>> list = [3,4,5]
>>> list((6,7,8))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

编辑:回答后续帖子。

set是一个仅包含唯一项目且项目没有订单的数据结构。例如:

>>> set([1,2,2,4,4])
set([1, 2, 4])
>>> set([1,2,4]) == set([4,4,4,1,2])
True

frozenset是类似于set的数据结构,但它是不同的。 set是可变的,frozenset不是;那就是我们可以添加要设置的内容,但不能添加到frozenset。但是,frozenset是可清除的,而set则不是。即fozenset可以是“词典”中的键或集合中的元素。

可变性:

>>> b = set([1,2,3])
>>> b.add(6)
>>> b
set([1, 2, 3, 6])
>>> d = frozenset([1,2,3])
>>> d.add(6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

Hashability:

>>> b = set([1,2])
>>> d = set([5,3])
>>> g = set([b,d])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> b = frozenset([1,2])
>>> d = frozenset([5,3])
>>> g = set([b,d])
>>> g
set([frozenset([1, 2]), frozenset([3, 5])])

我希望这会有所帮助。

答案 1 :(得分:0)

l = [[u'polySurface5.e[4]', u'polySurface5.e[1]', .5], [u'polySurface5.e[8]', u'polySurface5.e[2]', .9], [u'polySurface5.e[1]', u'polySurface5.e[4]', .5], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2], [u'polySurface5.e[2]', u'polySurface5.e[2]', 0]]
# remove [...,0]
l = filter(lambda i: i[-1] != 0, l)
# sort keys (1st and 2nd field)
l = [sorted(i[:-1]) + i[-1:] for i in l]
# uniques
add_if_unique = lambda res, x: (res + [x]) if x not in res else res
l = reduce(add_if_unique, l, [])

答案 2 :(得分:0)

这个单线程怎么样?

l = [[u'polySurface5.e[4]', u'polySurface5.e[1]', .5],
     [u'polySurface5.e[8]', u'polySurface5.e[2]', .9],
     [u'polySurface5.e[1]', u'polySurface5.e[4]', .5],
     [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2],
     [u'polySurface5.e[2]', u'polySurface5.e[2]', 0]]

newlist = [list(member) for member in set([tuple(sorted(x[:-1]) + x[-1:]) for x in l if x[-1] != 0])]

print newlist
[[u'polySurface5.e[1]', u'polySurface5.e[4]', 0.5], [u'polySurface5.e[2]', u'polySurface5.e[8]', 0.9], [u'polySurface5.e[2]', u'polySurface5.e[9]', 1.2]]