3

我是 python 和一般编程的新手,需要一些帮助:我有一个列表,我在程序的前面通过从循环附加创建的(即我现在不能重新定义我的列表来解决我的问题) , 24 个 4 元组:

elementary = [(23, 1, 18, 4), (23, 1, 6, 16), (23, 1, 4, 18), (23, 2, 18, 3), (23, 2, 12, 9), (23, 2, 9, 12), (23, 2, 3, 18), (23, 3, 18, 2), (23, 3, 2, 18), (23, 4, 18, 1), (23, 4, 1, 18), (23, 5, 14, 7), (23, 5, 7, 14), (23, 6, 16, 1), (23, 6, 9, 8), (23, 6, 8, 9), (23, 6, 1, 16), (23, 7, 14, 5), (23, 7, 5, 14), (23, 8, 9, 6), (23, 8, 6, 9), (23, 9, 12, 2), (23, 9, 8, 6), (23, 9, 6, 8), (23, 9, 2, 12), (23, 12, 9, 2), (23, 12, 2, 9), (23, 14, 7, 5), (23, 14, 5, 7), (23, 16, 1, 6), (23, 18, 4, 1), (23, 18, 3, 2), (23, 18, 2, 3), (23, 18, 1, 4)]

但现在想摆脱刚刚重新排列的元组......换句话说,在第一个元组 ( (23,1,18,4)) 之后我会摆脱(23,1,4,18), (23,4,1,18),等等......,如果可能的话,我想在整个列表中执行此操作,这样我最终只会得到 6 个完全不同的 4 元组。有什么方法可以做到这一点,而无需返回并在我的程序早期做一些不同的事情?任何帮助将不胜感激。谢谢!

4

5 回答 5

3

怎么样:

{tuple(sorted(t)): t for t in elementary}.values()
于 2013-01-24T03:46:43.973 回答
2

作为 1-liner,这对每个 4 元组进行排序,然后创建一组结果,具有删除重复项的效果。我假设您的 4 元组可以更改元素的顺序。

set(tuple(sorted(i)) for i in elementary)

>>> set((5, 7, 14, 23), (6, 8, 9, 23), (2, 3, 18, 23), (1, 4, 18, 23), (1, 6, 16, 23), (2, 9, 12, 23))
于 2013-01-24T03:38:28.757 回答
0

如果您真的想通过比较感到沮丧和肮脏:

In [1028]: elementary = [(23, 1, 18, 4), (23, 1, 6, 16), (23, 1, 4, 18), (23, 2, 18, 3), (23, 2, 12, 9), (23, 2, 9, 12), (23, 2, 3, 18), (23, 3, 18, 2), (23, 3, 2, 18), (23, 4, 18, 1), (23, 4, 1, 18), (23, 5, 14, 7), (23, 5, 7, 14), (23, 6, 16, 1), (23, 6, 9, 8), (23, 6, 8, 9), (23, 6, 1, 16), (23, 7, 14, 5), (23, 7, 5, 14), (23, 8, 9, 6), (23, 8, 6, 9), (23, 9, 12, 2), (23, 9, 8, 6), (23, 9, 6, 8), (23, 9, 2, 12), (23, 12, 9, 2), (23, 12, 2, 9), (23, 14, 7, 5), (23, 14, 5, 7), (23, 16, 1, 6), (23, 18, 4, 1), (23, 18, 3, 2), (23, 18, 2, 3), (23, 18, 1, 4)]

In [1029]: for e in elementary:                                                                                                                                  
               add = True                                                                                                                                                   
               for a in answer:                                                                                                                                                 
                   if all(_e in a and e.count(_e)==a.count(_e) and len(e)==len(a) for _e in e):
                       add = False
               if add:
                   answer.append(e)    

In [1030]: answer
Out[1030]: 
[(23, 1, 18, 4),
 (23, 1, 6, 16),
 (23, 2, 18, 3),
 (23, 2, 12, 9),
 (23, 5, 14, 7),
 (23, 6, 9, 8)]
于 2013-01-24T03:44:21.830 回答
0

你真的只有24个吗?如果是这样,带有一些不必要的内存分配的慢速解决方案可能在这里工作正常,并节省您的编程时间:

elementary_unique = set(tuple(sorted(t)) for t in elementary)

现在elementary_unique是一个集合而不是一个列表 - 如果它很重要,你可以使用

elementary_unique = list(set(tuple(sorted(t)) for t in elementary))

相反,但这会比第一个版本慢一点。

于 2013-01-24T03:39:10.063 回答
0

您可以随时识别等价组合sorted(tuple1) == sorted(tuple2)

代码简短而甜蜜:

>>> set(map(tuple, map(sorted, elementary)))
set([(5, 7, 14, 23), (6, 8, 9, 23), (2, 3, 18, 23), 
     (1, 4, 18, 23), (1, 6, 16, 23), (2, 9, 12, 23)])

如果您需要保留每个第一个不同元组的顺序,则需要更多的工作:

>>> uniq = set()
>>> for t in elementary:
    s = tuple(sorted(t))
    if s not in uniq:
        uniq.add(s)
        print t

(23, 1, 18, 4)
(23, 1, 6, 16)
(23, 2, 18, 3)
(23, 2, 12, 9)
(23, 5, 14, 7)
(23, 6, 9, 8)
于 2013-01-24T03:39:52.443 回答