简体   繁体   中英

Remove following duplicates in a tuple

I have tuples of arbitrary size. This is an example:

ax = ('0','1','1','1','2','2','2','3')

For x axis labeling I want to convert this tuple to:

ax = ('0','1','','','2','','','3')

So duplicates should be erased while the tuple size should stay the same. Is there an easy way to do that?

In [12]: seen = set()

In [13]: [x if x not in seen and not seen.add(x) else '' for x in ax]
Out[13]: ['0', '1', '', '', '2', '', '', '3']

This is a slightly modified version of a uniquifier suggested by Dave Kirby, here .


seen.add(x) adds x to the set seen . The seen.add method returns None . So in a boolean context, (since bool(None) is False ), not seen.add(x) is always True . Therefore the condition

x not in seen and not seen.add(x)

has a boolean value equal to

x not in seen and True

which is equivalent to

x not in seen

So the conditional expression

x if x not in seen and not seen.add(x) else ''

returns x if x is not already in seen and returns '' if x is already in seen (and x then gets added to seen ). If x not in seen is False (that is, if x is already in seen ) then seen.add(x) is not called because Python's and short-circuits -- any expression of the form False and something is automatically False without one having to evaluate something .


This could also be written, not as succinctly, but without the complexity, as

def replace_dupes(ax):
    result = []
    seen = set()
    for x in ax:
        if x in seen:
            result.append('')
        else:
            seen.add(x)
            result.append(x)
    return result

ax = ('0','1','1','1','2','2','2','3')
print(replace_dupes(ax))
# ['0', '1', '', '', '2', '', '', '3']

If you are just looking for adjacent duplicates, then you could make use of Python's groupby function as follows:

from itertools import groupby

ax = ['0', '1', '1', '1', '2', '2', '2', '3']
ax_output = []

for k, g in groupby(ax):
    ax_output.extend([k] + [''] * (len(list(g))-1))

print ax_output

This would give you the following list:

['0', '1', '', '', '2', '', '', '3']    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM