我有一个嵌套的python列表,称为:
from test1 import a,b
print a
print b
在该列表中,我将每个元素加倍。
示例:
[[[[['p'], ['q'], ['t'], ['u']], [['r'], ['s'], ['v'], ['w']]]],
[[[['p'], ['q'], ['t'], ['w']], [['r'], ['s'], ['v'], ['u']]]],
[[[['p'], ['q'], ['v'], ['u']], [['r'], ['s'], ['t'], ['w']]]],
[[[['p'], ['q'], ['v'], ['w']], [['r'], ['s'], ['t'], ['u']]]],
[[[['p'], ['s'], ['t'], ['u']], [['r'], ['q'], ['v'], ['w']]]],
[[[['p'], ['s'], ['t'], ['w']], [['r'], ['q'], ['v'], ['u']]]],
[[[['p'], ['s'], ['v'], ['u']], [['r'], ['q'], ['t'], ['w']]]],
[[[['p'], ['s'], ['v'], ['w']], [['r'], ['q'], ['t'], ['u']]]],
[[[['r'], ['q'], ['t'], ['u']], [['p'], ['s'], ['v'], ['w']]]],
[[[['r'], ['q'], ['t'], ['w']], [['p'], ['s'], ['v'], ['u']]]],
[[[['r'], ['q'], ['v'], ['u']], [['p'], ['s'], ['t'], ['w']]]],
[[[['r'], ['q'], ['v'], ['w']], [['p'], ['s'], ['t'], ['u']]]],
[[[['r'], ['s'], ['t'], ['u']], [['p'], ['q'], ['v'], ['w']]]],
[[[['r'], ['s'], ['t'], ['w']], [['p'], ['q'], ['v'], ['u']]]],
[[[['r'], ['s'], ['v'], ['u']], [['p'], ['q'], ['t'], ['w']]]],
[[[['r'], ['s'], ['v'], ['w']], [['p'], ['q'], ['t'], ['u']]]]]
与
相同[[[['p'], ['q'], ['t'], ['u']], [['r'], ['s'], ['v'], ['w']]]]
如何使用列表理解功能删除双精度元素? 我试图将列表转换为集合,但这不起作用。
所需的输出应为:
[[[['r'], ['s'], ['v'], ['w']], [['p'], ['q'], ['t'], ['u']]]]
谢谢
答案 0 :(得分:1)
您可以使用每个子列表中的拼合值创建字典:
d = [[[[['p'], ['q'], ['t'], ['u']], [['r'], ['s'], ['v'], ['w']]]], [[[['p'], ['q'], ['t'], ['w']], [['r'], ['s'], ['v'], ['u']]]], [[[['p'], ['q'], ['v'], ['u']], [['r'], ['s'], ['t'], ['w']]]], [[[['p'], ['q'], ['v'], ['w']], [['r'], ['s'], ['t'], ['u']]]], [[[['p'], ['s'], ['t'], ['u']], [['r'], ['q'], ['v'], ['w']]]], [[[['p'], ['s'], ['t'], ['w']], [['r'], ['q'], ['v'], ['u']]]], [[[['p'], ['s'], ['v'], ['u']], [['r'], ['q'], ['t'], ['w']]]], [[[['p'], ['s'], ['v'], ['w']], [['r'], ['q'], ['t'], ['u']]]], [[[['r'], ['q'], ['t'], ['u']], [['p'], ['s'], ['v'], ['w']]]], [[[['r'], ['q'], ['t'], ['w']], [['p'], ['s'], ['v'], ['u']]]], [[[['r'], ['q'], ['v'], ['u']], [['p'], ['s'], ['t'], ['w']]]], [[[['r'], ['q'], ['v'], ['w']], [['p'], ['s'], ['t'], ['u']]]], [[[['r'], ['s'], ['t'], ['u']], [['p'], ['q'], ['v'], ['w']]]], [[[['r'], ['s'], ['t'], ['w']], [['p'], ['q'], ['v'], ['u']]]], [[[['r'], ['s'], ['v'], ['u']], [['p'], ['q'], ['t'], ['w']]]], [[[['r'], ['s'], ['v'], ['w']], [['p'], ['q'], ['t'], ['u']]]]]
def flatten(_d):
return [[c for [c] in _d[0][0]], [c for [c] in _d[0][1]]]
def match(a, b):
return all(c == d for c, d in zip(sorted(a), sorted(b)))
_d = {i:flatten(a) for i, a in enumerate(d)}
result = [a for i, a in enumerate(d) if not any(match(_d[i], _d[c]) for c in range(i))]
输出:
[[[[['p'], ['q'], ['t'], ['u']], [['r'], ['s'], ['v'], ['w']]]],
[[[['p'], ['q'], ['t'], ['w']], [['r'], ['s'], ['v'], ['u']]]],
[[[['p'], ['q'], ['v'], ['u']], [['r'], ['s'], ['t'], ['w']]]],
[[[['p'], ['q'], ['v'], ['w']], [['r'], ['s'], ['t'], ['u']]]],
[[[['p'], ['s'], ['t'], ['u']], [['r'], ['q'], ['v'], ['w']]]],
[[[['p'], ['s'], ['t'], ['w']], [['r'], ['q'], ['v'], ['u']]]],
[[[['p'], ['s'], ['v'], ['u']], [['r'], ['q'], ['t'], ['w']]]],
[[[['p'], ['s'], ['v'], ['w']], [['r'], ['q'], ['t'], ['u']]]]]