1

我有一个列表,它是函数的结果,列表是这样的:

lovers=[['Warner', 'Lugar'], ['Warner', 'Murkowski'], ['Warner', 'Smith'], ['Chambliss', 'Allard'], ['Chambliss', 'Isakson'], ['Lugar', 'Warner'], ['Lugar', 'Murkowski'], ['Lugar', 'Smith'], ['Murkowski', 'Warner'], ['Murkowski', 'Lugar'], ['Murkowski', 'Smith'], ['Allard', 'Chambliss'], ['Allard', 'Isakson'], ['Smith', 'Warner'], ['Smith', 'Lugar'], ['Smith', 'Murkowski'], ['Grassley', 'Talent'], ['Grassley', 'Bond'], ['Grassley', 'Allen'], ['Grassley', 'Roberts'], ['Talent', 'Grassley'], ['Talent', 'Bond'], ['Talent', 'Allen'], ['Talent', 'Roberts'], ['Durbin', 'Sarbanes'], ['Bond', 'Grassley'], ['Bond', 'Talent'], ['Bond', 'Allen'], ['Bond', 'Roberts'], ['Allen', 'Grassley'], ['Allen', 'Talent'], ['Allen', 'Bond'], ['Allen', 'Roberts'], ['Sarbanes', 'Durbin'], ['Roberts', 'Grassley'], ['Roberts', 'Talent'], ['Roberts', 'Bond'], ['Roberts', 'Allen'], ['Isakson', 'Chambliss'], ['Isakson', 'Allard']]

我想要将此列表转换为有重复项的列表。即如果['Warner', 'Lugar']已经存在那么我不想要['Lugar', 'Warner']。我怎样才能做到这一点?

4

5 回答 5

2

为什么不使用frozenset

from pprint import pprint
pprint(set(map(frozenset, lovers)))
set([frozenset(['Bond', 'Roberts']),
     frozenset(['Lugar', 'Smith']),
     frozenset(['Murkowski', 'Warner']),
     frozenset(['Allard', 'Chambliss']),
     frozenset(['Allen', 'Roberts']),
     frozenset(['Roberts', 'Talent']),
     frozenset(['Allard', 'Isakson']),
     frozenset(['Bond', 'Talent']),
     frozenset(['Lugar', 'Warner']),
     frozenset(['Allen', 'Talent']),
     frozenset(['Allen', 'Grassley']),
     frozenset(['Allen', 'Bond']),
     frozenset(['Smith', 'Warner']),
     frozenset(['Lugar', 'Murkowski']),
     frozenset(['Chambliss', 'Isakson']),
     frozenset(['Grassley', 'Roberts']),
     frozenset(['Murkowski', 'Smith']),
     frozenset(['Bond', 'Grassley']),
     frozenset(['Grassley', 'Talent']),
     frozenset(['Durbin', 'Sarbanes'])])

您还可以使用:

pprint(OrderedDict.fromkeys((map(frozenset, lovers))).keys())

它保留了订单对,但任意选择了这些对中的一个值。(如果这很重要,您可能还希望考虑使用OrderedSet配方而不是使用OrderedDict)。

于 2013-07-14T15:06:37.317 回答
0

使用元组而不是列表:

lovers=[('Warner', 'Lugar'), ('Warner', 'Murkowski'), ('Warner', 'Smith'), ... ]

由于元组是不可变的,因此您可以将整个列表转换为一个集合以删除重复项。

于 2013-07-14T15:03:55.853 回答
0

为什么不能做成套装?

def remove_duplicates(lst):
    return set([tuple(sorted(pair)) for pair in lst])

使用它:

>>> unique_lovers = sorted(remove_duplicates(lovers))
>>> for pair in unique_lovers:
    print pair

('Allard', 'Chambliss')
('Allard', 'Isakson')
('Allen', 'Bond')
('Allen', 'Grassley')
('Allen', 'Roberts')
('Allen', 'Talent')
('Bond', 'Grassley')
('Bond', 'Roberts')
('Bond', 'Talent')
('Chambliss', 'Isakson')
('Durbin', 'Sarbanes')
('Grassley', 'Roberts')
('Grassley', 'Talent')
('Lugar', 'Murkowski')
('Lugar', 'Smith')
('Lugar', 'Warner')
('Murkowski', 'Smith')
('Murkowski', 'Warner')
('Roberts', 'Talent')
('Smith', 'Warner')

唯一的“缺点”是你失去了列表顺序,还有恋人本身的顺序。

于 2013-07-14T15:04:19.290 回答
0

将内部列表转换为frozensets,然后将整个列表转换为一个集合:

frozenSetified = [frozenset(pair) for pair in lovers]
set(frozenSetified)
>>> {frozenset({'Allen', 'Bond'}), frozenset({'Roberts', 'Grassley'}), frozenset({'Isakson', 'Allard'}), frozenset({'Warner', 'Smith'}), frozenset({'Allen', 'Talent'}), frozenset({'Lugar', 'Smith'}), frozenset({'Murkowski', 'Lugar'}), frozenset({'Bond', 'Grassley'}), frozenset({'Talent', 'Grassley'}), frozenset({'Roberts', 'Bond'}), frozenset({'Murkowski', 'Warner'}), frozenset({'Roberts', 'Talent'}), frozenset({'Isakson', 'Chambliss'}), frozenset({'Sarbanes', 'Durbin'}), frozenset({'Allen', 'Grassley'}), frozenset({'Lugar', 'Warner'}), frozenset({'Murkowski', 'Smith'}), frozenset({'Allen', 'Roberts'}), frozenset({'Bond', 'Talent'}), frozenset({'Chambliss', 'Allard'})}
于 2013-07-14T15:08:56.603 回答
0

由于您不想使用set

newlist = list()
for item in lovers:
    if not filter(newlist.count,(item,item[::-1])):
        newlist.append(item)
print newlist

输出:

[['Warner', 'Lugar'], ['Warner', 'Murkowski'], ['Warner', 'Smith'], ['Chambliss', 'Allard'], ['Chambliss', 'Isakson'], ['Lugar', 'Murkowski'], ['Lugar', 'Smith'], ['Murkowski', 'Smith'], ['Allard', 'Isakson'], ['Grassley', 'Talent'], ['Grassley', 'Bond'], ['Grassley', 'Allen'], ['Grassley', 'Roberts'], ['Talent', 'Bond'], ['Talent', 'Allen'], ['Talent', 'Roberts'], ['Durbin', 'Sarbanes'], ['Bond', 'Allen'], ['Bond', 'Roberts'], ['Allen', 'Roberts']]
于 2013-07-14T15:12:56.140 回答