Merging lists has made my brain hurt.
Mark McEahern
marklists at mceahern.com
Wed Oct 2 18:08:01 EDT 2002
[Max M]
> Slightly improved:
>
> d = {}
> for l in lol:
> for i in l:
> d[i] = d.setdefault(i, 0) + 1
> n=len(lol)
> common = [e for e in d if d[e]==n]
> print common
Slightly improved yet more since the above version assumes each list has
unique elements (which may have been one of the original requirements, I
don't know):
#!/usr/bin/env python
l = [1, 1, 1]
m = [2, 2, 2]
n = [3, 3, 3]
lol = [l, m, n]
d = {}
for l in lol:
unique = dict(zip(l, l))
for i in unique:
d[i] = d.setdefault(i, 0) + 1
n = len(lol)
common = [e for e in d if d[e]==n]
print common
// m
More information about the Python-list
mailing list