Returning semi-unique values from a list

2019-07-10 04:40发布

Not sure how else to word this, but say I have a list containing the following sequence:

[a,a,a,b,b,b,a,a,a]

and I would like to return:

[a,b,a]

How would one do this in principle?

4条回答
女痞
2楼-- · 2019-07-10 04:54

You can use itertools.groupby, this groups consecutive same elements in the same group and return an iterator of key value pairs where the key is the unique element you are looking for:

from itertools import groupby  

[k for k, _ in groupby(lst)]
# ['a', 'b', 'a']

lst = ['a','a','a','b','b','b','a','a','a']
查看更多
太酷不给撩
3楼-- · 2019-07-10 04:55

Psidoms way is a lot better, but I may as well write this so you can see how it'd be possible just using basic loops and statements. It's always good to figure out what steps you'd need to take for any problem, as it usually makes coding the simple things a bit easier :)

original = ['a','a','a','b','b','b','a','a','a']
new = [original[0]]

for letter in original[1:]:
    if letter != new[-1]:
        new.append(letter)

Basically it will append a letter if the previous letter is something different.

查看更多
小情绪 Triste *
4楼-- · 2019-07-10 05:01

Using list comprehension:

original = ['a','a','a','b','b','b','a','a','a']
packed = [original[i] for i in range(len(original)) if i == 0 or original[i] != original[i-1]]
print(packed)  # > ['a', 'b', 'a']

Similarly (thanks to pylang) you can use enumerate instead of range:

[ x for i,x in enumerate(original) if i == 0 or x != original[i-1] ]
查看更多
迷人小祖宗
5楼-- · 2019-07-10 05:12

more_itertools has an implementation of the unique_justseen recipe from itertools:

import more_itertools as mit

list(mit.unique_justseen(["a","a","a","b","b","b","a","a","a"]))
# ['a', 'b', 'a']
查看更多
登录 后发表回答