Re: simultaneous assignment

2006-05-10 Thread Boris Borcic
Steve R. Hastings wrote: You could also use a function that counts all different values in a list, reducing the list to a dictionary whose keys are the unique values from the list. I got the idea from a discussion here on comp.lang.python; I called my version of it tally(). d =

Re: simultaneous assignment

2006-05-04 Thread Dave Hansen
On Tue, 02 May 2006 18:52:48 GMT in comp.lang.python, John Salerno [EMAIL PROTECTED] wrote: [...] Yeah, after trying some crazy things, I just wrote it this way: def truth_test(seq): truth = 0 for item in seq: if item: truth += 1 if truth == 1:

Re: simultaneous assignment

2006-05-04 Thread Paul Rubin
Dave Hansen [EMAIL PROTECTED] writes: Well, if you want something minimalist, you could try def truth_test(seq): return sum(1 for item in seq if item) == 1 def truth_test(seq): return sum(map(bool, seq)) == 1 -- http://mail.python.org/mailman/listinfo/python-list

Re: simultaneous assignment

2006-05-03 Thread bruno at modulix
John Salerno wrote: Bruno Desthuilliers wrote: But my question (sorry, it may not have been clear) was more along the line of : why do you worry about identity in the given snippet ?. Actually, I kind of thought that maybe it *didn't* matter in this particular example anyway, so my

Re: simultaneous assignment

2006-05-03 Thread Christos Georgiou
On Tue, 02 May 2006 17:15:05 GMT, rumours say that John Salerno [EMAIL PROTECTED] might have written: Another thing I'm trying to do is write a function that tests to see if a list contains exactly one true item, and the rest are false (obviously this would have to be a list of boolean values,

Re: simultaneous assignment

2006-05-03 Thread John Salerno
bruno at modulix wrote: John Salerno wrote: Bruno Desthuilliers wrote: But my question (sorry, it may not have been clear) was more along the line of : why do you worry about identity in the given snippet ?. Actually, I kind of thought that maybe it *didn't* matter in this particular

Re: simultaneous assignment

2006-05-03 Thread Edward Elliott
Steve R. Hastings wrote: You could also use a function that counts all different values in a list, reducing the list to a dictionary whose keys are the unique values from the list. Wouldn't reducing to a set instead of a dict make more sense if all you want to do is count uniq elements? --

Re: simultaneous assignment

2006-05-03 Thread Steve R. Hastings
On Wed, 03 May 2006 17:51:03 +, Edward Elliott wrote: Steve R. Hastings wrote: You could also use a function that counts all different values in a list, reducing the list to a dictionary whose keys are the unique values from the list. Wouldn't reducing to a set instead of a dict make

Re: simultaneous assignment

2006-05-02 Thread John Salerno
Boris Borcic wrote: w == x False w is x True That's the opposite of what I want to happen. -- http://mail.python.org/mailman/listinfo/python-list

Re: simultaneous assignment

2006-05-02 Thread Diez B. Roggisch
John Salerno wrote: Diez B. Roggisch wrote: I can't imagine what you're actually after here, but assuming that you really need this Hard to explain because I'm still trying to figure out how to do it myself. I'm trying to solve a little puzzle using Python, even though I'm sure it's not

Re: simultaneous assignment

2006-05-02 Thread David Isaac
John Salerno [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Is there a way to assign multiple variables to the same value, but so that an identity test still evaluates to False? Make sure the value is not a singleton. Assign them one at a time. w=1000 x=1000 w==x True w is x

Re: simultaneous assignment

2006-05-02 Thread Diez B. Roggisch
Hmm, classes still scare me a little, but I should consider this. The only thing is, it is not known in advance if the propositions are true or false, you have to figure that out yourself based on the combination of the statements given. I don't know if this changes the idea behind your

Re: simultaneous assignment

2006-05-02 Thread John Salerno
bruno at modulix wrote: Now if I may ask: what is your actual problem ? Ok, since you're so curious. :) Here's a scan of the page from the puzzle book: http://johnjsalerno.com/spies.png Basically I'm reading this book to give me little things to try out in Python. There's no guarantee that

Re: simultaneous assignment

2006-05-02 Thread Steve R. Hastings
# probably not true a = 0 b = 0 a is b # always true A key point is that you never really assign a value to a variable. What you really do is bind an object reference to a name. The long, simultaneous assignment form binds the same object reference to multiple variables: a = b = c = d = 12345

Re: simultaneous assignment

2006-05-02 Thread John Salerno
Steve R. Hastings wrote: Anyway, the major point I want you to take away from all this: it doesn't matter whether the is test succeeds or fails, if all you care about is the *value* of a variable. Python reuses object references to save memory, because this doesn't affect expressions that

Re: simultaneous assignment

2006-05-02 Thread Grant Edwards
On 2006-05-02, John Salerno [EMAIL PROTECTED] wrote: Yeah, after trying some crazy things, I just wrote it this way: def truth_test(seq): truth = 0 for item in seq: if item: truth += 1 if truth == 1: return True else: return

Re: simultaneous assignment

2006-05-02 Thread Boris Borcic
Steve R. Hastings wrote: So, don't test to see if something is equal to True or False: if 0 == False: pass # never executed; False and 0 do not directly compare of course they do - ie isinstance(False,int) is True and False == 0 You *could* do this, but I don't really recommend

Re: simultaneous assignment

2006-05-02 Thread Grant Edwards
On 2006-05-02, John Salerno [EMAIL PROTECTED] wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in seq if v]) def truth_test(seq): return countTrue(seq) == 1 Gosh, so much to learn!

Re: simultaneous assignment

2006-05-02 Thread Boris Borcic
Grant Edwards wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in seq if v]) def truth_test(seq): return countTrue(seq) == 1 I'd suggest the more direct def countFalse(seq) :

Re: simultaneous assignment

2006-05-02 Thread Steve R. Hastings
On Tue, 02 May 2006 19:11:36 +, John Salerno wrote: Grant Edwards wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in seq if v]) def truth_test(seq): return countTrue(seq) == 1

Re: simultaneous assignment

2006-05-02 Thread Terry Reedy
John Salerno [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] I'm sure it's not necessary. Basically W, X, Y and Z are propositions that are either true or false, and the puzzle lists a few statements such as Exactly one of X, Y and Z is true, and I'm trying to work out a little

Re: simultaneous assignment

2006-05-02 Thread Grant Edwards
On 2006-05-02, Boris Borcic [EMAIL PROTECTED] wrote: Grant Edwards wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in seq if v]) def truth_test(seq): return countTrue(seq) == 1

Re: simultaneous assignment

2006-05-02 Thread Boris Borcic
Grant Edwards wrote: On 2006-05-02, Boris Borcic [EMAIL PROTECTED] wrote: Grant Edwards wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in seq if v]) def truth_test(seq): return

[SPOILER] Re: simultaneous assignment

2006-05-02 Thread Boris Borcic
John Salerno wrote: bruno at modulix wrote: Now if I may ask: what is your actual problem ? Ok, since you're so curious. :) Here's a scan of the page from the puzzle book: http://johnjsalerno.com/spies.png Basically I'm reading this book to give me little things to try out in

Re: simultaneous assignment

2006-05-02 Thread Serge Orlov
John Salerno wrote: bruno at modulix wrote: Now if I may ask: what is your actual problem ? Ok, since you're so curious. :) Here's a scan of the page from the puzzle book: http://johnjsalerno.com/spies.png Basically I'm reading this book to give me little things to try out in Python.

Re: simultaneous assignment

2006-05-02 Thread Edward Elliott
bruno at modulix wrote: re-phrase it according to how Python works, and you'll get the answer: Is there a way to bind multiple names to the same object, but so the identity of this object is different from the identity of this object ? Which raises an interesting parallel question: is there

Re: simultaneous assignment

2006-05-02 Thread Heiko Wundram
Am Dienstag 02 Mai 2006 22:39 schrieb Edward Elliott: Which raises an interesting parallel question: is there a way to clone an arbitrary object? Yes, check the copy module. copy.copy() does a shallow copy of the parameter, copy.deepcopy() a deep copy of the parameter. For the difference

Re: simultaneous assignment

2006-05-02 Thread Edward Elliott
Heiko Wundram wrote: Integer and string objects are immutable in Python, so why'd you want to have different IDs for an object of the same value? It's the value you're working with in a program, not the objects ID. At least it should be, if you're truly intent on working with the

Re: simultaneous assignment

2006-05-02 Thread Gary Duzan
In article [EMAIL PROTECTED], John Salerno [EMAIL PROTECTED] wrote: bruno at modulix wrote: Now if I may ask: what is your actual problem ? Ok, since you're so curious. :) Here's a scan of the page from the puzzle book: http://johnjsalerno.com/spies.png Basically I'm reading this book to

Re: simultaneous assignment

2006-05-02 Thread Grant Edwards
On 2006-05-02, Boris Borcic [EMAIL PROTECTED] wrote: Grant Edwards wrote: On 2006-05-02, Boris Borcic [EMAIL PROTECTED] wrote: Grant Edwards wrote: Python knows how to count. :) def countFalse(seq): return len([v for v in seq if not v]) def countTrue(seq): return len([v for v in

Re: simultaneous assignment

2006-05-02 Thread Bruno Desthuilliers
John Salerno a écrit : bruno at modulix wrote: Now if I may ask: what is your actual problem ? Ok, since you're so curious. :) Sorry !-) Here's a scan of the page from the puzzle book: http://johnjsalerno.com/spies.png Basically I'm reading this book to give me little things to try

Re: simultaneous assignment

2006-05-02 Thread Bruno Desthuilliers
Heiko Wundram a écrit : (snip) If you're looking for things to code in Python, I'd rather suggest you look at number theory than at logic problems. Basically, every logic problem can be solved by exhaustive search (which is always the same algorithm), whereas a number theory problem

Re: simultaneous assignment

2006-05-02 Thread John Salerno
Heiko Wundram wrote: Hummm... Isn't it easier and faster to solve this problem by hand than to code a Python program for it? I had proofs for what has to be on both papers in about 30 seconds... ;-) Yeah, I had actually already figured it out in my head fairly quickly. If you're looking

Re: simultaneous assignment

2006-05-02 Thread John Salerno
Bruno Desthuilliers wrote: But my question (sorry, it may not have been clear) was more along the line of : why do you worry about identity in the given snippet ?. Actually, I kind of thought that maybe it *didn't* matter in this particular example anyway, so my question was meant to be

Re: simultaneous assignment

2006-05-02 Thread Grant Edwards
On 2006-05-03, John Salerno [EMAIL PROTECTED] wrote: Bruno Desthuilliers wrote: But my question (sorry, it may not have been clear) was more along the line of : why do you worry about identity in the given snippet ?. Actually, I kind of thought that maybe it *didn't* matter in this

Re: simultaneous assignment

2006-05-02 Thread Steve R. Hastings
On Tue, 02 May 2006 21:20:48 +0200, Boris Borcic wrote: Steve R. Hastings wrote: So, don't test to see if something is equal to True or False: if 0 == False: pass # never executed; False and 0 do not directly compare of course they do - ie isinstance(False,int) is True and False ==

Re: simultaneous assignment

2006-05-02 Thread Mel Wilson
Roger Miller wrote: Steve R. Hastings wrote: a = 0 b = 0 a is b # always true Is this guaranteed by the Python specification, or is it an artifact of the current implementation? AFAIK it's an artifact. The performance hit it Python stopped sharing small integers could be enormous,

Re: simultaneous assignment

2006-05-02 Thread Steve R. Hastings
On Tue, 02 May 2006 12:58:14 -0700, Roger Miller wrote: Steve R. Hastings wrote: a = 0 b = 0 a is b # always true Is this guaranteed by the Python specification, or is it an artifact of the current implementation? I believe it's an artifact of the current implementation. And I only

Re: simultaneous assignment

2006-05-02 Thread Steve R. Hastings
On Tue, 02 May 2006 21:44:21 +0200, Boris Borcic wrote: note that generators have no defined length - precisely because they feed values one at a time while you need them all together to speak of a length. The second expression will raise a TypeError because of that. Er, yes. If I had