473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Initializing a set from a list

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.

Jun 21 '06 #1
4 4383
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.

--
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

Jun 21 '06 #2
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.

Jun 21 '06 #3

Sybren Stuvel wrote:
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.

Jun 21 '06 #4
Xiaolei wrote:
Sybren Stuvel wrote:
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

Jun 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
3
by: tkpmep | last post by:
I want to create a list of lists, each of which is identical, but which can be modified independently i.e: >>>x = , , ] >>> x.append(1) >>> x , , ] The above construct works if I have only...
9
by: Dennis Jones | last post by:
Hi, I have some old code that I am refactoring to use smart pointers and have run into a small problem. My original code looks something like this: class WorkerThread { std::map<int,...
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
16
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a...
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.