Re: multidimensional "arrays"

2007-12-06 Thread Remco Gerlich
Hi,

DATA = [ [ 0 for i in range(ncolumns) ] for i in range(nrows) ]

Is one way.

DON'T do it like this:

row = [0] * ncolumns
data = [ row ] * nrows # WRONG!

Since after that, every row is the exact same object; if you set data[0][0]
= 1, the first element of _every_ row is 1.

But I guess you already found out about that :-)

That said, are you sure a list of lists is the best data structure for what
you're trying to do? I keep being surprised by Python code that uses other
data structures in clever ways; lists of lists seem to be pretty rare!

Remco Gerlich

On Dec 6, 2007 4:29 PM, Horacius ReX <[EMAIL PROTECTED]> wrote:

> in python, when I want to use arrays, I follow this way;
>
> DATA = [0] * nint
>
> and when I want to use I do;
>
> 
>
> DATA[i] = 
>
> do you know how to do similar but in two dimensions ?
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: what the heck does this mean?

2007-12-13 Thread Remco Gerlich
On Dec 13, 2007 4:57 AM, katie smith <[EMAIL PROTECTED]> wrote:

> Traceback (most recent call last):
>   File "C:\Python25\empire\Empire Strategy.pyw", line 322
> Maty = Searched(number)
> TypeError: 'list' object is not callable
>

This is the error message. The first line basically says "This is what
happened:"

The second shows the file, and the line number.

The third is the actual line.

Then the error message: you had a list object, and you tried to call it.

Well, looking at that line, we see Searched(number). Search is apparently a
list, and you used ( ) on it, which tries to call it, but that's not
possible.
So that's the mistake - you meant to use [ ], but used ( ).


> so Maty Searched(number is supposed to give me 0 when
> Search = "NewMap"
> number = 0
> bignum = 1
> bignumer = repr(bignum)
> Searching = Search+bignumer
> Searched = eval(Searching)
> Maty = Searched[number]
>

This isn't the actual code that gave the error message - here you used  [
].

>

Anyway, doing it like this is really bad and likely to go wrong, but that's
not your question.

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

Re: Question from a python newbie

2007-12-13 Thread Remco Gerlich
On Dec 13, 2007 4:39 PM, Russell <[EMAIL PROTECTED]> wrote:

> I've been learning Python slowly for a few months, coming from a C/C+
> +, C#, Java, PHP background.  I ran across a code fragment I'm having
> trouble wrapping my brain around.  I've searched the Language
> Reference and was not able to find any info regarding the structure of
> this code fragment:
>
> int(text) if text.isdigit() else text


Hi,

It's a pretty new construct; basically it's Python's version of the ? :
"ternary operator" in other languages.

The line above is equivalent to

text.isdigit() ? int(text) : text

in, for instance, C.

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

Re: How to get memory size/usage of python object

2008-01-10 Thread Remco Gerlich
Hi,

The only list without references to other objects in it is [ ].

0, 1, 2, etc are objects. Every value in Python is a reference to an object.

Remco

On Jan 10, 2008 9:14 AM, Santiago Romero < [EMAIL PROTECTED]> wrote:

>
> > Would you care to precisely define "REAL size" first? Consider:
> >
> > >>> atuple = (1, 2)
> > >>> mylist = [(0, 0), atuple]
> >
> > Should sizeof(mylist) include sizeof(atuple) ?
>
>  No, I'm talking about "simple" lists, without REFERENCES to another
> objects into it.
>
>  I mean:
>
> lists = [ 0, 1, 2, 3, 4, (1,2), 3]
>
>  or
>
> array = [ [0,0,0,0,0,0,0], [1,1,1,1,2,1,2], ... ]
>
>  Maybe I can "pickle" the object to disk and see the filesize ... :-?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Loop in a loop?

2008-01-17 Thread Remco Gerlich
Use zip() to combine them into a single list, then loop over that:

for x, y in zip(array1, array2):
   ...

Remco

On Jan 17, 2008 1:21 PM, Sacred Heart <[EMAIL PROTECTED]> wrote:

> Hi,
> I'm new to Python and have come across a problem I don't know how to
> solve, enter com.lang.python :)
>
> I'm writing some small apps to learn the language, and I like it a lot
> so far.
>
> My problem I've stumbled upon is that I don't know how to do what I
> want. I want to do a loop in a loop. I think.
>
> I've got two arrays with some random stuff in, like this.
>
> array1 = ['one','two','three','four']
> array2 = ['a','b','c','d']
>
> I want to loop through array1 and add elements from array2 at the end,
> so it looks like this:
>
> one a
> two b
> three c
> four c
>
> I'm stuck. I know how to loop through the arrays separatly and print
> them, but both at the same time? Hmmm.
>
> A push in the right direction, anyone?
>
> R,
> SH
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Excess whitespace in my soup

2008-01-19 Thread Remco Gerlich
Not sure if this is sufficient for what you need, but how about

import re
re.sub(u'[\s\xa0]+', ' ', s)

That should replace all occurances of 1 or more whitespace or \xa0
characters, by a single space.

Remco

On Jan 19, 2008 12:38 PM, John Machin <[EMAIL PROTECTED]> wrote:

> I'm trying to recover the original data from some HTML written by a
> well-known application.
>
> Here are three original data items, in Python repr() format, with
> spaces changed to tildes for clarity:
>
> u'Saturday,~19~January~2008'
> u'Line1\nLine2\nLine3'
> u'foonly~frabjous\xa0farnarklingliness'
>
> Here is the HTML, with spaces changed to tildes, angle brackets
> changed to square brackets,
> omitting \r\n from the end of each line, and stripping a large number
> of attributes from the [td] tags.
>
> ~~[td]Saturday,~19
> ~~January~2008[/td]
> ~~[td]Line1[br]
> Line2[br]
> Line3[/td]
> ~~[td]foonly
> ~~frabjous farnarklingliness[/td]
>
> Here are the results of feeding it to ElementSoup:
>
> >>> import ElementSoup as ES
> >>> elem = ES.parse('ws_soup1.htm')
> >>> from pprint import pprint as pp
> >>> pp([(e.tag, e.text, e.tail) for e in elem.getiterator()])
> [snip]
>  (u'td', u'Saturday, 19\n  January 2008', u'\n'),
>  (u'td', u'Line1', u'\n'),
>  (u'br', None, u'\nLine2'),
>  (u'br', None, u'\nLine3'),
>  (u'td', u'foonly\n  frabjous\xa0farnarklingliness', u'\n')]
>
> I'm happy enough with reassembling the second item. The problem is in
> reliably and
> correctly collapsing the whitespace in each of the above five
> elements. The standard Python
> idiom of u' '.join(text.split()) won't work because the text is
> Unicode and u'\xa0' is whitespace
> and would be converted to a space.
>
> Should whitespace collapsing be done earlier? Note that BeautifulSoup
> leaves it as   -- ES does the conversion to \xa0 ...
>
> Does anyone know of an html_collapse_whitespace() for Python? Am I
> missing something obvious?
>
> Thanks in advance,
> John
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How avoid both a newline and a space between 2 print commands?

2008-01-23 Thread Remco Gerlich
Hi,

Use

import sys
sys.stdout.write("foo")
sys.stdout.write("bar")

(and, possibly, sys.stdout.flush() to get the text to show up, it might wait
for the end of a complete line otherwise).

The alternative

import sys
print "foo",
sys.stdout.softspace=0
print "bar"

is just too hackish.

In Python 3, this will be improved!

Remco

On Jan 23, 2008 3:03 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]>
wrote:

> print "foo"
> print "bar"
>
> has a newline in between "foo" and "bar"
>
> print "foo",
> print "bar"
>
> has a space in between "foo" and "bar"
>
> How prevent ANYTHING from going in between "foo" and "bar" ??
>
> (Without defining a string variable.)
>
> Chris
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: validate string is valid maths

2008-01-28 Thread Remco Gerlich
Hi,

It seems that for every group of 2 or more +-/* signs, one of the following
holds:

- The group starts with '-', everything after it should be dropped,
otherwise
- The second character is '-', everything after it should be dropped,
otherwise
- Drop everything after the first.

That should turn into one short regex. Did I miss something?

Remco

On Jan 28, 2008 4:10 PM, <[EMAIL PROTECTED]> wrote:

> Hi pythoners.
>
> I am generating strings of length n, randomly from the symbols
>
> +-/*0123456789
>
> What would be the 'sensible' way of transforming the string, for example
> changing '3++8' into 3+8
> or '3++--*-9' into '3+-9' such that  eval(string) will always return a
> number?
>
> in cases where multiple symbols conflict in meaning (as '3++--*-9' the
> earliest valid symbols in the sequence should be preserved
>
> so for example,
>
> '3++*/-9' = 3+-9
> '45--/**/+7'  = 45-+7
> '55/-**+-6**' = 55/-6
>
> ...that is, unless there is a canonical method for doing this that does
> something else instead..
>
>
> this sounds like homework. It's not. I like making problems up and it's a
> slow work day. So, as an aside, there is no real reason I want to do this,
> nor other problem to solve, nor other background to what I'm trying to
> achieve ;) other than twiddling with python.
>
> Matt.
>
>
>
> This message and any attachments (the "message") is
> intended solely for the addressees and is confidential.
> If you receive this message in error, please delete it and
> immediately notify the sender. Any use not in accord with
> its purpose, any dissemination or disclosure, either whole
> or partial, is prohibited except formal approval. The internet
> can not guarantee the integrity of this message.
> BNP PARIBAS (and its subsidiaries) shall (will) not
> therefore be liable for the message if modified.
> Do not print this message unless it is necessary,
> consider the environment.
>
>-
>
> Ce message et toutes les pieces jointes (ci-apres le
> "message") sont etablis a l'intention exclusive de ses
> destinataires et sont confidentiels. Si vous recevez ce
> message par erreur, merci de le detruire et d'en avertir
> immediatement l'expediteur. Toute utilisation de ce
> message non conforme a sa destination, toute diffusion
> ou toute publication, totale ou partielle, est interdite, sauf
> autorisation expresse. L'internet ne permettant pas
> d'assurer l'integrite de ce message, BNP PARIBAS (et ses
> filiales) decline(nt) toute responsabilite au titre de ce
> message, dans l'hypothese ou il aurait ete modifie.
> N'imprimez ce message que si necessaire,
> pensez a l'environnement.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list