Alan Gauld | 1 Jul 01:09 2008

Re: s[len(s):len(s)] = [x] ??

"Jerry Hill" <malaclypse2 <at> gmail.com> wrote

> You can do it with slice assignment too:
>>>> a = [1, 2, 3, 4]
>>>> a[1:3] = [[7, 8]]
>>>> a
> [1, [7, 8], 4]
>
> Now, which way is better?  The answer depends on context.

> If, conceptually, you are removing two elements from a list, then
> adding a new element which is itself a list, then doing it with 
> remove
> and insert is probably best.

Even in that case I'd replace themultiple removes with a slice:

>>> L = [1,2,3,4]
>>> L[1:3] = []
>>> L.insert(1,[7,8])
>>> L
[1, [7, 8], 4]

HTH,

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 

(Continue reading)

Alan Gauld | 1 Jul 01:20 2008

Re: Is "var = None" in Python equivalent to "Set var

"John Fouhy" <john <at> fouhy.net> wrote 

> (I don't want to criticise VB programmers because I am not one.
> "Follow the local conventions" is generally always a good rule of
> programming)

I think this is a key point. I'm no VB expert but I do have
to read it from time to time.

VB programmers tend to set to null more often than Python 
programmers because their variables are not typeless.
In VB Set is explicitly used for setting variables to object 
references (as opposed to Let which is (optionally) used 
to set variables to non-object values(int, string etc). 
ie 

Set myvar = New MyObject

is like Python doing

myvar = MyObject()

and is different to 

Let X = myvalue

which in turn is usually written more simply as

X = myvalue

(Continue reading)

Alan Gauld | 1 Jul 01:26 2008

Re: Deleting specified files using a python program...help with code?]


"David" <david <at> abbottdavid.com> wrote

> dir_input = raw_input('Enter dir: ')
> win_trace = ['*.ini', '*.db']
> files_removed = 0
> for root, dirs, files in os.walk(dir_input):
>    for trace in win_trace:
>        win_trace_path = os.path.join(root, trace)
>        for filename in glob.glob(win_trace_path):
>            if os.path.exists(filename):
>                print filename
>            else:
>                print 'No files found'

Note that at this point you have printed both names 
but you have not stored a reference to them.
Thus filename contains only the last name.

You need to create a list of the valid filenames.
You could use a list compreghension like so:

files = [f for f in glob.glob(win_trace_path) if os.path.exists(f)]
print files

> confirmation = raw_input('Confirm removal: ')
> if confirmation == 'y':
>    print "removing '%s'" % filename
>    os.remove(filename)
>    files_removed += 1
(Continue reading)

David | 1 Jul 03:05 2008

Re: Deleting specified files using a python program...help with code?]

Alan Gauld wrote:
>
> "David" <david <at> abbottdavid.com> wrote
>
>> dir_input = raw_input('Enter dir: ')
>> win_trace = ['*.ini', '*.db']
>> files_removed = 0
>> for root, dirs, files in os.walk(dir_input):
>>    for trace in win_trace:
>>        win_trace_path = os.path.join(root, trace)
>>        for filename in glob.glob(win_trace_path):
>>            if os.path.exists(filename):
>>                print filename
>>            else:
>>                print 'No files found'
>
> Note that at this point you have printed both names but you have not 
> stored a reference to them.
> Thus filename contains only the last name.
>
> You need to create a list of the valid filenames.
> You could use a list compreghension like so:
>
> files = [f for f in glob.glob(win_trace_path) if os.path.exists(f)]
> print files
>
>> confirmation = raw_input('Confirm removal: ')
>> if confirmation == 'y':
>>    print "removing '%s'" % filename
>>    os.remove(filename)
(Continue reading)

Martin Walsh | 1 Jul 05:52 2008

Re: list objects are unhashable

Hi Norman,

Norman Khine wrote:
> 
>         for brain in brains:
>             x = getattr(brain, horizontal)
>             x = string.join(x, '' )
>             y = getattr(brain, vertical)
>             y = string.join(y, '' )
>             if x and y and (x, y) in table:
>                 table[(x, y)] += 1
>                 table[(x, '')] += 1
>                 table[('', y)] += 1
>                 table[('', '')] += 1

For what it's worth, string.join has been deprecated since the addition
of the join method for str and unicode types. Other deprecated string
module functions are documented here: http://docs.python.org/lib/node42.html

If I'm not mistaken, the conventional form would be:

  x = ''.join(x)

> 
> So now my list becomes a string, which is not really good for me, as
> this fails when there is more then one item.
> 
> Is there a better way to loop through this and sum up all occurrences of
> each entry ie  'airport-car-parking'

(Continue reading)

Shrutarshi Basu | 1 Jul 07:12 2008
Picon

String concatenation too slow

I'm working on a program to create Lindenmayer systems. These systems
depend on heavy string rewriting to form complex patterns.I've been
using string concatenation to read through the string, and then create
the new one based on a dictionary lookup. However it becomes very slow
once the string gets very long (several thousand characters). Part of
it is undoubtedly due to the fact that the algorithm is quadratic (i'm
trying to find a better way) but I was wondering if there might be a
faster alternative to string concatenation. Would appending to a list
of strings be faster? I'm going to be doing thousands of these
appends, so even a small boost would be helpful.
Thanks,
Basu

--

-- 
The ByteBaker :
http://www.bytebaker.com
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

wesley chun | 1 Jul 07:37 2008
Picon

Re: String concatenation too slow

> I've been
>  using string concatenation to read through the string, and then create
>  the new one based on a dictionary lookup. However it becomes very slow
>  once the string gets very long (several thousand characters). [...]
> I was wondering if there might be a
>  faster alternative to string concatenation. Would appending to a list
>  of strings be faster?

without knowing more about your application, my 1st inclination would
be to turn your code that "appends" each successive addition to the
string into a generator function.  then when you need to final
massively large string, i'd use a generator expression inside the call
to the delimiter's join() method.

for example:

def nextLsystem(...):
        :
    for n in range(XXX):
        # blah-blah stuff in a loop
        yield nextStr

final = ''.join(x for x in nextLsystem(XXX))

i like this code because it doesn't keep building up a data structure
like continuously concatenating strings nor continually appending to a
list, both of which are memory-intensive.

i'm using a generator to create each successive string, without saving
previous result necessarily.  then the generator expression -- unlike
(Continue reading)

Shrutarshi Basu | 1 Jul 07:55 2008
Picon

Re: String concatenation too slow

My bad, should have included some code. Here's the function which does
the grunt work. self.axiom is a string, where each character gets
replaced by its counterpart from self.rules. output then goes back to
the calling function. That's the end of one generation of the string.
The next generation happens when generate() is called again after
axiom has been replaced (usually by output and some additions)

    def generate(self):
        output = ''
        for element in self.axiom:
            if element in self.rules.keys():
                output = output + self.rules[element]
            else:
                output = output + element
        return output

Looking at Wesley's example. the function should then yield each
character replacement (a dict lookup) and the join() would put
everything together. I think that would be faster. Will try soon.
Thanks,
Basu

On Tue, Jul 1, 2008 at 1:37 AM, wesley chun <wescpy <at> gmail.com> wrote:
>> I've been
>>  using string concatenation to read through the string, and then create
>>  the new one based on a dictionary lookup. However it becomes very slow
>>  once the string gets very long (several thousand characters). [...]
>> I was wondering if there might be a
>>  faster alternative to string concatenation. Would appending to a list
>>  of strings be faster?
(Continue reading)

Chris Fuller | 1 Jul 08:00 2008
Picon

Re: String concatenation too slow

You could try creating a list of strings, and then using a ''.join(list) to 
concatenate, but you are probably going to be best off using the cStringIO 
module (http://docs.python.org/lib/module-cStringIO.html).  Try timing all 
three and see how they compare when joining lots of strings.  The length of 
the strings probably won't matter as much as the number of them.

Cheers

On Tuesday 01 July 2008 00:12, Shrutarshi Basu wrote:
> I'm working on a program to create Lindenmayer systems. These systems
> depend on heavy string rewriting to form complex patterns.I've been
> using string concatenation to read through the string, and then create
> the new one based on a dictionary lookup. However it becomes very slow
> once the string gets very long (several thousand characters). Part of
> it is undoubtedly due to the fact that the algorithm is quadratic (i'm
> trying to find a better way) but I was wondering if there might be a
> faster alternative to string concatenation. Would appending to a list
> of strings be faster? I'm going to be doing thousands of these
> appends, so even a small boost would be helpful.
> Thanks,
> Basu
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Andre Engels | 1 Jul 08:12 2008
Picon

Fwd: Problem Euler 26

On Sun, Jun 29, 2008 at 10:34 PM, kinuthiA muchanE <muchanek <at> gmail.com> wrote:

> Er... er, that does not exactly work as expected but it narrows the
> search to only 3 candidates because of the inclusion of the zero:
>
>  (28, '035714286')
>  (38, '026315789')
>  (81, '012345679')
>
> For 28, the digit, in the fractional part, after 8 is 5, so 5 is
> repeated and as for, 81 the next digit after 7 is 0, so again 0 occurs
> twice. But for 38, the next digit after 9 is 4, and because it has NOT
> occurred before, I assume 38 is the correct answer... and I am wrong!
>
> I suspect I have completely misunderstood the question.

Yes, it seems you have... I will try to rephrase:

For each fraction x/y (y>x to get it below 1), its representation as a
decimal fraction consists of two parts: first some arbitrary series of
numbers, then a series of numbers that is repeated over and over
again. For example, 1/55 = 0.018181818181818..., or in short 0.0(18) -
the arbitrary series here is 0, the repeating part 18. You are asked
to get the largest repeating part.

Your solution finds the longest time before a digit is repeated
instead, which is incorrect because the same digit can be used more
than once in the repeating part (and also because it might occur once
or more in the non-repeating part) - for example, the 1/38 that you
mentioned equals:
(Continue reading)


Gmane