User Profile

Collapse

Profile Sidebar

Collapse
rogerlew
rogerlew
Last Activity: Nov 16 '08, 06:39 AM
Joined: Jun 6 '07
Location:
  •  
  • Time
  • Show
  • Source
Clear All
new posts

  • rogerlew
    replied to How do you find the median of a list?
    You want to find the median of a list?

    Code:
    x=[1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
    median=sorted(x)[len(x)/2]
    See more | Go to post

    Leave a comment:


  • Assuming Apache's setup, you (should) just need to:
    1. put it in your cgi-bin (or sub-directory)
    2. change the extension to .cgi,
    3. make it executable and voila!
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to list question
    Neat trick, but Python101 is looking for a method of counting items common to both lists....
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to list question
    How about this:
    [code=python]
    count=0
    for a in A:
    if a in B: count+=1
    print count
    [/code]
    or this:
    [code=python]
    count=sum([1 for a in A if a in B])
    print count
    [/code]
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to Problems opening a file in a shared dir
    It will work if you "map" the network share.
    http://www.microsoft.com/windowsxp/u.../mapdrive.mspx...
    See more | Go to post

    Leave a comment:


  • Here a "batteries included" approach using numpy and pylab

    [code=python]
    import numpy
    import pylab
    # see http://matplotlib.sour ceforge.net/

    from time import mktime
    from datetime import datetime
    # see http://pleac.sourcefor ge.net/pleac_python/datesandtimes.h tml

    def c2f(strnum) : return (float(strnum)* 5.0/9)+32.0

    # dict to convert month abbreviations...
    See more | Go to post
    Last edited by rogerlew; Dec 14 '07, 01:39 AM. Reason: fix line wrap

    Leave a comment:


  • Nothing built-in.

    Popular choices are gnuplot, and matplotlib.

    If you have matlab experience try matplotlib. The syntax and concepts are similiar.
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to manipulating string question
    And another... with re.
    It had to be done!

    Code:
    import re
    def repl(match):
        globals()['count']+=1
        return '$%i%s'%(globals()['count'],
                        match.groups()[0])
             
    globals()['count']=0 
    print re.sub('(AS)',repl,stringA)
    
    >>> $1ASD DSA D$2ASFSADSA FSAFSADSAF $3AS
    See more | Go to post

    Leave a comment:


  • Excel has excellent CSV (comma separated values) support and the Python CSV module is standard and pretty easy to use. It might also make your code easier to modify. With your code if you want to insert an entry into column 3 for example you would have to swap a lot of stuff around.

    Code:
    import csv
    fid=open('c:/fahad/new/file.xls','wb')
    writer=csv.writer(fid)
    for f, load in enumerate(loads):
        writer.writerow([
    ...
    See more | Go to post
    Last edited by rogerlew; Oct 5 '07, 09:25 PM. Reason: syntax error

    Leave a comment:


  • rogerlew
    replied to Use Python to write HTML file
    Use triple double-quoted strings
    Code:
    f.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">""")
    ...
    See more | Go to post

    Leave a comment:


  • rogerlew
    started a topic Capture Numpy errors and warnings

    Capture Numpy errors and warnings

    How can I capture Numpy errors and warnings (to a log file for instance)? I tried redirecting stdout and stderr with:
    Code:
    import sys
    sys.stdout=sys.stderr=open('log.log','wb')
    but Numpy warnings are not caught and are passed to the shell.

    I am using 2.4.4 and numpy 1.0.3.1 on a windows XP box.
    Thanks,
    Roger
    See more | Go to post
    Last edited by rogerlew; Sep 19 '07, 08:59 PM. Reason: Clarity

  • rogerlew
    replied to What is your OS and Python Version
    Kubuntu Feisty (64-bit)
    3800+, 4 GB 6400
    Python 2.5
    Kate

    XP SP2
    2.4.4, 2.5
    IDLE
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to operator.itemgetter() syntax
    The method described above works for Python 2.5 but not for 2.4.4. Operator.itemge tter only accepts one argument under 2.4.4. Another problem I found was that with 2.5 the * will only flatten a sequence. So if you want to sort by unordered a set of unordered indices (e.g. -1, 4,5,6) your out of luck (from want I tried at least).

    I ended writing a function which works with 2.4 and 2.5. I am posting it here encase someone runs into the...
    See more | Go to post

    Leave a comment:


  • rogerlew
    replied to operator.itemgetter() syntax
    I figured it out.
    Code:
    for key in sorted(M_keys,key=operator.itemgetter(*keyindices)):
    See more | Go to post

    Leave a comment:


  • rogerlew
    started a topic operator.itemgetter() syntax

    operator.itemgetter() syntax

    Hi all,

    I need to pass operator.itemge tter(item,...) multiple arguments but the number of arguments is variable.

    Code:
    ivlist=['var1','var2','var3'] # This variable changes length
    keyindices=range(-len(ivlist)-1,0) # = [-4, -3, -2, -1] in this example
    
    # What I want in this example is
    M_keys=M.keys()
    for key in sorted(M_keys,key=operator.itemgetter(-4, -3, -2, -1)):
        # for
    ...
    See more | Go to post
No activity results to display
Show More
Working...