How is the logical processing being done for strings like 'Dog' and 'Cat'

2008-10-20 Thread Sumitava Mukherjee
Hi all,
I am a novice programmer in Python.
Please could you explain me the results (regarding logical operators).

I get this:

>>> print bool('God' and 'Devil')
True

[This is ok because (any) string is True, so; (True and True) gives
True]



>>> print('God' and 'Devil')
Devil

[This is what I don't get ]
and for that matter,I also checked out this:

>>> 01 and 10
10


What is python doing when we type in ('God' and 'Devil') or key in (01
and 10) ?

--
http://mail.python.org/mailman/listinfo/python-list


How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Sumitava Mukherjee
Hi all,
I need to randomly sample from a list where all choices have weights
attached to them. The probability of them being choosen is dependent
on the weights.
If say Sample list of choices are [A,B,C,D,E] and weights of the same
are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
want the likeliness of them being chosen be in the order : D>A>C>E>B

In short I mean if prob of a H is .9 and probability of T be 0.1 then
if I draw 10 samples, 9 should be H and 1 should be T.

I coudn't find a function in the module random that does so.
Please can someone guide me how the above could be implemented [either
through some function which exists and I don't know or pointers to
some code snippets which does so]?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Sumitava Mukherjee
On May 26, 11:39 pm, Sumitava Mukherjee  wrote:
> Hi all,
> I need to randomly sample from a list where all choices have weights
> attached to them. The probability of them being choosen is dependent
> on the weights.
> If say Sample list of choices are [A,B,C,D,E] and weights of the same
> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
> want the likeliness of them being chosen be in the order : D>A>C>E>B
>
> In short I mean if prob of a H is .9 and probability of T be 0.1 then
> if I draw 10 samples, 9 should be H and 1 should be T.
>
> I coudn't find a function in the module random that does so.
> Please can someone guide me how the above could be implemented [either
> through some function which exists and I don't know or pointers to
> some code snippets which does so]?

>>>> [Oh, I forgot to mention. I am looking for sampling without replacement.]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-31 Thread Sumitava Mukherjee
On May 28, 3:52 pm, Antoon Pardon  wrote:
> Op 2009-05-26, Arnaud Delobelle schreef :
>
>
>
> > Sumitava Mukherjee  writes:
>
> >> On May 26, 11:39 pm, Sumitava Mukherjee  wrote:
> >>> Hi all,
> >>> I need to randomly sample from a list where all choices have weights
> >>> attached to them. The probability of them being choosen is dependent
> >>> on the weights.
> >>> If say Sample list of choices are [A,B,C,D,E] and weights of the same
> >>> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
> >>> want the likeliness of them being chosen be in the order : D>A>C>E>B
>
> > You mean A > D > C > E > B
>
> >>> In short I mean if prob of a H is .9 and probability of T be 0.1 then
> >>> if I draw 10 samples, 9 should be H and 1 should be T.
>
> >>> I coudn't find a function in the module random that does so.
> >>> Please can someone guide me how the above could be implemented [either
> >>> through some function which exists and I don't know or pointers to
> >>> some code snippets which does so]?
>
> >>>>>> [Oh, I forgot to mention. I am looking for sampling without 
> >>>>>> replacement.]
>
> > If you do sampling without replacement, you need to know the exact
> > number of each of A, B, C, D, E in the sample, not just their relative
> > frequency.
>
> As far as I understand, you are given the exact number of each. It is one.
> The numbers given are not relative frequencies of appearance but weights
> to be attributed for picking them.
>
> --
> Antoon Pardon

Yes, the numbers are weights attributed for picking them.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-31 Thread Sumitava Mukherjee
On May 27, 8:08 pm, Scott David Daniels  wrote:
> Sumitava Mukherjee wrote:
> > I need to randomly sample from a list where all choices have weights
> > attached to them. The probability of them being choosen is dependent
> > on the weights.
>
> I am not sure why everybody is normalizing by dividing the weights.
> This isa possibility (I had fun writing it).
>
> def sample_without_replacement(choices, weights):
>      '''Yield elements sampled w/o replacement by weighting'''
>      if len(weights) != len(choices):
>          raise ValueError('%d choices, but %d weights?' % (
>              len(choices), len(weights)))
>      if min(weights) < 0:
>          raise ValueError('Negative weights?: %s' % (
>                      [(i, w) for i, w in enumerate(weights) if w < 0]))
>
>      # look at only non-zero probabilities
>      combined = [(w, v) for w, v in zip(weights, choices) if w > 0]
>
>      # Go from highest probability down to reduce expected traversal
>      combined.sort(key=operator.itemgetter(0), reverse=True)
>
>      total = sum(w for w, v in combined) # sum(weights) also works
>      while combined:
>          spot = sample = random.random() * total
>          for n, (weight, choice) in enumerate(combined):
>              spot -= weight
>              if spot <= 0:
>                  break
>          else:
>              # n, (weight, choice) = 0, combined[0] # Highest probability
>              raise ValueError('%f left after choosing %f/%f?: %s' % (
>                                  spot, sample, total, combined))
>          yield choice
>          total -= weight
>          if weight > total * 256: # arbitrary choice for recalculation
>              # precision affected, rebuild
>              total = sum(w for w, v in combined)
>          del combined[n]
>      raise ValueError('Samplng more than %d without replacement?' % (
>                        sum(1 for w in weights if w > 0)))
>
> for n in range(10):
>      gen = sample_without_replacement('abcdef', [32,16,8,4,2,1])
>      print gen.next(), gen.next(), gen.next()
>
> --Scott David Daniels
> scott.dani...@acm.org

Among all the help (all of which I really appreciate), I found your
solution closest to what I was expecting. Thanks a lot Scott.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Installation and Running on Windows Vista

2008-09-09 Thread Sumitava Mukherjee
On Sep 9, 3:46 pm, Mchizi_Crazy <[EMAIL PROTECTED]> wrote:
> Please help with issue... I heard of compatimbiltity issues and would
> like clarification.

The win32 installer available from python.org works absolutely fine
with Vista. So, go ahead and try it out. It works for all of my
friends and myself
--
http://mail.python.org/mailman/listinfo/python-list