From lysdexia at crackrabbit.com Fri Oct 1 01:32:05 2004 From: lysdexia at crackrabbit.com (Douglas N. Shawhan) Date: Fri Oct 1 01:32:56 2004 Subject: [Tutor] Is there a prettier way to do this? Message-ID: <415C9775.7040306@crackrabbit.com> I am trying to sort through a list of items, find items that contain the same string fragments and report yea or nay The way I am doing it below (setting a value of 1 and multiplying it by 0 if any of the iterations come up negative) seems to be the easiest for my skull, but I have found over and over that there is usually a more pythonic way of doing things, no matter what I come up with! :-) -------------------------snip------------------------------- import string DB = {"hi":"one, two", "ho":"two, three", "he":"three, one"} search_str='three, one' search_list=string.split(search_str, ",") items = DB.keys() items.sort() search_group=[] for each in items: search_group.append(DB[each]) for each in search_group: count = 1 for item in search_list: if string.find(each, item) == -1: count = count * 0 else: count = count * 1 if count == 0: print 'not found' else: print each From kent_johnson at skillsoft.com Fri Oct 1 02:35:01 2004 From: kent_johnson at skillsoft.com (Kent Johnson) Date: Fri Oct 1 02:35:07 2004 Subject: [Tutor] Is there a prettier way to do this? In-Reply-To: <415C9775.7040306@crackrabbit.com> References: <415C9775.7040306@crackrabbit.com> Message-ID: <6.1.0.6.0.20040930203315.02a47ce0@mail4.skillsoft.com> Here's my take on it, with comments in-line. Kent # You don't need the string module, use the string methods instead DB = {"hi":"one, two", "ho":"two, three", "he":"three, one"} search_str='three, one' search_list=search_str.split(",") # search_list is now ['three', ' one'] # Note the space before 'one' - this may not be what you want. # You could split on ', ' if you always have the space, or use a regex split, or # omit the space in the original data... # Personally I like to get the key, value pairs if I am going to need both # Is there a reason why you are using a dict? for this snippet a list of pairs would # make more sense items = DB.items() items.sort() # A list comprehension makes this very concise search_group = [value for key, value in items] # In Python 2.4 you will be able to do this in one line: # search_group = [value for key, value in DB.items().sorted()] # Usually a loop with a break is a better way to code something like this for each in search_group: for item in search_list: if item not in each: # Since Python 2.3 you can do a find like this # At this point we know we're not going to find them all so we can stop print 'not found' break else: # This will only be run if the loop exits without a break print each At 06:32 PM 9/30/2004 -0500, Douglas N. Shawhan wrote: >I am trying to >sort through a list of items, >find items that contain the same string fragments and >report yea or nay > >The way I am doing it below (setting a value of 1 and multiplying it by 0 >if any of the iterations come up negative) seems to be the easiest for my >skull, but I have found over and over that there is usually a more >pythonic way of doing things, no matter what I come up with! :-) > >-------------------------snip------------------------------- >import string > >DB = {"hi":"one, two", "ho":"two, three", "he":"three, one"} > >search_str='three, one' >search_list=string.split(search_str, ",") > >items = DB.keys() >items.sort() >search_group=[] > >for each in items: > search_group.append(DB[each]) > >for each in search_group: > count = 1 > for item in search_list: > if string.find(each, item) == -1: > count = count * 0 > else: > count = count * 1 > if count == 0: > print 'not found' > else: > print each > >_______________________________________________ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor From lysdexia at crackrabbit.com Fri Oct 1 05:53:39 2004 From: lysdexia at crackrabbit.com (Douglas N. Shawhan) Date: Fri Oct 1 05:54:33 2004 Subject: [Tutor] Is there a prettier way to do this? In-Reply-To: <6.1.0.6.0.20040930203315.02a47ce0@mail4.skillsoft.com> References: <415C9775.7040306@crackrabbit.com> <6.1.0.6.0.20040930203315.02a47ce0@mail4.skillsoft.com> Message-ID: <415CD4C3.2050009@crackrabbit.com> Kent Johnson wrote: > > # You don't need the string module, use the string methods instead > > search_list=search_str.split(",") Yes! Very good. > > # search_list is now ['three', ' one'] > # Note the space before 'one' - this may not be what you want. > # You could split on ', ' if you always have the space, or use a regex > split, or > # omit the space in the original data... Good advice! > > # Personally I like to get the key, value pairs if I am going to need > both > # Is there a reason why you are using a dict? for this snippet a list > of pairs would > # make more sense > > items = DB.items() > items.sort() Yes, this makes more sense and removes a step. The problem is: I think I was unclear stating what the loop is supposed to do! I need to find *more than one* string fragment per string. Say I need to verify the existence of the sequences "joe", "bill" and "leon" in a string - but the next time I may need only find "joe" and "bill", which is why I went with the 0, 1 scheme. It may be better stated as: "How do I find arbitrary numbers of substrings in arbitrary order in a string pythonically?" From s4046441 at student.uq.edu.au Fri Oct 1 07:38:49 2004 From: s4046441 at student.uq.edu.au (Ms Soo Chong) Date: Fri Oct 1 07:38:57 2004 Subject: [Tutor] How can I code to get more than ONE value from checkbox? Message-ID: <6ac8f66b26df.6b26df6ac8f6@uq.edu.au> Hi, I'm trying to code a python script (with HTML) to get values from a html form that consists of about 10 checkbox and a textbox where user have to key in a value to perform a search. >From Lee Harr and Danny, I learned that I have to use the following method: ### qtype = data.getfirst('qtype') ---- checkbox name qtext = data.getfirst('qtext', '') ---- textbox name ### but I don't really know how to do it. Should I use qtype = data.getlist('qtype') to get the value from the checkbox and how to? Currently, my method required me to type in every possible combination of the checkbox and I know that this is wrong. I wanna to include %(qtype) in my query statement so that the script will look for the value that contain qtype everytime a form is processs but the way I'm doing now is wrong. query = "select %(qtype)s from shot_descriptions where shot_number=%(qtext)s", qtype, qtext Thus, can someone help me on this. Thank you for any help. Shufen -------------- next part -------------- #!/usr/bin/env python #Created on: 30/09/04 #Help from Lee Harr and Danny Yoo - Python Tutor import sys, os import cgi import cgitb; cgitb.enable() import pg def form(): print """
""" print "Content-Type: text/html\n\n" print "Example of pulling the list of dictionary results apart.
""" for record in listOfResults: print "key: | ', k, ' | ' print 'value: | ', record[k], ' | ' print '