删除多个嵌套列表中的重复列表项

时间:2018-10-31 14:31:28

标签: python python-3.x

我有以下列表:

values = [
    ['registrationController', 'regBean', 'firstName'],
    ['registrationController', 'regBean', 'surname'],
    ['registrationController', 'regBean', 'userName'],
    ['registrationController', 'regBean', 'password'],
    ['registrationController', 'regBean', 'confirmPassword'],
    ['registrationController', 'regBean', 'emailAddress'],
    ['registrationController', 'regBean', 'confirmEmail'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerOne', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerOne', 'answer'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerTwo', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerTwo', 'answer'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerThree', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerThree', 'answer'],
    ['registrationController', 'regBean', 'tAndCAccepted']
]

我正在尝试弄清楚如何删除此列表中所有列表中预设的值,并将这些值保留在 当他们成为唯一的某人时得到这样的东西:

unique_values = [
    ['firstName'],
    ['surname'],
    ['userName'],
    ['password'],
    ['confirmPassword'],
    ['emailAddress'],
    ['confirmEmail'],
    ['securityQuestionAndAnswerOne', 'question'],
    ['securityQuestionAndAnswerOne', 'answer'],
    ['securityQuestionAndAnswerTwo', 'question'],
    ['securityQuestionAndAnswerTwo', 'answer'],
    ['securityQuestionAndAnswerThree', 'question'],
    ['securityQuestionAndAnswerThree', 'answer'],
    ['tAndCAccepted']
]

有什么想法可以实现吗?我尝试了各种方法,但无法真正找到可行的解决方案。

5 个答案:

答案 0 :(得分:4)

使用集合交集获取所有公共元素,并使用嵌套列表理解来构建清除列表:

common = set(values[0])
for lst in values[1:]:
    common = common.intersection(lst)

unique_values = [[v for v in lst if v not in common] for lst in values]

答案 1 :(得分:2)

这是一个易于理解和阅读的版本。它将保留顺序。

common = set.intersection(*values)
reduced_values = [[value for value in l if value not in common] for l in values]

答案 2 :(得分:1)

具有列表理解:

>>> [
    [elt for elt in line if not all(elt in subline for subline in values)]
    for line in values
]

[['firstName'], ['surname'], ['userName'], ['password'], ['confirmPassword'], ['emailAddress'], ['confirmEmail'], ['securityQuestionAndAnswerOne', 'question'], ['securityQuestionAndAnswerOne', 'answer'], ['securityQuestionAndAnswerTwo', 'question'], ['securityQuestionAndAnswerTwo', 'answer'], ['securityQuestionAndAnswerThree', 'question'], ['securityQuestionAndAnswerThree', 'answer'], ['tAndCAccepted']]

答案 3 :(得分:-1)

假设每个值在列表中都是唯一的,则可以执行以下操作:

from collections import Counter
from itertools import chain

values = [
    ['registrationController', 'regBean', 'firstName'],
    ['registrationController', 'regBean', 'surname'],
    ['registrationController', 'regBean', 'userName'],
    ['registrationController', 'regBean', 'password'],
    ['registrationController', 'regBean', 'confirmPassword'],
    ['registrationController', 'regBean', 'emailAddress'],
    ['registrationController', 'regBean', 'confirmEmail'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerOne', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerOne', 'answer'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerTwo', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerTwo', 'answer'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerThree', 'question'],
    ['registrationController', 'regBean', 'securityQuestionAndAnswerThree', 'answer'],
    ['registrationController', 'regBean', 'tAndCAccepted']
]

counts = Counter(chain.from_iterable(values))
result = [[e for e in value if counts[e] != len(values)] for value in values]

print(result)

输出

[['firstName'], ['surname'], ['userName'], ['password'], ['confirmPassword'], ['emailAddress'], ['confirmEmail'], ['securityQuestionAndAnswerOne', 'question'], ['securityQuestionAndAnswerOne', 'answer'], ['securityQuestionAndAnswerTwo', 'question'], ['securityQuestionAndAnswerTwo', 'answer'], ['securityQuestionAndAnswerThree', 'question'], ['securityQuestionAndAnswerThree', 'answer'], ['tAndCAccepted']]

想法是对值中的每个元素进行计数,并获得未出现在值的所有元素中的元素:

答案 4 :(得分:-3)

trunc_lists = [tuple(sublist[2:]) for sublist in values]
ans = [list(i) for i in set(trunc_lists)]

请注意,输出是无序的