I wrote a small generator function that produces multinomial combinations.
(Python's itertools module does ordinary combinations, but not multinomial
combinations). The code essentially works, except that the the last
combination in each tuple is not enclosed in a nested tuple:
In [2]: x= multi
A few weeks ago, I wrote a class that creates an iterator for solving the
general unlabeled-balls-in-labeled boxes occupancy problem. Chris Rebert
converted my code to a generator, which made the code cleaner, and I
subsequently simplified it somewhat further.
My problem is the following: All of
Mark Dickinson-2 wrote:
>
>
> This is a well-known trick: to divide 5 (unlabeled) balls amongst 3
> (labeled) boxes, you write down sequences of 5 o's and 2 x's, where
> the o's represent the 5 balls and the 'x's represent dividers:
>
> ooxooxo -> [2, 2, 1]
> xoooxoo -> [0, 3, 2]
>
Chris,
Your code is much cleaner than mine. I will have to figure out exactly how
it is working.
Thanks!
Phillip
--
View this message in context:
http://old.nabble.com/recursive-algorithm-for-balls-in-numbered-boxes-tp32440187p32443579.html
Sent from the Python - python-list mailing list a
Hello Peter,
When I run my code, I get the same 14 configurations that your code
produces; the only different that I can see in the output is that the
configurations are produced in a different order. Note that your code is
not creating an iterator, so thus doesn't do what I want. Also, generat
I've written a recursive class that creates an iterator to solve a general
formulation of the combinatorics problem known as "balls in numbered boxes"
(also known as "indistinguishable balls in distinguishable boxes"). The
code has been extensively tested and appears to work, but isn't terribly
e
I just realized that there is a defect in my algorithm, so I will try to code
this using a recursive algorithm instead.
--
View this message in context:
http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32439439.html
Sent from the Python - python-list mailing list archive at
Very nice explanation! I've circumvented the problem by returning a
`deepcopy` of the list. I've also added an acknowledgment. The revised
code is attached.
I'd like to see both balls in numbered boxes (this code) and balls in
unnumbered (indistinguishable) boxes in Python's `itertools` module
It is supposed to be possible to generate a list representation of any
iterator that produces a sequence of finite length, but this doesn't always
work. Here's a case where it does work:
Input:
from itertools import combinations
list(combinations(range(4),2))
Output:
[(0, 1), (0, 2), (0, 3), (
The title should have been "can't generate list from iterator".
--
View this message in context:
http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32435569.html
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.org/mailman/listinfo/
Stefan Behnel-3 wrote:
>
> alexander@gmail.com wrote:
>> I think the speed function may be broken from the turtle graphics package
>>
>>
>> "from turtle import *
>>
>> speed('fastest')
>>
>> forward(50)"
>>
>>
>> I have tried all of the different speed settings, but I get no change
>>
Bruno- You've made some excellent suggestions, and I'm always grateful for
the opportunity to learn. My revised code appears below. Philllip
def strip_pairs(s, open='([{\'"', close=')]}\'"'):
"""
OVERVIEW
This function strips matching pairs of characters from the beginning and
end
I wrote a handy-dandy function (see below) called "strip_pairs" for stripping
matching pairs of characters from the beginning and end of a string. This
function works, but I would like to be able to invoke it as a string method
rather than as a function. Is this possible?
def strip_pairs(s=None
OK. I was able to reproduce the problem. My difficulty was that the command
that I issued initially was "from xyz import *" rather than just "import
xyz". If I say "import xyz", then the docstring is defined; if I say "from
xyz import *", it isn't. I'm not sure whether this is a bug or expecte
Steven D'Aprano-7 wrote:
>
> On Sun, 06 Dec 2009 10:55:50 +0100, Andreas Waldenburger wrote:
>
>> On Sat, 5 Dec 2009 23:04:42 -0800 (PST) "Dr. Phillip M. Feldman"
>> wrote:
>>
>>
>>> If I create a module xyz.py with a docstring &quo
If I create a module xyz.py with a docstring """xyz does everything you could
possibly want.""" at the top, the command ?xyz issued at the IPython prompt
does not display this docstring. What am I doing wrong?
--
View this message in context:
http://old.nabble.com/How-to-create-a-docstring-for-
I would like to put a statement on line N of my program that prints the line
number that is currently executing. This may sound fairly trivial, but I
don't want to hard code the line number because N will change if lines are
inserted or deleted above that point. Any advice will be appreciated.
--
I'm amazed that this works. I had not realized that
x,y= [3,4]
is equivalent to
x= 3; y= 4
Python is rather clever.
Thanks!
To elaborate on Paul's answer, returning the list will also unpack it if
you have it set up that way. E.g.
def func(alist):
return alist
some_list = [1, 2]
t
I currently have a function that uses a list internally but then returns the
list items as separate return
values as follows:
if len(result)==1: return result[0]
if len(result)==2: return result[0], result[1]
(and so on). Is there a cleaner way to accomplish the same thing?
--
View this messag
It looks as though what I should have done is the following:
In [23]: array(x) > 6
Out[23]: array([False, False, False, False], dtype=bool)
--
View this message in context:
http://www.nabble.com/comparison-on-list-yields-surprising-result-tp25195170p25195893.html
Sent from the Python - python-l
In [21]: x
Out[21]: [1, 2, 3, 5]
In [22]: x>6
Out[22]: True
Is this a bug?
--
View this message in context:
http://www.nabble.com/comparison-on-list-yields-surprising-result-tp25195170p25195170.html
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.
I wrote the following correct but inefficient test of primality for purposes
of demonstrating that the simplest algorithm is often not the most
efficient. But, when I try to run the following code with a value of n that
is large enough to produce a significant amount of running time, I get an
out
> Could you please clarify how you're calling it? E.g.
> reload('foo')
> or
> reload(foo)
>
> ?
>
> On Thu, 13 Aug 2009 12:05:26 -0700, Dr. Phillip M. Feldman
> wrote:
>
>> According to the Python documentation, 'reload
According to the Python documentation, 'reload' reloads a previously imported
module (so that changes made via an external editor will be effective).
But, when I try to use this command, I get the following error message:
TypeError: reload() argument must be module
Any suggestions will be appre
Is there a mechanism for submitting comments on a Python PEP?
--
View this message in context:
http://www.nabble.com/How-to-comment-on-a-Python-PEP--tp24840417p24840417.html
Sent from the Python - python-list mailing list archive at Nabble.com.
--
http://mail.python.org/mailman/listinfo/python
I am using Python 2.5, and most of the cmath functions are not yet available
in this version. Thanks!
Phillip
P.S. In your code, that should be x+= 0J
P.P.S. I wish that the documentation indicated anything that is new.
Christian Heimes-2 wrote:
>
>
>
> phase() has been added to Python 2.
When I try to compute the phase of a complex number, I get an error message:
In [3]: from cmath import *
In [4]: x=1+1J
In [5]: phase(x)
NameError: name 'phase' is not defined
AttributeError: 'complex' object has no attribute 'phase'
Any advice will be appreciated.
--
View this message in con
This was very close to what I wanted. Thanks! My final code looks like
this:
def num2str(x,f=4):
"""Convert x (int or float) to a string with f digits to right of
the decimal point. f may be zero or negative, in which case the decimal
point is suppressed."""
s= str(round(x,f))
i
I'd like to be able to convert a float to a string representation in which
the number is rounded to a specified number of digits. If num2str is a
hypothetical function that does this, then num2str(pi,3) would be '3.142'
(not '3.141'). I've been told that there is no such function in Python. I
t
In the attached http://www.nabble.com/file/p24726902/test.py test.py code,
it appears that additional statements execute after the call to sys.exit(0).
I'll be grateful if anyone can shed light on why this is happening. Below
is a copy of some sample I/O. Note that in the last case I get addit
in xs:
print x
Having to put such extra logic into practically every function is one of the
annoying things about Python.
Phillip
Diez B. Roggisch-2 wrote:
>
> Dr. Phillip M. Feldman schrieb:
>> Some aspects of the Python design are remarkably clever, while others
>> leave
>
"As far as I know, there is no programming language which treats scalars like
ints as if they were
vectors of length 1"
Actually, Matlab does:
>> length(5)
ans =
1
>>
--
View this message in context:
http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654358.html
isinstance(x, (int, float, complex))
is certainly very compact, and does what I want. Thanks!
--
View this message in context:
http://www.nabble.com/len%28%29-should-always-return-something-tp24639361p24654347.html
Sent from the Python - python-list mailing list archive at Nabble.com.
--
htt
Some aspects of the Python design are remarkably clever, while others leave
me perplexed. Here's an example of the latter: Why does len() give an error
when applied to an int or float? len() should always return something; in
particular, when applied to a scalar, it should return a value of 1. Of
Does anyone know if there is a PDF version of the Python Tutorial (URL=
http://www.python.org/doc/current/tutorial/)?
--
View this message in context:
http://www.nabble.com/PDF-version-of-Python-Tutorial--tp24543817p24543817.html
Sent from the Python - python-list mailing list archive at Nabble.
Suppose that 'xor' returns the value that is true when only one value is
true, and False otherwise. This definition of xor doesn't have the standard
associative property, that is,
(a xor b) xor c
will not necessarily equal
a xor (b xor c)
To see that this is the case, let a= 1, b= 2, and c= 3
I did initially ask for an infix xor operator, but eventually gave up on
this. I like the first of your two one-line solutions below; this is clean
and easy to understand. Thanks! I'd still like to be able to write an
expression like '(a and b) xor (c and d) xor (e and f)', but it looks as
thou
arg in args:
if bool(arg): result= not result
return result
MRAB-2 wrote:
>
> Ethan Furman wrote:
>> Robert Kern wrote:
>>> On 2009-07-14 14:56, Dr. Phillip M. Feldman wrote:
>>>
>>>> != does do what I want, except that it doesn't indicate
to see an option for type checking on
operands of logical operators, so that attempting to apply a logical
operator to non-Boolean entities generates a warning message. With operand
type checking, 'xor' and != would be different.
Mark Dickinson wrote:
>
> On Jul 14, 7:25 pm, "D
Current Boolean operators are 'and', 'or', and 'not'. It would be nice to
have an 'xor' operator as well.
--
View this message in context:
http://www.nabble.com/missing-%27xor%27-Boolean-operator-tp24485116p24485116.html
Sent from the Python - python-list mailing list archive at Nabble.com.
--
40 matches
Mail list logo