On Apr 24, 4:41 pm, norseman norse...@hughes.net wrote:
(How do I) ...explain these?
[...]
I can get the sashimi and take it home and BBQ it, I can roast it, I can
steam it, I can wok it,..., but the other is what it is. (greasy)
Besides, who says I like your cooking? :)
Err...
norseman wrote:
Of course each method has its time and place of use and Python has some
well oiled list search and list maintain routines of its own. List
comparisons are most accurate when using presorted ones. (Some things
don't lend themselves to sorting very well. Like paragraphs, books,
Esmail:
oh, I forgot to mention that each list may contain duplicates.
Comparing the sorted lists is a possible O(n ln n) solution:
a.sort()
b.sort()
a == b
Another solution is to use frequency dicts, O(n):
from itertools import defaultdict
d1 = defaultdict(int)
for el in a:
d1[el] += 1
John Yeung gallium.arsen...@gmail.com (JY) wrote:
JY It takes care of the duplicates, but so does your initial solution,
JY which I like best:
sorted(a)==sorted(b)
JY This is concise, clear, and in my opinion, the most Pythonic. It may
JY well even be the fastest. (If you didn't have to
On Thu, 23 Apr 2009 21:51:42 -0400, Esmail wrote:
set(a) == set(b)# test if a and b have the same elements
# check that each list has the same number of each element # i.e.
[1,2,1,2] == [1,1,2,2], but [1,2,2,2] != [1,1,1,2] for elem in set(a):
a.count(elem) == b.count(elem)
Ah
John Yeung wrote:
so does your initial solution,
which I like best:
sorted(a)==sorted(b)
This is concise, clear, and in my opinion, the most Pythonic. It may
well even be the fastest.
Great .. I can live with that :-)
--
http://mail.python.org/mailman/listinfo/python-list
MRAB wrote:
You could use Raymond Hettinger's Counter class:
http://code.activestate.com/recipes/576611/
on both lists and compare them for equality.
thanks for the pointer, I'll study the code provided.
Esmail
--
http://mail.python.org/mailman/listinfo/python-list
Arnaud Delobelle:
Thanks to the power of negative numbers, you only need one dict:
d = defaultdict(int)
for x in a:
d[x] += 1
for x in b:
d[x] -= 1
# a and b are equal if d[x]==0 for all x in d:
not any(d.itervalues())
Very nice, I'll keep this for future use.
Someday I'll have
On Apr 24, 7:12 am, bearophileh...@lycos.com wrote:
[...]
Another solution is to use frequency dicts, O(n):
from itertools import defaultdict
d1 = defaultdict(int)
for el in a:
d1[el] += 1
d2 = defaultdict(int)
for el in b:
d2[el] += 1
d1 == d2
Thanks to the power of negative
Thanks all, after reading all the posting and suggestions for
alternatives, I think I'll be going with
sorted(a)==sorted(b)
it seems fast, intuitive and clean and can deal with
duplicates too.
Best,
Esmail
--
http://mail.python.org/mailman/listinfo/python-list
Piet van Oostrum wrote:
John Yeung gallium.arsen...@gmail.com (JY) wrote:
JY It takes care of the duplicates, but so does your initial solution,
JY which I like best:
sorted(a)==sorted(b)
JY This is concise, clear, and in my opinion, the most Pythonic. It may
JY well even be the
Steven D'Aprano wrote:
On Thu, 23 Apr 2009 21:51:42 -0400, Esmail wrote:
set(a) == set(b)# test if a and b have the same elements
# check that each list has the same number of each element # i.e.
[1,2,1,2] == [1,1,2,2], but [1,2,2,2] != [1,1,1,2] for elem in set(a):
a.count(elem) ==
Esmail wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my lists will be larger),
a=[1, 2, 3]
b=[2, 3, 1]
should yield true if a==b
On Fri, 24 Apr 2009 10:39:39 -0700, norseman wrote:
Technically, == is reserved for identical, as in byte for byte same
Really? Then how do you explain these?
u'abc' == 'abc'
True
1 == 1.0
True
2L == 2
True
import decimal
decimal.Decimal('42') == 42
True
Here's one to think about:
Steven D'Aprano wrote:
On Fri, 24 Apr 2009 10:39:39 -0700, norseman wrote:
Technically, == is reserved for identical, as in byte for byte same
Really? Then how do you explain these?
u'abc' == 'abc'
True
1 == 1.0
True
2L == 2
True
import decimal
decimal.Decimal('42') == 42
True
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my lists will be larger),
a=[1, 2, 3]
b=[2, 3, 1]
should yield true if a==b
I suppose I
+hans.dushanthakumar=hcn.com...@python.org]
On Behalf Of Esmail
Sent: Friday, 24 April 2009 11:31 AM
To: python-list@python.org
Subject: best way to compare contents of 2 lists?
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists
On Thu, Apr 23, 2009 at 9:31 PM, Esmail ebo...@hotmail.com wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my lists will be
Esmail wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my lists will be larger),
a=[1, 2, 3]
b=[2, 3, 1]
should yield true if a==b
David Robinow wrote:
On Thu, Apr 23, 2009 at 9:31 PM, Esmail ebo...@hotmail.com wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my
Esmail wrote:
Esmail wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
E.g. a trivial example (my lists will be larger),
a=[1, 2, 3]
b=[2, 3, 1]
should
Hans DushanthaKumar wrote:
'set' comes to mind,
Yes, I thought about that, but I wasn't sure how to potentially
deal with different number of duplicates in the lists. The post
by David seems to address that issue nicely.
though I'm not sure if there are performance
inplications with large
Esmail ebo...@hotmail.com wrote:
What is the best way to compare the *contents* of two different
lists regardless of their respective order? The lists will have
the same number of items, and be of the same type.
Best can mean different things. Fastest? Shortest code? Most
readable?
David
23 matches
Mail list logo