listerator clonage
Alan McIntyre
alan.mcintyre at esrgtech.com
Fri Feb 11 21:16:24 EST 2005
Cyril,
Here's some code that (I think) does what you want:
l = [1, 7, 3, 4, 3, 2, 1]
s, dups = set(), set()
for x in i:
if x in s:
dups.add(x)
s.add(x)
print dups
I'm sure there are more elegant ways to do it, but this seemed to be the
most straightforward way I could think of.
Hope this helps,
Alan McIntyre
ESRG LLC
http://www.esrgtech.com
Cyril BAZIN wrote:
> Hello,
>
> I want to build a function which return values which appear two or
> more times in a list:
>
> So, I decided to write a little example which doesn't work:
> #l = [1, 7, 3, 4, 3, 2, 1]
> #i = iter(l)
> #for x in i:
> # j = iter(i)
> # for y in j:
> # if x == y:
> # print x
>
> In thinked that the instruction 'j= iter(i)' create a new iterator 'j'
> based on 'i' (some kind of clone). I wrote this little test which show
> that 'j = iter(i)' is the same as 'j = i' (that makes me sad):
>
> #l = [1, 7, 3, 4, 2]
> #i = iter(l)
> #j = iter(i)
> #k = i
> #i, j, k
> (<listiterator object at 0x02167B50>, <listiterator object at
> 0x02167B50>, <listiterator object at 0x02167B50>)
>
> Just in order to test, I wrote these little test:
> #l = [1, 7, 3, 4, 2]
> #i = iter(l)
> #import pickle
> #j = pickle.loads(pickle.dumps(i))
> Traceback (most recent call last):
> File "<input>", line 1, in ?
> File "C:\Python24\lib\pickle.py", line 1386, in dumps
> Pickler(file, protocol, bin).dump(obj)
> File "C:\Python24\lib\pickle.py", line 231, in dump
> self.save(obj)
> File "C:\Python24\lib\pickle.py", line 313, in save
> rv = reduce(self.proto)
> File "C:\Python24\lib\copy_reg.py", line 69, in _reduce_ex
> raise TypeError, "can't pickle %s objects" % base.__name__
> TypeError: can't pickle listiterator objects
>
> #import copy
> #j = copy.copy(i)
> Traceback (most recent call last):
> File "<input>", line 1, in ?
> File "C:\Python24\lib\copy.py", line 95, in copy
> return _reconstruct(x, rv, 0)
> File "C:\Python24\lib\copy.py", line 320, in _reconstruct
> y = callable(*args)
> File "C:\Python24\lib\copy_reg.py", line 92, in __newobj__
> return cls.__new__(cls, *args)
> TypeError: object.__new__(listiterator) is not safe, use listiterator.__new__()
>
> So, I would like to know if there is a way to 'clone' a 'listiterator'
> object. I know that is possible in Java for example...
>
> If it is impossible, have you better ideas to find duplicate entries
> in a list...
>
> Thanks,
>
> Cyril
More information about the Python-list
mailing list