Python equivalent of uniq

Run the given command:

$ man uniq

We get the following result:

uniq: report or omit multiple lines

Python has a way to implement this functionality. Of course one can do the following:

import commands

cmd = < # uniq command usage >

status, output = commands.getstatusoutput(cmd)

Using commands allows us to run any bash command from within Python.

The itertools module provides us with group by, which can be used to give results similar to uniq.

It can group “adjacent duplicate elements” together.

For the following code, an input of HHiiiii! will yield an output of

[(2,’H’), (5,’i’), (1,’!’)]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: