Python 2.7:从列表中获取所有唯一值,删除任何已重复或已重复的值

问题描述 投票:0回答:10

在Python 2.7中只从列表中获取唯一值的合理方法是什么?例如,如果重复某个值,请从列表中删除重复和原始值。因此,我们只剩下从未重复过的值​​。

直觉上,我会从列表中创建一个set,并从bag创建一个multiset(即list),并从list MINUS bag集合中获取set中的值。但是,我在Python 2.7中找不到bag(或multiset)的简单实现。有什么建议?仅使用set和bag操作来实现它会很好。

示例这是我最好的实现,使用强力方法:

此示例使用朋友列表,如果朋友被多次列出,他们不再是朋友:

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
unique_list_of_friends = []
for friend in list_of_friends:
    if (friend in list_of_friends):
        list_of_friends.remove(friend)
        if (friend not in list_of_friends):
            unique_list_of_friends.append(friend)
        # Ensure that all duplicates of current friend are removed
        list_of_friends = [f for f in list_of_friends if f != friend]

最后,我们留下:

unique_list_of_friends = ['jenna','brad']
python list python-2.7
10个回答
2
投票

这种方式与您使用set / bag描述的方法相匹配

>>> from collections import Counter
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> S = set(list_of_friends)
>>> bag = Counter(list_of_friends)
>>> S.difference(bag - Counter(S))
set(['brad', 'jenna'])

0
投票
a = [1,2,3,4,5,6,6,5,4,3,2,1,1,12,4,6,2,3,7,9,4,1]
b = []
print(a)
c = []
for i in range(len(a)):
    for j in range(len(b)):
        if(a[i] == b[j]):
            count = count + 1

    if(count == 0):
        b.append(a[i])
    count = 0
print(b)

输出:

[1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 1, 12, 4, 6, 2, 3, 7, 9, 4, 1]

[1, 2, 3, 4, 5, 6, 12, 7, 9]


6
投票
>>> from collections import Counter
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> [k for k, v in Counter(list_of_friends).items() if v == 1]
['brad', 'jenna']
>>> 

3
投票

你可以使用Counter

>>> from collections import Counter
>>> c = Counter(['bill','bill','mark','jenna','brad','mark'])
>>> [k for k in c.keys() if c[k] == 1]
['brad', 'jenna']

1
投票

试试这个:

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
unique_list_of_friends = [i for i in list_of_friends if list_of_friends.count(i) == 1]
print(unique_list_of_friends)

1
投票

如果由于某种原因你不能使用Counter(呃为什么?)

list_of_friends = ['bill','bill','mark','jenna','brad','mark']
a_set = set()
b_set = set()
for friend in list_of_friends:
    if friend not in a_set:
        a_set.add(friend)
    else:
        b_set.add(friend)
result = a_set - b_set

0
投票

或者您可以使用递归:

In [83]: def retUn(x):
   ....:     if not x: return []
   ....:     if x[0] in x[1:]: return retUn( filter(lambda m: m != x[0], x) )
   ....:     else: return [ x[0] ] + retUn( x[1:] )
   ....:

In [84]: retUn(x)
Out[84]: ['jenna', 'brad']

0
投票

这很可怕而且太聪明了但是它有效:

list_of_friends = ['bill', 'bill', 'mark', 'jenna', 'brad', 'mark']
set_of_friends = set(list_of_friends)
[list_of_friends.remove(friend) for friend in set_of_friends]

real_friends = [friend for friend in set_of_friends if friend not in list_of_friends]
print(real_friends) # ['brad', 'jenna']

0
投票

只是为了增加答案的花香,这里有一个使用itertools.groupby()。这与Counter m \ ethod相同,但可能效率较低。

>>> import itertools
>>> list_of_friends = ['bill','bill','mark','jenna','brad','mark']
>>> [k for k,v in itertools.groupby(sorted(list_of_friends)) if len(list(v)) == 1]
['brad', 'jenna']

0
投票

一个好的旧列表理解与内部过滤器应该做的伎俩:

>>> a = [1, 1, 2, 3, 5, 3]
>>> b = [i for i in a if len(filter(lambda x,val=i: x == val, a)) == 1]
>>> b
[2, 5]

要小心,因为它不会改变初始列表但会构建一个新列表。

© www.soinside.com 2019 - 2024. All rights reserved.