Question on lists

  • Thread starter Kristofer Pettijohn
  • Start date
K

Kristofer Pettijohn

My question is about lists:

Is there a way to remove duplicate items from a list that are
next to each other?

Example...

Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
will return ['a', 'b', 'c', 'd', 'e']

Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']

I'm guessing there is probably nothing internal that will do it, so
I may have to write something on my own - just thought I'd check
first ;)

Thanks!
 
A

Andrew Bennetts

My question is about lists:

Is there a way to remove duplicate items from a list that are
next to each other?

Example...

Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
will return ['a', 'b', 'c', 'd', 'e']

Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']

I'm guessing there is probably nothing internal that will do it, so
I may have to write something on my own - just thought I'd check
first ;)

Nothing builtin that I know of, but it's trivial to write:

def uniq(iterable):
"""Remove consecutive duplicates from a sequence."""

prev = object()
for element in iterable:
if element != prev:
prev = element
yield element
>>> list(uniq(['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e'])) ['a', 'b', 'c', 'd', 'e']
>>> list(uniq(['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']))
['a', 'b', 'c', 'd', 'c', 'd', 'e']

-Andrew.
 
H

Heiko Wundram

Am Mittwoch, 28. Juli 2004 05:26 schrieb Kristofer Pettijohn:
My question is about lists:

Is there a way to remove duplicate items from a list that are
next to each other?

They don't even have to be next to each other:

(>= Python 2.3 required)
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))
['a', 'c', 'b', 'd', 'ladida']

This doesn't maintain the order of the items, though. If you need that, and
you're sure your list is always sorted, use the example Andrew gave.

Heiko.
 
K

Kristofer Pettijohn

Andrew Bennetts said:
Nothing builtin that I know of, but it's trivial to write:

def uniq(iterable):
"""Remove consecutive duplicates from a sequence."""

prev = object()
for element in iterable:
if element != prev:
prev = element
yield element

Thank you! Easier than I thought. I don't know why I make things
so hard on myself.
 
K

Kristofer Pettijohn

Heiko Wundram said:
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))
['a', 'c', 'b', 'd', 'ladida']

This doesn't maintain the order of the items, though. If you need that, and
you're sure your list is always sorted, use the example Andrew gave.

Good to know for the future; unfortunately these needed to remain
in order - I'm graphing routes and paths, and if the same node shows
up 2+ times in a row, I needed them weeded out.

Thanks!
 
E

Elaine Jackson

If x is the given sequence, then
[x for i in filter(lambda i: i==0 or x[i-1]<>x, range(len(x)))]
is what you want.

| My question is about lists:
|
| Is there a way to remove duplicate items from a list that are
| next to each other?
|
| Example...
|
| Performing the operation on ['a', 'b', 'c', 'c', 'd', 'd', 'd', 'e']
| will return ['a', 'b', 'c', 'd', 'e']
|
| Performing the operation on ['a', 'b', 'c', 'c', 'd', 'c', 'd', 'e']
| will return ['a', 'b', 'c', 'd', 'c', 'd', 'e']
|
| I'm guessing there is probably nothing internal that will do it, so
| I may have to write something on my own - just thought I'd check
| first ;)
|
| Thanks!
|
| --
| Kristofer Pettijohn
| (e-mail address removed)
 
A

Aahz

If x is the given sequence, then
[x for i in filter(lambda i: i==0 or x[i-1]<>x, range(len(x)))]
is what you want.


Please report to the UnPythonic Activities Committee. ;-)
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/

"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." (e-mail address removed)
 
A

Alan G Isaac

Heiko Wundram said:
import sets
list(sets.Set(['a','a','b','b','d','b','c','a','ladida']))
['a', 'c', 'b', 'd', 'ladida']


Newbie question:
OK, this is great. But I was not aware of this module,
and I was working in my own sets.py.
So how do I handle this namespace issue?
I.e., how can I 'import sets' and get the Python module,
when my own script is named sets.py?

Thanks,
Alan Isaac
 
C

Christopher T King

Newbie question:
OK, this is great. But I was not aware of this module,
and I was working in my own sets.py.
So how do I handle this namespace issue?
I.e., how can I 'import sets' and get the Python module,
when my own script is named sets.py?

The best way, of course, is to rename your script ;) it's never a good
idea to give a script the same name as that of a module you plan to use.

If you absolutely must leave your script named sets.py, you can use the
following trick to get to Python's sets.py:

import sys
sys.path.remove('')
import sets

This removes the current directory from Python's search path. Note you
won't be able to access any modules in the current directory after that
line, unless you do a "sys.path.insert(0,'')" first.
 
M

Martin Maney

Aahz said:
Elaine Jackson said:
If x is the given sequence, then
[x for i in filter(lambda i: i==0 or x[i-1]<>x, range(len(x)))]
is what you want.

Please report to the UnPythonic Activities Committee. ;-)

Has the committee considered the other functional solution?
.... if not lst or lst[-1] != x:
.... lst.append(x)
.... return lst
....
['a', 'b', 'c', 'd', 'e']

If it would help, I'm sure we could get an obfuscation expert to make
it into one line, but without a first-class conditional expression,
it's nothing but a circus trick. Perl envy, like.

Oh, okay, twist my arm...
reduce(lambda r,x: (not r or r[-1]!=x) and r.append(x) or r, l, [])
['a', 'b', 'c', 'd', 'e']

I knew reduce would come in handy for something.
"To me vi is Zen. To use vi is to practice zen. Every command is a
koan. Profound to the user, unintelligible to the uninitiated. You
discover truth everytime you use it." (e-mail address removed)

Apparently vi also shares with koans the characteristic of battering
down the walls of rationality. :)

--
Although we may never know with complete certainty the identity
of the winner of this year's presidential election, the identity
of the loser is perfectly clear. It is the nation's confidence
in the judge as an impartial guardian of the law.
- Justice John Paul Stevens, from his dissenting opinion Dec 12, 2000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,218
Latest member
NatishaFin

Latest Threads

Top