Initializing a set from a list

X

Xiaolei Li

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?

Thank you.
 
R

Robert Kern

Xiaolei said:
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.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
S

Sybren Stuvel

Xiaolei Li enlightened us with:
My list "c", looks like:

[(1.00909, 0.91969999999999996, -0.13550388182991072, 0),
(0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)]

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.

What are the exact commands you use, and what version of Python do you
have? It works fine here:

Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
c=[(1.00909, 0.91969999999999996, -0.13550388182991072, 0), .... (0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)]
set(c)
set([(1.00909, 0.91969999999999996, -0.13550388182991072, 0),
(0.87423999999999991, 0.6666700000000001, -0.21230487137222254, 0)])

Sybren
 
X

Xiaolei

Robert said:
Xiaolei said:
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.
 
S

Sybren Stuvel

Xiaolei enlightened us with:
from pylab import *

You'd better not do that. Just use "import pylab".
If I remove the first line, I correctly get:

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

Pylab shadows the built-in set name, which is one of the reasons you
should generally use "import XXX" instead of "from XXX import *".

Sybren
 
X

Xiaolei

Sybren said:
Xiaolei enlightened us with:
from pylab import *

You'd better not do that. Just use "import pylab".
If I remove the first line, I correctly get:

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

Pylab shadows the built-in set name, which is one of the reasons you
should generally use "import XXX" instead of "from XXX import *".

Ahh. Understood. Thank you very much.
 
R

Robert Kern

Xiaolei said:
Sybren said:
Xiaolei enlightened us with:
from pylab import *

You'd better not do that. Just use "import pylab".
If I remove the first line, I correctly get:

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

Pylab shadows the built-in set name, which is one of the reasons you
should generally use "import XXX" instead of "from XXX import *".

Ahh. Understood. Thank you very much.

It should be noted that recent versions of matplotlib's pylab module have
changed that function to setp() in order to avoid this problem.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top