recursive function

2007-01-08 Thread cesco
Hi,

I have a dictionary of lists of tuples like in the following example:
dict = {1: [(3, 4), (5, 8)],
2: [(5, 4), (21, 3), (19, 2)],
3: [(16, 1), (0, 2), (1, 2), (3, 4)]]

In this case I have three lists inside the dict but this number is
known only at runtime. I have to write a function that considers all
the possible combinations of tuples belonging to the different lists
and return a list of tuples of tuples for which the sum of the first
element of the most inner tuple is equal to N.

For example, assuming N = 24, in this case it should return:
[((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8), (19,
2), (0, 2))]

A simple list comprehension would be enough if only I knew the number
of keys/lists beforehand but this is not the case. I guess I need a
recursive function. Can anyone help?

Thanks in advance
Francesco

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


Re: recursive function

2007-01-08 Thread cesco

Neil Cerutti wrote:
> On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I have a dictionary of lists of tuples like in the following example:
> > dict = {1: [(3, 4), (5, 8)],
> > 2: [(5, 4), (21, 3), (19, 2)],
> > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
> >
> > In this case I have three lists inside the dict but this number
> > is known only at runtime. I have to write a function that
> > considers all the possible combinations of tuples belonging to
> > the different lists and return a list of tuples of tuples for
> > which the sum of the first element of the most inner tuple is
> > equal to N.
> >
> > For example, assuming N = 24, in this case it should return:
> > [((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8), (19,
> > 2), (0, 2))]
>
> What do you mean by "most inner tuple"?
>
> > A simple list comprehension would be enough if only I knew the
> > number of keys/lists beforehand
>
> len(dict.keys()).

What I mean is that the number of keys/lists is not known until runtime
and it changes randomly from time to time which means I would have to
write every time a different list comprehension (or a different number
of nested loops) to accomplish the same thing.
In the example the result of the search should be a list containing
three tuples each of which contains again three tuple (the most inner
tuples). If you consider the first element of each tuple the sum is 24
(like in 3+5+16, or 3+21+0 or 5+19+0).

Any other help will be appreciated

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


Re: recursive function

2007-01-09 Thread cesco

Hendrik van Rooyen wrote:
> "cesco" <[EMAIL PROTECTED]> wrote:
>
> >
> > Neil Cerutti wrote:
> > > On 2007-01-08, cesco <[EMAIL PROTECTED]> wrote:
> > > > Hi,
> > > >
> > > > I have a dictionary of lists of tuples like in the following example:
> > > > dict = {1: [(3, 4), (5, 8)],
> > > > 2: [(5, 4), (21, 3), (19, 2)],
> > > > 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
> > > >
> > > > In this case I have three lists inside the dict but this number
> > > > is known only at runtime. I have to write a function that
> > > > considers all the possible combinations of tuples belonging to
> > > > the different lists and return a list of tuples of tuples for
> > > > which the sum of the first element of the most inner tuple is
> > > > equal to N.
> > > >
> > > > For example, assuming N = 24, in this case it should return:
> > > > [((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8), (19,
> > > > 2), (0, 2))]
> > >
> > > What do you mean by "most inner tuple"?
> > >
> > > > A simple list comprehension would be enough if only I knew the
> > > > number of keys/lists beforehand
> > >
> > > len(dict.keys()).
> >
> > What I mean is that the number of keys/lists is not known until runtime
> > and it changes randomly from time to time which means I would have to
> > write every time a different list comprehension (or a different number
> > of nested loops) to accomplish the same thing.
> > In the example the result of the search should be a list containing
> > three tuples each of which contains again three tuple (the most inner
> > tuples). If you consider the first element of each tuple the sum is 24
> > (like in 3+5+16, or 3+21+0 or 5+19+0).
> >
> > Any other help will be appreciated
>
>
> Is there any reliable structure in the data?
> - for instance in your example, the first list
> has two tuples, the second one three, and the
> third one four - Is this a pattern?

Hi Hendrik,

there is no such a pattern (I just happened to insert that ascending
number of lists). Anyway the answer from [EMAIL PROTECTED] was
quite satisfactory:-)

regards
Cesco

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


escaping only double quotes

2007-08-31 Thread cesco
Hi,

I have a string which is constructed like this:

string = "Butler's 15\" TV"
s = """my string goes here "%s" """ % string

the single string, unfortunately, gets escaped which is something I
want to avoid because I have to load the data into a database using
the json format and I get an "invalid escape error" if the single
quote is escaped.

Is there a way to accomplish this?

Thanks and regards
Francesco

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


number generator

2007-03-09 Thread cesco
I have to generate a list of N random numbers (integer) whose sum is
equal to M. If, for example, I have to generate 5 random numbers whose
sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
simple pattern or function in Python to accomplish that?

Thanks and regards
Francesco

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


Re: number generator

2007-03-09 Thread cesco
On Mar 9, 3:51 pm, Paul Rubin <http://[EMAIL PROTECTED]> wrote:
> "cesco" <[EMAIL PROTECTED]> writes:
> > I have to generate a list of N random numbers (integer) whose sum is
> > equal to M. If, for example, I have to generate 5 random numbers whose
> > sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
> > simple pattern or function in Python to accomplish that?
>
> Erm, yes, lots of ways, there are probably further constraints on the
> problem, such as the size of the integers, how the lists are supposed
> to be distributed, etc.  Can you be more specific?  Is this for an
> application?  If it's a homework problem, that's fine, but it's better
> in that case for respondents to suggest hints rather than full solutions.

Given two positive integers, N and M with N < M, I have to generate N
positive integers such that sum(N)=M. No more constraints.

Thanks again
Francesco

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


Re: number generator

2007-03-14 Thread cesco
> Since people are posting their solutions now (originally only hints
> were provided for the homework problem), here's mine:

Well, I never really said it was a homework problem. Sure there are
more implications to my question that I had first imagined and I'm
really happy I could get so many insights but the question did
originate from a real problem I was facing in my research.

Thanks to everyone for the wonderful thread so far
Francesco

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


Optimization problem

2007-03-18 Thread cesco
I have a set of N blocks of different lengths. The length of each
block is a multiple of a basic unit. The blocks, once lined up, make a
path of distance equal to R. Let's say we have 5 blocks with the
following lengths: N_set_lengths = (1, 3, 2, 1, 3), then the path we
cover by lining them up is equal to R = 10.
Now, each of these blocks is associated with a different metric m
(metric) which depends on the location/position that the block
occupies within the path. So a block  of length 1 will have R = 10
different m values, one for each of the 10 positions it can occupy
within the path, while a block of length 3 will have R-3+1 = 8
different m values.

Here is a graphical representation:
 

block 0:  |m0,0|m0,1|m0,2|m0,3|m0,4|m0,5|m0,6|m0,7|m0,8|m0,9|
(length 1, 10 different metrics)
 

 
---
block 1:  |m1,0|m1,1|m1,2|m1,3|m1,4|m1,5|m1,6|m1,7|   |
|  (length 3, 8 different metrics, each metric
 
---
refers to 3 consecutive units)
 
---
block 2:  |m2,0|m2,1|m2,2|m2,3|m2,4|m2,5|m2,6|m2,7|m2,8|   |
(length 2, 9 different metrics, each
 
---
referring to 2 consecutive units)
 
---
block 3:  |m3,0|m3,1|m3,2|m3,3|m3,4|m3,5|m3,6|m3,7|m3,8|m3,9|
(length 1, 10 different metrics)
 
---
 
---
block 4:  |m4,0|m4,1|m4,2|m4,3|m4,4|m4,5|m4,6|m4,7|   |
|  (length 3, 8 different metrics)
 
---

Those blocks can be allocated in fact(N) possible different ways to
cover the path (In the example considered I have 5 possible blocks to
choose from to cover the first part of the path, 4 blocks to cover the
second part of the path, and so on).

Each of these possible combinations results in a different overall
metric which we can define as the sum of the individual metrics that
each block gets according to the position occupied within the path.
There is at least one combination which is the optimum solution,
because it maximizes the overall metric. Finding such a combination is
possible but it may require a long processing time.

If, for example, the number of blocks is 20 the number of possible
combinations is expressed as 2.4*10^18. In the application I'm
considering the time is really a constraint so I'm trying to find an
algorithm which would give a near optimum solution but with a much
lower complexity.

Does anyone have suggestion on how should such an algorithm behave
(possibly considering implementation issues)?

Sorry for the lengthy description, I was just trying to be as clear as
possible.
Please, don't hesitate to  ask questions if the problem statement is
not clear.

Many thanks and regards
Francesco

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


generating list of sub lists

2007-09-16 Thread cesco
Hi,

is there a one-liner to accomplish the following task?
>From the list
l = ['string1', 'string2', 'string3']
generate the list of lists
l = [['string1'], ['string1', 'string2'], ['string1', 'string2',
'string3']]

Any help would be appreciated.

Thanks
Francesco

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


generate list of partially accumulated values

2007-09-16 Thread cesco
Hi,

I have the following list:
l = [1, 2, 3, 4]
and I'd like to obtain a list like the following:
l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])

Is there a simple way to accomplish this?

I came up with the following solution but it seems a bit too
elaborated for the task:
l_partial_sum = [reduce(lambda x,y:x+y, sub_l) for sub_l in [l[:i+1]
for i in range(len(l))]]

Any help will be appreciated.

Thanks
Francesco

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


Re: generate list of partially accumulated values

2007-09-16 Thread cesco
> l = [1, 2, 3, 4]
> [sum(l[:x+1]) for x in xrange(len(l))]

Thanks,

actually my problem is a bit more complex (I thought I could get a
solution by posting a simplified version...)

The list is composed of objects:
l = [obj1, obj2, obj3, obj4]
and I need to call a method (say method1) on each object as follow:
l1 = [obj1.method1(obj2), obj2.method1(obj3), obj3.method1(obj4),
obj4]

Is there a clean way of doing this?

Thanks again
Francesco

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


help on dictionary

2007-11-01 Thread cesco
Hi,

I have a defaultdict which I have initialized as follows:

def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model

def_dict = train(large_set_of_words)

where large_set_of_words is the list of possible strings.

I'd like to use such a default_dict as follows:

return max(list_of_strings, key=lambda w: dic[w])

that is, among the strings in list_of_strings, I'd like to return the
one with the highest score in def_dict.

The problem with such approach is that if the list_of_strings contains
a string that is not part of the def_dict, such a string gets added to
it while I'd like to keep the original def_dict unchanged/frozen.

Do you have any suggestion on how to do that?

Thanks and regards
Francesco

P.S. the code above is part from Norvig's spelling corrector

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


matplotlib and numpy installation

2006-05-27 Thread cesco
Hi,

I wanted to install python, numpy and matplotlib on Linux Ubuntu.
I installed python with the following commands
./configure --enable-unicode=ucs4
make
make install
and then I installed numpy running
python setup.py install
Finally I came to matplotlib, that for the installation requires the
commands
python setup.py build
python setup.py install
but when I run the first (python setup.py build) I got the following
output that I copied partially below. What am I doing wrong? Can anyone
help?
I had previously another version of python located in usr/bin while the
current is in usr/local/bin. I guess I could remove the previous by the
deleting the relative folder and now if I run the command 'which
python' it actually says 'usr/local/bin/python' so I hope I done it
correctly. If not, how can I uninstall any previous version of python?
Do you think that somehow the error I get is caused by an unclean
removal of that previous version?

Thanks and regards
Francesco

Here follows the output:
---

GTK requires pygtk
TKAgg requires TkInter
GTKAgg requires pygtk
running build
running build_py
running build_ext
building 'matplotlib.backends._ns_backend_agg' extension
gcc options: '-pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall
-Wstrict-prot\
otypes -fPIC'
compile options:
'-I/usr/local/lib/python2.4/site-packages/numpy/core/include -\
I/usr/include -I. -Isrc -Iswig -Iagg23/include -I. -I/usr/include -I.
-I/usr/lo\
cal/lib/python2.4/site-packages/numpy/core/include/freetype2
-I/usr/include/fre\
etype2 -I./freetype2 -Isrc/freetype2 -Iswig/freetype2
-Iagg23/include/freetype2\
 -I./freetype2 -I/usr/include/freetype2 -I./freetype2
-I/usr/local/include/pyth\
on2.4 -c'
extra options: '-DSCIPY=1'
gcc: src/_image.cpp
src/_image.cpp:5:17: png.h: No such file or directory
In file included from /usr/local/include/python2.4/Python.h:8,
 from src/_image.cpp:7:
/usr/local/include/python2.4/pyconfig.h:835:1: warning:
"_POSIX_C_SOURCE" redef\
ined
In file included from
/usr/include/c++/3.3/i486-linux/bits/os_defines.h:39,
 from
/usr/include/c++/3.3/i486-linux/bits/c++config.h:35,
 from /usr/include/c++/3.3/iostream:44,
 from src/_image.cpp:1:
/usr/include/features.h:131:1: warning: this is the location of the
previous de\
finition
src/_image.cpp: In member function `Py::Object Image::write_png(const
   Py::Tuple&)':
src/_image.cpp:626: error: `png_structp' undeclared (first use this
function)
src/_image.cpp:626: error: (Each undeclared identifier is reported only
once
   for each function it appears in.)
src/_image.cpp:626: error: parse error before `;' token
src/_image.cpp:627: error: `png_infop' undeclared (first use this
function)
src/_image.cpp:628: error: aggregate `png_color_8_struct sig_bit' has
   incomplete type and cannot be defined
src/_image.cpp:629: error: `png_uint_32' undeclared (first use this
function)
src/_image.cpp:629: error: parse error before `=' token
src/_image.cpp:632: error: `png_bytep' undeclared (first use this
function)
src/_image.cpp:632: error: `row_pointers' undeclared (first use this
function)
src/_image.cpp:632: error: parse error before `[' token
src/_image.cpp:634: error: `row' undeclared (first use this function)
src/_image.cpp:645: error: `png_ptr' undeclared (first use this
function)
src/_image.cpp:645: error: `PNG_LIBPNG_VER_STRING' undeclared (first
use this
   function)
src/_image.cpp:645: error: `png_create_write_struct' undeclared (first
use this\

   function)
src/_image.cpp:653: error: `info_ptr' undeclared (first use this
function)
src/_image.cpp:653: error: `png_create_info_struct' undeclared (first
use this
   function)
src/_image.cpp:657: error: `png_destroy_write_struct' undeclared (first
use
   this function)
src/_image.cpp:662: error: `setjmp' undeclared (first use this
function)
src/_image.cpp:670: error: `png_init_io' undeclared (first use this
function)
src/_image.cpp:673: error: `PNG_COLOR_TYPE_RGB_ALPHA' undeclared (first
use
   this function)
src/_image.cpp:673: error: `PNG_INTERLACE_NONE' undeclared (first use
this
   function)
src/_image.cpp:674: error: `PNG_COMPRESSION_TYPE_BASE' undeclared
(first use
   this function)
...

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


alternating string replace

2008-01-09 Thread cesco
Hi,

say I have a string like the following:
s1 = 'hi_cat_bye_dog'
and I want to replace the even '_' with ':' and the odd '_' with ','
so that I get a new string like the following:
s2 = 'hi:cat,bye:dog'
Is there a common recipe to accomplish that? I can't come up with any
solution...

Thanks in advance
Cesco
-- 
http://mail.python.org/mailman/listinfo/python-list


reading a specific column from file

2008-01-11 Thread cesco
Hi,

I have a file containing four columns of data separated by tabs (\t)
and I'd like to read a specific column from it (say the third). Is
there any simple way to do this in Python?

I've found quite interesting the linecache module but unfortunately
that is (to my knowledge) only working on lines, not columns.

Any suggestion?

Thanks and regards
Francesco
-- 
http://mail.python.org/mailman/listinfo/python-list


shorten path to files

2008-06-27 Thread cesco
Hi,

I need to retrieve the content of some files which are placed on a
network drive so in order to open them I need the full path to the
file.
Unfortunately some times the path is longer than 256 characters and in
Windows such a path is too long with the result that the file is not
found (though it is there).

Is there any way around this?

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


__init__.py file

2008-04-08 Thread cesco
Hi,

I need to instantiate an object (my_object) whose methods I have to
use in two files (file1.py and file2.py) which are in the same
directory. Is it possible to instantiate such object in the
__init__.py file and then directly use it in file1.py and file2.py?
If not, as I seem to experience, what is the best practice to follow
in this case? (I thought __init__.py was somehow useful for that).

Many thanks
Francesco
-- 
http://mail.python.org/mailman/listinfo/python-list


reading float from binary data file

2006-03-08 Thread cesco
Hi,

I have a binary file containing 1000 floating point numbers. I want to
load that file into an array. A way to do it could be the following:

>>> import array
>>> data = array.array('f')
>>> f = open('FileName.bin', 'rb')
>>> data.fromfile(f, 1000)

Now I have the following problem: if I don't know how many values the
file contains and I want to read all the values till the last one what
shall I do?

Thanks & regards
Francesco

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


installing numpy

2006-03-15 Thread cesco
Hi,

I'm trying to install the numpy library (precisely
numpy-0.9.6-py2.4-linux-i686) on Linux but I encounter several
problems.
After unpacking the file it creates the following folders:
usr/lib/python2.4/site-packages/numpy/
The file setup.py and the whole library is located under the folder
numpy.
If, from the location where I unpacked the file, I run the command
python usr/lib/python2.4/site-packages/numpy/setup.py install
I receive the following error:
Traceback (most recent call last):
  File "usr/lib/python2.4/site-packages/numpy/setup.py", line 24, in ?
sys.path.remove(os.getcwd())
ValueError: list.remove(x): x not in list
If, instead, I do
cd usr/lib/python2.4/site-packages/numpy/
and then
python setup.py install
I receive the following error
Traceback (most recent call last):
  File "setup.py", line 26, in ?
from numpy.distutils.core import setup
ImportError: No module named numpy.distutils.core
Finally I tried to move the folder numpy to the location where all the
third-party modules are installed, that in my case is
/usr/lib/python2.4/site-packages/ and run the command from there like:
python numpy/setup.py install
but, once again, I receive the error:
Traceback (most recent call last):
  File "numpy/setup.py", line 26, in ?
from numpy.distutils.core import setup
ImportError: No module named numpy.distutils.core

Does anyone know how this module should be installed?

Thanks and regards
Cesco

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