Initializing a set from a list

Xiaolei propheci at gmail.com
Wed Jun 21 02:38:28 EDT 2006


Robert Kern wrote:
> Xiaolei Li wrote:
> > Hi,
> >
> > I'm trying to initialize a set from a list but am unable to do so.  My
> > list "c", looks like:
> >
> > [(1.00909, 0.91969999999999996, -0.13550388182991072, 0),
> > (0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)]
> >
> > So basically a list of 2 tuples, each with 4 elements.  Since tuples
> > are immutable, I think a set should be able to support them.
> >
> > Anyway, I then do:
> >
> > set_c = set(c)
> >
> > And instead of getting a set, I get "None" when I try to print out
> > set_c.  len(set_c) complains "TypeError: len() of unsized object."
> > Help?
>
> >>> c = [(1.00909, 0.91969999999999996, -0.13550388182991072, 0),
> ... (0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)]
> >>> set_c = set(c)
> >>> set_c
> set([(1.00909, 0.91969999999999996, -0.13550388182991072, 0),
> (0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)])
> >>>
>
> Please copy-and-paste the exact code that you wrote and the exact output.

Ok, I've basically found the cause of my problem.  I'm including stuff
from pylab and that's causing some conflicts?  Here's 6 lines of code:

from pylab import *
c = [1, 2, 3, 3]
print c
set_c = set(c)
print set
print set_c

When I run that (Python 2.4.2, Pylab 0.80), I get:

[1, 2, 3, 3]

<function set at 0xb751e1b4>
None

If I remove the first line, I correctly get:

[1, 2, 3, 3]
<type 'set'>
set([1, 2, 3])

Good news is that I didn't really need pylab in the program.  So for
now, everything's working just fine.




More information about the Python-list mailing list