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 = tally
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
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:
>
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 di
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?
--
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
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 valu
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
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 actua
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. An
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 b
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 an
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
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
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 loo
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 pro
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 thi
Am Dienstag 02 Mai 2006 22:47 schrieb Gary Duzan:
> >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 this puzzle is even conducive to (or
> >worthy
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(
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 th
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 (pseudo-
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 be
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
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
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
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
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? My understanding has been that an
implementation is free to share integer objects or not, so using 'is'
as an equality test takes yo
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 coun
"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 l
Steve R. Hastings wrote:
> We could also use "generator expressions", available only in
> Python 2.4 and newer.
>
> A list comprehension always builds a list. A generator expression can
> return values one at a time.
>
> [v for v in seq if v] # builds a list and returns it
> (v for v in seq if
Steve R. Hastings wrote:
> len([v for v in seq if v]) # builds a list, then feeds it to len()
> len(v for v in seq if v) # gen obj feeds values to len one at a time
note that generators have no defined length - precisely because they feed
values
one at a time while you need them all together
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
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
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
>
> Go
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 recom
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
Gosh, so much to learn! Of course, I already know
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:
>
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 th
r uncommon
(larger) values, Python usually won't. So:
a = 12345
b = 12345
a is b # 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
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 t
John Salerno wrote:
> Boris Borcic wrote:
>
>> >>> w == x
>> False
>> >>> w is x
>> True
>> >>>
>
>
> That's the opposite of what I want to happen.
And that wouldn't be really helpful anyway !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) fo
John Salerno wrote:
> Is there a way to assign multiple variables to the same value,
> but so
> that an identity test still evaluates to False?
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
> 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 P
"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
Tr
Diez B. Roggisch wrote:
> Then use a Proposition-class that holds the actual value.
>
>> 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, I guess)
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
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 necessary. Basically W,
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
John Salerno wrote:
> Is there a way to assign multiple variables to the same value, but so
> that an identity test still evaluates to False?
>
> e.g.:
>
> >>> w = x = y = z = False
> >>> w
> False
> >>> x
> False
> >>> w == x
> True
> >>> w is x
> True # not sure if this is harmful
John Salerno wrote:
> Is there a way to assign multiple variables to the same value, but so
> that an identity test still evaluates to False?
>
> e.g.:
>
> >>> w = x = y = z = False
> >>> w
> False
> >>> x
> False
> >>> w == x
> True
> >>> w is x
> True # not sure if this is harmful
Is there a way to assign multiple variables to the same value, but so
that an identity test still evaluates to False?
e.g.:
>>> w = x = y = z = False
>>> w
False
>>> x
False
>>> w == x
True
>>> w is x
True # not sure if this is harmful
The first line above is the only way I know to
51 matches
Mail list logo