Re: How to check all elements of a list are same or different

2009-04-16 Thread Arnaud Delobelle
Piet van Oostrum writes: >> John Machin (JM) wrote: > >>JM> On Apr 16, 8:14 am, Chris Rebert wrote: def all_same(lst):     return len(set(lst)) == 1 def all_different(lst):     return len(set(lst)) == len(lst) > >>JM> @ OP: These are very reasonable interpret

Re: How to check all elements of a list are same or different

2009-04-16 Thread Piet van Oostrum
> John Machin (JM) wrote: >JM> On Apr 16, 8:14 am, Chris Rebert wrote: >>> >>> def all_same(lst): >>>     return len(set(lst)) == 1 >>> >>> def all_different(lst): >>>     return len(set(lst)) == len(lst) >JM> @ OP: These are very reasonable interpretations of "all same" and "all >JM> dif

Re: How to check all elements of a list are same or different

2009-04-15 Thread Miles
On Wed, Apr 15, 2009 at 8:48 PM, Paul Rubin wrote: > I'd use: > >   from operator import eq >   all_the_same = reduce(eq, mylist) That won't work for a sequence of more than two items, since after the first reduction, the reduced value that you're comparing to is the boolean result: >>> reduce(eq

Re: How to check all elements of a list are same or different

2009-04-15 Thread Paul Rubin
John Posner writes: > # get list of object-IDs > ids = map(lambda x: id(x), mylist) > # ALL THE SAME? ... test whether "average ID" matches "first ID" > sum(ids)/len(ids) == ids[0] I don't think you can rely on id's being the same if what you want is that the values are the same: >>>

Re: How to check all elements of a list are same or different

2009-04-15 Thread John Machin
On Apr 16, 8:14 am, Chris Rebert wrote: > > On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert wrote: > > >> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe wrote: > > Thanks for the reply. But I am interested in analysing the contents of just > > one list. For example, > > > list1=[1,2,3,4,5,6] > > S

Re: Re: How to check all elements of a list are same or different

2009-04-15 Thread John Posner
Scott David Daniels wrote: Assuming you really are going for "is" comparison, how about: max(id(x) for x in mylist) == min(id(x) for x in mylist) It has the advantage that if [id(x) for x in mylist] = [2N, 1N, 3N], you get the answer you desire. Oops -- got me! -John -- http://mail.python

Re: How to check all elements of a list are same or different

2009-04-15 Thread Scott David Daniels
John Posner wrote: ...This solution relies on the object ID -- no hashability required: # get list of object-IDs ids = map(lambda x: id(x), mylist) # ALL THE SAME? ... test whether "average ID" matches "first ID" sum(ids)/len(ids) == ids[0] Assuming you really are going for "is" comparison,

Re: Re: How to check all elements of a list are same or different

2009-04-15 Thread Rhodri James
On Wed, 15 Apr 2009 23:56:45 +0100, John Posner wrote: Chris Rebert wrote: Ah, okay. Then you want: def all_same(lst): return len(set(lst)) == 1 def all_different(lst): return len(set(lst)) == len(lst) Note that these require all the elements of the list to be hashable. This solut

Re: Re: How to check all elements of a list are same or different

2009-04-15 Thread John Posner
Chris Rebert wrote: Ah, okay. Then you want: def all_same(lst): return len(set(lst)) == 1 def all_different(lst): return len(set(lst)) == len(lst) Note that these require all the elements of the list to be hashable. This solution relies on the object ID -- no hashability required:

Re: How to check all elements of a list are same or different

2009-04-15 Thread Gaurav Moghe
Thanks! That works! On Wed, Apr 15, 2009 at 6:14 PM, Chris Rebert wrote: > > On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert wrote: > >> > >> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe wrote: > >> > Hi, > >> > > >> > I am an amateur python user I wanted to know how do I know whether all > >>

Re: How to check all elements of a list are same or different

2009-04-15 Thread Chris Rebert
> On Wed, Apr 15, 2009 at 5:49 PM, Chris Rebert wrote: >> >> On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe wrote: >> > Hi, >> > >> > I am an amateur python user I wanted to know how do I know whether all >> > the >> > contents of a list are all same or all different? Now, I could certainly >> > w

Re: How to check all elements of a list are same or different

2009-04-15 Thread Gaurav Moghe
Hi Chris, Thanks for the reply. But I am interested in analysing the contents of just one list. For example, list1=[1,2,3,4,5,6] So, the logical statement would probably be: if list1==(contains all same values), print "Same">False if list1==(contains all different values), print "Differen

Re: How to check all elements of a list are same or different

2009-04-15 Thread Chris Rebert
On Wed, Apr 15, 2009 at 2:36 PM, Gaurav Moghe wrote: > Hi, > > I am an amateur python user I wanted to know how do I know whether all the > contents of a list are all same or all different? Now, I could certainly > write a loop with a counter. But is there a ready command for that? Checked > a lot

How to check all elements of a list are same or different

2009-04-15 Thread Gaurav Moghe
Hi, I am an amateur python user I wanted to know how do I know whether all the contents of a list are all same or all different? Now, I could certainly write a loop with a counter. But is there a ready command for that? Checked a lot of docs and this mailing list, but didnt get anything worthwhile