finding items that occur more than once in a list
Michael Mabin
d3vvnull at gmail.com
Tue Mar 18 18:09:19 EDT 2008
How about using list comprehension?
l1 = ["apples","apples","bananas","oranges","oranges","peaches"]
s1 = set([x for x in l1 if l1.count(x) > 1])
On Tue, Mar 18, 2008 at 4:56 PM, sturlamolden <sturlamolden at yahoo.no> wrote:
> On 18 Mar, 22:25, sturlamolden <sturlamol... at yahoo.no> wrote:
>
> > def nonunique(lst):
> > slst = sorted(lst)
> > return list(set([s[0] for s in
> > filter(lambda t : t[0] != t[1], zip(slst[:-1],slst[1:]))]))
>
> Obviously that should be 'lambda t : t[0] == t[1]'. Instead of using
> the set function, there is more structure to exploit when the list is
> sorted:
>
>
> def nonunique(lst):
> slst = sorted(lst)
> dups = [s[0] for s in
> filter(lambda t : t[0] == t[1], zip(slst[:-1],slst[1:]))]
> return [dups[0]] + [s[1] for s in
> filter(lambda t : t[0] != t[1], zip(dups[:-1],dups[1:]))]
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
| _ | * | _ |
| _ | _ | * |
| * | * | * |
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080318/65d3b5ee/attachment-0001.html>
More information about the Python-list
mailing list