Viewing 8 posts - 1 through 8 (of 8 total)
  • Anybody know anything about python on here?
  • RealMan
    Free Member

    Just started using it, any useful tips? Using the IDLE thing at the moment.

    Seems horrible compared to other things I’ve used, like vb or matlab. Currently trying to write a program that reads a text file then displays how many words contain n letters, for n from 1-max. Simple, but can’t get it to work, and it just crashed and I lost all I had.

    So one of the things I like about other programs I use is pressing up to get the last command you entered, is there a similar thing on python?

    Also how do you find the size of a list? Cheers.

    sas
    Free Member

    I’ve dabbled in it, if you’re coming from a VB/Matlab background then it might be a bit of a jump but should be a useful skill to have. Have you tried working through the tutorial?
    http://docs.python.org/tutorial/
    I haven’t tried IDLE, but ipython is fairly similar to the Matlab command prompt.

    List length:
    : a=[1,4]
    : len(a)
    : 2

    poly
    Free Member

    Python is lovely compared to many programming languages. I don’t tend to use the command line interpreter and prefer to write in programs as per conventional programs. Its definitely nicer than VB. I’ve never used Matlab properly in anger but once you are attuned to Python it should be easy.

    If you’ve got the bones of a prog, then feel free to post it and I’ll see if I can comment on where it might be going wonky. If you’ve not got anything remotely working then tell me roughly how big the file is, and if we can make any assumptions on maximum word length. Also whats the file “structure”?

    Fresh Goods Friday 696: The Middling Edition

    Fresh Goods Friday 696: The Middlin...
    Latest Singletrack Videos
    poly
    Free Member

    Oh and in Idle (at least in the version I am using – you an configure it) you can access the “command history” using Alt+p and Alt+n for previous and next lines.

    chvck
    Free Member

    Another big python fan here! I also, I don’t use IDLE, I prefer to use a standard editor. If I’m doing a bit project then I use Komodo otherwise I use Geany.

    http://www.developer.com/lang/other/article.php/625901

    I’ve used part of that tutorial and it seems to be quite good. If you’re playing with files then have a look at the “with” command as it does a chunk of the work for you!

    To get the size of a list just use: len(listName)

    poly
    Free Member

    i’m avoiding real work, so here is a simple program which will work IF each word is on a separate line in the file.

    If this is not your structure you’ll need to do a bit more work, but python is incredibly friendly for handling files so it should be simple.

    maxlength=0
    f=open(‘wordlist.txt’) ## opens wordlist.csv to read and assigns to f
    wordlist = f.readlines() ## reads entire file into word list
    ## (warning may be a problem with BIG files)
    ## note multiple words on a single line treated as one long word

    no_of_words=len(wordlist)
    print “Total words read = “,no_of_words

    for each in wordlist:
    ^ a=len(each) -1
    ^ if a>maxlength: maxlength=a

    resultlist=[0]
    for val in range(0,maxlength): resultlist.append(0) ## build list of size required

    print “Longest word is = “,maxlength

    for each in wordlist:
    ^ a=len(each)-1 ## subtract 1 as word length is over counted
    ^ resultlist[a] = resultlist[a] +1

    print “== Results: ==”
    for result in range(1,maxlength+1):
    ^ print “Words with”,result,”characters:”,resultlist[result]

    sorry – can’t get code formatting to work – lines starting ^ should be indented (delete the ^)

    RealMan
    Free Member

    Thanks all, very helpful. However I tried to run that program and it just didn’t do anything, no errors, no output, nothing..?

    Ugh, really don’t like python. Hate how everything has to be indented correctly, and how there’s no end statements, I really don’t get how to end an if or for.

    poly
    Free Member

    if’s and for’s are indented so to end the loop your return the indent to the level it was at when the if statement was. Not sure why that code won’t work for you. Copy and paste it into a text file, wherever there is a ^ replace it with say 4 spaces (to indent it). Save the file as something like wordcount.py then at the command prompt type:

    python wordcount.py

    and you will get either some error message or the output you expect. Obviously you will need the relevant txt file containing your words to get sensible output. My email is in my profile so feel free to get in touch directly if that doesn’t help. If you send me your email address I can ping you working files.

    The code above is not very clever (I could make it much simpler in terms of lines of code – but I was trying to keep the logic fairly simple).

    I’m probably otherwise engaged for the rest of today / this eve, but might be able to look at it for you tomorrow.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Anybody know anything about python on here?’ is closed to new replies.