Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Robert Bradshaw
On Dec 10, 2009, at 1:55 AM, javier wrote: > Well, the list comprehension doesn't make a lot of sense here. I think they do. List comprehensions are quite fast and express the intent well. def diffE(a,b): b = set(b) return [x for x in a if x not in b] Is both the fastest (on the dat

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Simon King
Hi Florent! On Dec 10, 10:56 am, Florent Hivert wrote: > > AFAIK, a set is internally represented by a sorted binary tree that, > > ideally, is balanced. > > I think that's not true. It is implemented as a hash table. Indeed, when you > put something into a set, you need the element to be hashabl

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
but aren't all the hashable elements ordered in python ? You can compare tuples, integers, Sets, etc... I always wondered how these comparison worked ( and guesses hash functions should be hiding somewhere ) Nathann -- To post to this group, send an email to sage-devel@googlegroups.com To unsub

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Florent Hivert
Hi Simon, > Not a big surprise, I would say. I mean, think what "v in b" does, if > b is a list that does not contain v, and what it does if b is a set. > AFAIK, a set is internally represented by a sorted binary tree that, > ideally, is balanced. > > So, in order to find out that v is NOT

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Jason Grout
Nathann Cohen wrote: > Thank you very much for your answers !!! I will try to fix these small > issues along with real patches, as I go over the code in the Graph > Section... Most of the time lists are used to represent sets, and I > guess it can be improved in a few cases :-) If you know the un

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
Thank you very much for your answers !!! I will try to fix these small issues along with real patches, as I go over the code in the Graph Section... Most of the time lists are used to represent sets, and I guess it can be improved in a few cases :-) Nathann -- To post to this group, send an emai

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread javier
Well, the list comprehension doesn't make a lot of sense here. Even if you want to be as pythonic as possible, you don't want to run the comprehension through all the elements on A, when you can do it just in the elements of B: def diffD(a,b): c = copy(a) for y in b: c.remove(y)

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
Thankss !!! :-) Nathann 2009/12/10 Carlo Hamalainen : > On Thu, Dec 10, 2009 at 10:37 AM, Nathann Cohen > wrote: >> After a few tests, lists are slightly better for append/pop than set >> is for add/remove. Obviously remove(i) for lists is linear, and hence >> much longer than it is in

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Carlo Hamalainen
On Thu, Dec 10, 2009 at 10:37 AM, Nathann Cohen wrote: > After a few tests, lists are slightly better for append/pop than set > is for add/remove. Obviously remove(i) for lists is linear, and hence > much longer than it is in sets... Time complexity of various operations on sets, lists, dictionar

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
After a few tests, lists are slightly better for append/pop than set is for add/remove. Obviously remove(i) for lists is linear, and hence much longer than it is in sets... I will take a look at the Graph section considering this :-) Nathann On Dec 10, 10:30 am, Nathann Cohen wrote: > ( I forgo

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
( I forgot to add that in all these cases, lists represent Sets and nothing else ( no repetition, no order, etc... )) 2009/12/10 Nathann Cohen : > In understand, but then there are many places where lists are used in > the Graph section where sets would greatly improve the performances ! > > Natha

Re: [sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
In understand, but then there are many places where lists are used in the Graph section where sets would greatly improve the performances ! Nathann 2009/12/10 Simon King : > Hi Nathann! > > On Dec 10, 9:02 am, Nathann Cohen wrote: > [...] >> time cA=diffA(a,b) >> CPU times: user 29.36 s, sys: 0.

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Simon King
Hi Nathann! On Dec 10, 9:02 am, Nathann Cohen wrote: [...] > time cA=diffA(a,b) > CPU times: user 29.36 s, sys: 0.09 s, total: 29.45 s > Wall time: 29.76 s > > time cB=diffB(a,b) > CPU times: user 0.03 s, sys: 0.01 s, total: 0.04 s > Wall time: 0.04 s Not a big surprise, I would say. I mean, thi

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread Nathann Cohen
Hello !!! I just tried it, and I'm actually quite surprised def diffA(a,b): return [v for v in a if v not in b] def diffB(a,b): return list(Set(a).difference(Set(b))) n=10 k=1 a = range(n) b = list(Set([randint(0,n) for i in range(k)])) time cA=diffA(a,b) CPU times: user 29

[sage-devel] Re: Easy question in Python/Sage...

2009-12-10 Thread MaxTheMouse
On Dec 10, 8:58 am, Nathann Cohen wrote: > Hello everybody !!! > > I recently had to write two very easy lines of python, and I wondered > if there was ( there is ) a better way to write them. The problem is > easy : I have a list A, a list B whose elements all belong to A, and I > want to retur