woooee 814 Nearly a Posting Maven

A google will yield a lot of hits. The python wiki is always a good place to start. http://wiki.python.org/moin/GuiProgramming

sneekula commented: good site +8
woooee 814 Nearly a Posting Maven

The standard solution for anagrams is to sort on the letters. So 'ogd' sorted is 'dgo', and a lookup of the 'dgo' key in the dictionary yields the list ["dog", "god"]. Your code would only return whichever one of these words it found first. Also, you should not use "dict" as a variable name as it is a reserved word. It is a good idea to use 2 words for a variable name to avoid possible conflicts. "words_dict" would be a better choice for example.

vegaseat commented: very good help +12
woooee 814 Nearly a Posting Maven

Please don't spam this site or someone will report you and you will be banned. Since the OP didn't say which toolkit or OS was involved, it is obvious that there is no way to recommend anything, hence MESHIKUMAR and mohankumar554 are the same and are spamming.

vegaseat commented: thnaks for the spam fight +12
woooee 814 Nearly a Posting Maven

This was the second entry from a Google for "python list write"
http://www.daniweb.com/forums/thread173615.html

and effbot is usually in search results. This explains writelines(), but writelines() does not insert a newline but is useful for transferring lists to a file.
http://effbot.org/zone/python-list.htm

slate commented: Straihgt to the point. +1
woooee 814 Nearly a Posting Maven

If you want to print from the word to the end of the line, use index. This untested code will tell you if a word is found. You can then use .join to print out what you want.

for line in logfile:
    words_list = line.split():
    for word in KEYWORDS:
          ## or for word in words_list:
          ## use the shortest list in the for() statement
          print word
          if word in words_list:
               el_number = words_list.index( word )
               print "found", el_number, word
    print line
    #Move to next line
sneekula commented: good idea +6
woooee 814 Nearly a Posting Maven

Please put your code between code tags (the "#" icon) as no one will be able to run your program without proper indents. Generally, you use a control variable, in your case a string control variable, and the ".set " and ".get" methods to change the text or get the text. Here is a link to New Mexico Tech's page http://infohost.nmt.edu/tcc/help/pubs/tkinter/control-variables.html From that site "For example, you could link an Entry widget to a Label widget so that when the user changes the text in the entry and presses the Enter key, the label is automatically updated to show that same text." This is their example, using different widgets than you, but the idea is the same

self.spamVar = StringVar()
self.spamCB  = Checkbutton ( self, text="Spam?",
        variable=self.spamVar, onvalue="yes", offvalue="no" )

Note the "variable=" which defines the variable that contains the text/data. Write a simple example with one widget and update the text every time you click a button. Then, you should be able to do the rest.

vegaseat commented: good ref +11
woooee 814 Nearly a Posting Maven

The problem may be that you are using the reserved word "file" and perhaps overloading it. file and open are basically the same for Python. So use something other than "file", check the contents of the output file with a text/word processor to see that it is really there 3 times, and post back if the problem doesn't disappear.

sneekula commented: good catch +6
woooee 814 Nearly a Posting Maven

There's also a second part where I should define a SortedSet class

Verify with the teacher that the set will only contain integers, floats, and strings, and whether there will be more than one type of these in the set.. If you have lists mixed in with integers mixed in with whatever, sorting gets very tricky.

sneekula commented: very good point +6
woooee 814 Nearly a Posting Maven

In 1221 Genghis Khan killed 1,748,000 people at Nishapur in one hour.

That is too fantastic to believe. 1000 soldiers would have to kill 1748 people in one hour, and 10,000 would still have to kill 174.8 in one hour. And that's not even mentioning how difficult it would be to find that many people who are in hiding (unless they were really, really dumb). That number of people may have disappeared as a result of being conquered, but many probably relocated out of fear. It sounds more like a horror story spun by the conquered, or Genghis Khan himself to instill fear. If anyone less lazy than me wants to dig up anything more on this, please post it.

GrimJack commented: Good call! I agree +9
woooee 814 Nearly a Posting Maven

Generally, you would do a merge type program. Read in both files, compare and change, then write to a third file, which will be the merged file. So some pseudo-code

if "hb15b01" in record, find record in second file to change (let's say it has "hb15b00"), change "hb15b00" to "hb15b01" and write to the thiird file, then rinse and repeat.

It is not a good idea to try and replace directly into a file, because everything has to be exactly the same, and you can't guarantee that with any file. If you are replacing
2671, "hb1", " UGz9", 1,
with
"hb15b01"
a direct overlay could wind up with something like
2671, "hb15b01UGz9", 1,
because of the different lengths, which is not what you want. So you would generally use split() to isolate the field you want to change, modify it, and then use join() to convert back to a string.

Ene Uran commented: good point +6
woooee 814 Nearly a Posting Maven

A slight modification.

for index, item in enumerate(arr):
    if item > 100:
        return index, item

See here for info on lists http://effbot.org/zone/python-list.htm

Edit: Damn, this is probably homework. My mistake. Using enumerate will probably red flag it though.

woooee 814 Nearly a Posting Maven

This will only execute actionDefault once.

do_action = 0
if testA:
  actionA()
  if testB:
    actionB()
    if textC:
      actionC()
    else:
      do_action=1
  else:
    do_action=1
else:
  do_action=1
if do_action:
  actionDefault()
woooee 814 Nearly a Posting Maven

I would like to know if there is an easier way than what I just did

Would raise red flags for a beginning homework assignment like above, but you can use operator to sort on any list element

import operator

data = [
'kick me 989',                                              
'bodybody 344',
'Santa Clause 0943',
'Bart Simpson 100', 
'As Is 2856' ]

##  sort on last element
data_as_lists = [ rec.split() for rec in data ]   ## convert into list of lists
data_as_lists.sort(key=operator.itemgetter(-1))
print data_as_lists
print
for each_rec in data_as_lists :
   print each_rec

## or, using integers
for rec in data_as_lists:
   rec[-1] = int(rec[-1])
data_as_lists.sort(key=operator.itemgetter(-1))
print data_as_lists
print
for each_rec in data_as_lists :
   print each_rec
woooee 814 Nearly a Posting Maven

You don't have to use a tuple to store the guesses. A list is normally used, but you can also use a string thusly (this is not meant to be a complete solution)

guess_word = "alfalfa"
correct_guess = ""
wrong_guess = ""
previous_guesses = ""
                                                                              
## generate a list to test with instead of input
tries = [ "a", "b", "e", "a", "o" ]
for guess in tries:
   if guess in previous_guesses:
      print guess, "has already been guessed"
   elif guess in guess_word:
      print guess, "is correct"
      correct_guess += guess
   else:
      print guess, "is NOT correct"
      wrong_guess += guess
   previous_guesses += guess

Also, I prefer to use a function, and return at a breakpoint. Perhaps that will be easier to get your head around.

def guess_word(random_word):

   guess_count = 0
   correct_guess = ""
   wrong_guess = ""
   previous = ""

   while guess_count < 6: 
      guess_count+=1
      guess=raw_input("\nguess one letter ")

      if guess in previous:
         print guess, "has already been guessed"
         guess_count -= 1         ## don't charge for duplicate guesses
      elif guess in random_word:
         print guess, "is correct"
         correct_guess += guess
      else:
         print guess, "is NOT correct"
         wrong_guess += guess
      previous += guess
      ##---   while testing, check that this works correctly
      print correct_guess
      print wrong_guess

   print len(correct_guess), "correct guesses and", len(wrong_guess), "incorrect"        

   ##---  This is the return part                               
   while 1:
      print  "You have to guess the word now"
      guess_word=raw_input("What say you (hit Enter to quit) ")
      guess_word = guess_word.strip()
      if len(guess_word) == 0:   ##  Enter key only
         return
      elif guess_word.lower() == random_word:
         print …
Murtan commented: I like your solution, I was just hoping to encourage the OP to do more +1
woooee 814 Nearly a Posting Maven

Assuming that the server belongs to your ISP, you want to first ask them if Python is supported. It usually is, and they probably have some examples of the types of things they allow. Unlike using IDLE, the program file would be run from the command line or another program and how you do that would depend in part on which operating system you are using. This is the web programming page on the Python Wiki (and may be more than you want right now) http://wiki.python.org/moin/WebProgramming

vegaseat commented: thanks for the info +11
woooee 814 Nearly a Posting Maven

I wanted to know if i could get some help to start the menu.I'll do the maths part working with the iterations etc as i learnt about loops today

Post the code for the math and we will help with the menu. Of course it's not going to happen. The strategy here is to keep asking more questions until little by little the entire program is coded by someone else, or rather by many others. Please don't give positive reinforcement to lazy manipulators. It only increases their appetite.

Obviously, no one has tried Google. But that takes initiative and some work.

woooee 814 Nearly a Posting Maven

For future reference, you do not want to assume that the characters you search for are found. This is better IMHO, although I too would prefer to use .split(".")

if start == -1: 
   break
najdi = DB.find ('.', start)
if najdi > -1:
      end = DB.find ('.', najdi)
      if end > -1:
         br = DB[start:najdi:end]
woooee 814 Nearly a Posting Maven

I get no returned rows. I use the following SQL code to query:
SELECT * FROM fdi.fdiresults;

fdi.fdiresults seems odd for a table name. Try "SELECT tablename FROM pg_tables where tablename not like 'pg_%'" and see what it gives you for a table name. (Note that semicolons are not used in Python.) I am not familiar with PostGIS, but generally a select statement is more like 'cur.execute( "SELECT * FROM tablename")'.

PoovenM commented: Thank you for the tip :) +2
woooee 814 Nearly a Posting Maven

You could also replace the following:

reducedList = [n for n in listParagraph if len(n) == len(word)]
## replace with this
reduced_list = [n for n in list_paragraph if word == n.lower()]
if len(reduced_list):
    print "The word", "'" + word + "'", "occurs", len(reduced_list), "times in this section."
else:
    print "The word", "'" + word + "'", "does not occur in this section of text."
#
# anyway, here is another solution
#
paragraph = '''This is a test sentence.  We will look for hi in this sentence.
If we find 'hi', we want to keep count of it.  Remember, hi
can be Hi or hi.  Hi can also have characters before or
after it ie (hi. hi, hi:).  There should be a total of 10 'hi'
in this sentence, not any more for words like 'this' or
'hippo' or 'hiccups' '''

word='hi'
p_list = paragraph.split()
ctr=0
for p_word in p_list:
   p_word = p_word.lower()
   if p_word == word:
      ctr += 1
   elif not p_word.isalpha():     ## has non-alpha characters
      new_word = ""
      for chr in p_word:
         if chr.isalpha():
            new_word += chr
      if new_word == word:
         ctr += 1
print "The word '%s' was found %d times" % (word, ctr)
woooee 814 Nearly a Posting Maven

You can split and create new list with the numbers as the first part of the rec and then use the builtin sort method, or convert to a list of lists as below. Whichever is easier in this case.

data_as_lists = [ line.strip().split() for line in data ]     ## convert to a list of lists
data_as_lists.sort(key=operator.itemgetter(-1))  ## sort on last element
for el in data_as_lists :            ## print each item in the list
   print el
sneekula commented: nice code +4
woooee 814 Nearly a Posting Maven

Sorry, I meant slice, rec[begin:end], not rec.split(). Also, you possibly want a separate program file for 'read the dbf' and then would import it into 'read the XML' to keep it from getting too confusing as can happen when everything is in one file.

woooee 814 Nearly a Posting Maven

Append everything to c

a = [ "is", "cool"]
b = ["Peter", "James", "John"]
c = []

for name in b:
   c.append( [name, a]  )
print c

[['Peter', ['is', 'cool']], ['James', ['is', 'cool']], ['John', ['is', 'cool']]]
sneekula commented: very nice solution +3
woooee 814 Nearly a Posting Maven

Don't know if you are aware of Python's webbrowser class. It allows you to display web-based docs, etc. You can choose a particular browser or use the default browser. I don't know if it will tell you what the default browser is though.

scru commented: you've been helpful +3
woooee 814 Nearly a Posting Maven

FWIW this will find all occurrences of a word in a string. Also note that if you are looking for "the" it will give a hit for "then". If you only want the word, you have to search with spaces added, " the " -assuming string.lower() and that punctuation is removed

found=the_string.find(word)
while found > -1:
     print word, "found at location", found
     found=the_string.find(word, found+1)
woooee 814 Nearly a Posting Maven

Not very elegant, but try to do anything within a try/except?

try:
     Tkinter.Label(root).pack()
except:
     print "No window exists"

## Edit:  Modifing a well known label example
##        Uncomment the root.destroy() line
from Tkinter import *
root=Tk()

def changeLabel():
    myString.set("I'm, a-fraid we're fresh out of red Leicester, sir. ")
    ##root.destroy()
    try:
         label2=Label(root).pack()
    except:
         print "No window exists"
    
myString=StringVar()
Label(root,textvariable=myString).pack()
myString.set("Well, eh, how about a little red Leicester.")
Button(root,text='Click Me',command=changeLabel).pack()
root.mainloop()
vegaseat commented: nice idea +8
woooee 814 Nearly a Posting Maven

There is a slight flaw in ZZucker's post, but the OP should have reason this out for themselves.

sneekula commented: good eyes +3
woooee 814 Nearly a Posting Maven

Setting a path is OS dependent. In Linux you should be able to use os.system( "export NEWPATH" ) although I haven't tried it. If you want to import a module that is not in your PYTHONPATH then you can use sys.path.append(). Almost every tutorial covers this.

vegaseat commented: nice help +7
woooee 814 Nearly a Posting Maven

Let's forget about bounds checking for now. I'm talking about a different section of the program. What does "crashes" mean. What is the error message and does it actually save the data (look at the length and time stamp for the file).

Edit: I think this is the problem

def save_data(file_name,data):
   import pickle        ## should be at the beginning of the program so it imports once 
   data_file = open(file_name, 'wb')
   pickle.dump(data,data_file)
   file.close()                    ## should be data_file.close()
AJG commented: Helpful. +1
woooee 814 Nearly a Posting Maven

Or put them both in the same dictionary. There is only 100 or so-times 2

# small dictionary of chemical symbols
symbol_dic = {
'C': 'carbon',
'H': 'hydrogen',
'N': 'nitrogen',
'Li': 'lithium',
'Be': 'beryllium',
'B': 'boron'
}

keys_list=symbol_dic.keys()
for key in keys_list:
   symbol_dic[symbol_dic[key]]=key
Ene Uran commented: very nice idea +4
woooee 814 Nearly a Posting Maven

This is the second hit that I got on Google https://answers.launchpad.net/gasp/+faq/28

Racoon200 commented: This one is helpful +2
woooee 814 Nearly a Posting Maven

Use string.split() instead and split on the commas. Python has a csv module (comma separated variables), but it looks like that would be overkill here.

iamthwee commented: I agree +13
woooee 814 Nearly a Posting Maven

It's your extra credit, not mine. You'll have to come up with some code to start with or at least some ideas.