[Tutor] Equivalent of Set in PtO
I have a code snippet that I have used to count the duplicates in a list as such: from sets import Set def countDups(duplicateList): uniqueSet = Set(item for item in duplicateList) return[(item, duplicateList.count(item)) for item in uniqueSet] lst = ['word', 'word', 'new', 'new', 'new'] print countDups(lst) The result is: [('new', 3), ('word', 2)], which is what is expected. This was using python version 2.7. I want to do the same thing in Python 3.1, but I'm not sure what has replaced Set in the newer version, can someone give me an idea here? Becky ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unbound Method Error
Greg Nielsen wrote: > if crash: > for BlueCarSprite in crash: > redracersprites26.BlueCarSprite.collide(crash, playerCar) > for GreenCarSprite in crash: > redracersprites26.GreenCarSprite.collide(crash, playerCar) > for TruckSprite in crash: > redracersprites26.TruckSprite.collide(crash, playerCar) >if crash: >for car in crash: > if isinstance(car, redracersprites26.BlueCarSprite()): > redracersprites26.BlueCarSprite.collide(car, playerCar) That's too little of your code to be sure, but I have a hunch that you can avoid the if-crash test and all if-isinstance(...) tests and just write for car in crash: car.collide(playerCar) You rarely need to invoke an unbound method outside of a subclass method. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] after(), how do I use it?
"michael scott" wrote Now I understand what I misunderstood. Well he imported Tkinter as tk, so I thought if it belonged to Tkinter it would be tk.after(), but the after was attached to the app, so it was in actuality app.after() . app inherits from the tk class (taking with it all its methods), so its a built in function of tkinter. To be pedantic its a method of tkinter since its part of a class definition. For the distinction between a function and a method see yesterday's thread "Unbound Method Error" One pragmatic difference between them is that help on functions is located in module level documentation, help on methods is located in class level documentation. So recognising a method call helps in directing the search for help! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Equivalent of Set in PtO
"Becky Mcquilling" wrote from sets import Set def countDups(duplicateList): uniqueSet = Set(item for item in duplicateList) return[(item, duplicateList.count(item)) for item in uniqueSet] Can be abbreviated to: def countDups(duplicateList): return [(item, duplicateList.count(item)) for item in set(duplicateList)] was using python version 2.7. I want to do the same thing in Python 3.1, but I'm not sure what has replaced Set in the newer version, can someone give me an idea here? set() It has become a native type in v3. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Equivalent of Set in PtO
Becky Mcquilling wrote: I have a code snippet that I have used to count the duplicates in a list as such: from sets import Set Since Python 2.4, you no longer need to import module "sets" (note plural) to get Set (note capital letter). You can just use the built-in name "set" (note lower-case letter). def countDups(duplicateList): uniqueSet = Set(item for item in duplicateList) return[(item, duplicateList.count(item)) for item in uniqueSet] This becomes: uniqueSet = set(item for item in duplicateList) Another advantage is that the built-in set is much faster than sets.Set. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Equivalent of Set in PtO
Becky Mcquilling wrote: > I have a code snippet that I have used to count the duplicates in a list > as such: > > from sets import Set > > def countDups(duplicateList): > uniqueSet = Set(item for item in duplicateList) > return[(item, duplicateList.count(item)) for item in uniqueSet] > > > lst = ['word', 'word', 'new', 'new', 'new'] > print countDups(lst) > > The result is: [('new', 3), ('word', 2)], which is what is expected. This > was using python version 2.7. I want to do the same thing in Python 3.1, > but I'm not sure what has replaced Set in the newer version, can someone > give me an idea here? Note that your countDups() function has to iterate len(set(duplicateList))+1 times over the duplicateList, once to build the set and then implicitly in the count() method for every item in the set. If you use a dictionary instead you can find the word frequencies in a single pass: >>> lst = ['word', 'word', 'new', 'new', 'new'] >>> freq = {} >>> for item in lst: ... freq[item] = freq.get(item, 0) + 1 ... >>> freq.items() dict_items([('new', 3), ('word', 2)]) There is also a ready-to-use class that implements this efficient approach: >>> from collections import Counter >>> Counter(lst).items() dict_items([('new', 3), ('word', 2)]) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] voluntary work :p:
you need solve the problem and so far the solution of the problem is the name of the next url example: .html , you need get the result and change in the url bar On Tue, Apr 26, 2011 at 12:19 AM, bob gailer wrote: > On 4/25/2011 11:59 PM, Wolf Halton wrote: >> >> I didn't get anything out of pythonchallenge. > > Nothing? No web pages? > >> All seems static. > > Now you say All instead of nothing. Did you get more than 1 web page? > >> Do you need flash or something to make the magic happen? > > What are you expecting? What magic? > > Did you see an image of a monitor with 2 to the 38 on it? > > Did you see "Hint: try to change the URL address."? > > Did you try that? > > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Deleting strings from a line
Hello, I've written a script that scans a biological database and extracts some information. A sample of output from my script is as follows: LYLGILLSHAN AA3R_SHEEP26331 LYMGILLSHAN AA3R_HUMAN26431 MCLGILLSHANAA3R_RAT26631 LLVGILLSHAN AA3R_RABIT26531 The leftmost strings are the ones I want to keep, while I would like to get rid of the ones to the right (AA3R_SHEEP, 263 61) which are just indicators of where the sequence came from and genomic coordinates. Is there any way to do this with a string processing command? The loop which builds my list goes like this: for line in query_lines: if line.startswith('fd;'): # find motif sequences #print "Found an FD for your query!", line.rstrip().lstrip('fd;') print line.lstrip('fd;') motif.append(line.rstrip().lstrip('fd;')) Is there a del command I can use to preserve only the actual sequences themselves. Many thanks in advance! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Deleting strings from a line
Greetings, : I've written a script that scans a biological database and extracts some : information. A sample of output from my script is as follows: : : LYLGILLSHAN AA3R_SHEEP26331 : : LYMGILLSHAN AA3R_HUMAN26431 : : MCLGILLSHANAA3R_RAT26631 : : LLVGILLSHAN AA3R_RABIT26531 : : The leftmost strings are the ones I want to keep, while I would : like to get rid of the ones to the right (AA3R_SHEEP, 263 61) : which are just indicators of where the sequence came from and : genomic coordinates. Is there any way to do this with a string : processing command? The loop which builds my list goes like this: Yes, of course. I would suggest a casual walk through: http://docs.python.org/library/stdtypes.html#typesseq This should give you some ideas of the sorts of things you can do with strings (and other similar such types). I think what you are looking for is the split() method. : for line in query_lines: : if line.startswith('fd;'): # find motif sequences : #print "Found an FD for your query!", line.rstrip().lstrip('fd;') : print line.lstrip('fd;') : motif.append(line.rstrip().lstrip('fd;')) : : Is there a del command I can use to preserve only the actual sequences : themselves. Many thanks in advance! I see (though it's commented out) that you want to get the result of line.rstrip().lstrip('fd;') several times. Rather than calculate this several times, why not store that in a variable. Additionally, then you can perform other tasks on the intermediate result. Anyway, try out the following: line.rstrip().lstrip('fd;').split()[0] Which, if I were writing in code, I would do, like this: # -- strip off motif sequences (whatever the heck they are) # line = line.rstrip().lstrip('fd;') # -- break the data into individual, well, units # parts = line.split() # -- print out the sequence # print parts[0] # -- And, here, I still have the various other bits, so I can #store them in case I want to perform some other sort of #calculations... # my_dict[ parts[0] ] = tuple( parts[1:] ) So, in short, the answer is yes. I think you would benefit from looking at what sort of string methods there are and what they do. The strip() method is one of the first to learn. The split() should be your second. Keep on going, and you'll find all sorts of goodies in there. And, enjoy Python! -Martin -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Deleting strings from a line
Spyros Charonis wrote: > Hello, > > I've written a script that scans a biological database and extracts some > information. A sample of output from my script is as follows: > > LYLGILLSHAN AA3R_SHEEP26331 > > LYMGILLSHAN AA3R_HUMAN26431 > > MCLGILLSHANAA3R_RAT26631 > > LLVGILLSHAN AA3R_RABIT26531 > > The leftmost strings are the ones I want to keep, while I would like to > get rid of the ones to the right (AA3R_SHEEP, 263 61) which are just > indicators of where the sequence came from and genomic coordinates. Is > there any way to do this with a string processing command? The loop which > builds my list goes like this: > > for line in query_lines: > if line.startswith('fd;'): # find motif sequences > #print "Found an FD for your query!", > line.rstrip().lstrip('fd;') > print line.lstrip('fd;') > motif.append(line.rstrip().lstrip('fd;')) > > Is there a del command I can use to preserve only the actual sequences > themselves. Many thanks in advance! You don't have to delete; instead extract the piece you are interested in: with open("prints41_1.kdat") as instream: for line in instream: if line.startswith("fd;"): print line.split()[1] To see what the last line does, lets perform it in two steps >>> line = 'fd; RVNIENPSRADSYNPRAG A1YQH4_ORYSJ310 310\n' >>> parts = line.split() >>> parts ['fd;', 'RVNIENPSRADSYNPRAG', 'A1YQH4_ORYSJ', '310', '310'] >>> wanted = parts[1] >>> wanted 'RVNIENPSRADSYNPRAG' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Deleting strings from a line
Spyros Charonis wrote: Hello, I've written a script that scans a biological database and extracts some information. A sample of output from my script is as follows: LYLGILLSHAN AA3R_SHEEP26331 LYMGILLSHAN AA3R_HUMAN26431 MCLGILLSHANAA3R_RAT26631 LLVGILLSHAN AA3R_RABIT26531 The leftmost strings are the ones I want to keep, while I would like to get rid of the ones to the right (AA3R_SHEEP, 263 61) Split each line in multiple words, keeping only the first: line = "LYLGILLSHAN AA3R_SHEEP26331" # split on any whitespace, a maximum of 1 time head, tail = line.split(None, 1) head will be "LYLGILLSHAN" and tail will be "AA3R_SHEEP26331". Or, if the text is fixed-width, you can use string slice to extract the characters you care about: head = line[0:11] -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] voluntary work :p:
On 26 April 2011 04:59, Wolf Halton wrote: > I didn't get anything out of pythonchallenge. All seems static. Do you > need flash or something to make the magic happen? > > To add to what the others have said and explain the "flow" a bit more: Each challenge is a puzzle, with hints present in various places, either in the displayed image and/or text, sometimes in the page source, sometimes hidden inside an image or a file that you must download etc, and so on. The general goal for solving each puzzle is to find the next puzzle's solution URL and the next puzzle URL by modifying the page URL of the current puzzle based on your detective work on the current puzzle. In several instances (especially in the beginning) it's actually possible to solve a puzzle without using Python, but obviously the idea is really to solve or perform whatever calculations are required using Python, and thereby in the process learn something about Python as you go. Now, the very first puzzle presents you with a number, 2^38, and a hint "Try to change the URL address". Now, with a bit of research you'll find that Python's way to expressing exponentiation is with the ** operator. Thus, one might as a first try, attempt changing the URL to simply that, e.g.: http://www.pythonchallenge.com/pc/def/2**38.html Attempting that, takes you to a page that says: "give the answer, not the question." ... Hmmm OK. So that should lead you to the conclusion that you need to actually calculate the answer to 2^38, and then try that in the URL instead... Now after a bit of reading you should find out about the interactive aspects of the Python interpreter and that you can use it as a calculator, and that you can calculate the answer by simply typing 2**38 into the Python interpreter. Doing all that then teaches you something about the Python interpreter, and the fact that Python copes easily with very large numbers, which in many other languages would be problematic. And so it goes. You'll quickly get to places where you e.g. have to interact with the website quite a number of times to figure out what the next puzzle URL should be, and thereby be pushed to learn about urllib and Python's wonderful web modules in order to automate the interaction, places where you have to do some relatively involved (if you tried to do the same in other languages) text processing in order to get the answer and so on. Every puzzle pushes you to learn about a specific aspect of Python, and impresses upon you (especially if you have any prior programming experience) just how elegant and powerful Python really is. As an aside, The Python Challenge was one of the things that originally impressed on me just how good and useful a language Python really is, and how wide the variety of contexts are where it can be usefully applied. Best regards, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Gtk - relational data display and edit
Hi, I am reading in data from a csv file which will be in a format like this User1, attrib1, attrib2 User2, attrib1, attrib2 Etc. And need to display this in a window. My initial thought was to use gtk.Entry widgets because I will need to edit this data, but I don't think this will work as I will also need to read the data back, in relation to the user column, e.g I will have something like [Psuedocode] For I in entries: A = usercolumn B = attrib1column C = attrib2column Somefunction(A,B,C) [/psuedocode] I don't think I could use this method with entry boxes as I have no idea how many entries there will be (column length will be fixed) so I can't create the entry widgets beforehand Anyone have any suggestions? Thanks, Bodsda Sent from my BlackBerry® wireless device ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Gtk - relational data display and edit
On 26 April 2011 16:34, wrote: > Hi, > > I am reading in data from a csv file which will be in a format like this > > User1, attrib1, attrib2 > User2, attrib1, attrib2 > Etc. > Why would the data be in this format? Are you defining it? Reason I ask is that, relationally speaking, (e.g. database design-wise) this is a denormalised representation and you'd do better to store a single attribute per record. E.g. have entities, User, Attrib, and have relationship table User_Attrib that contains only user, attrib pairs. But, maybe such ideas are overkill for your app. > And need to display this in a window. My initial thought was to use > gtk.Entry widgets because I will need to edit this data, but I don't think > this will work as I will also need to read the data back, in relation to the > user column, e.g I will have something like > > [Psuedocode] > For I in entries: >A = usercolumn >B = attrib1column >C = attrib2column > >Somefunction(A,B,C) > [/psuedocode] > > I don't think I could use this method with entry boxes as I have no idea > how many entries there will be (column length will be fixed) so I can't > create the entry widgets beforehand > > Anyone have any suggestions? > Well if you can count the number of entries on reading the file (whatever the shape of the file) then in principle you can dynamically create the correct number of entry boxes on the fly. Of course, if you store one attribe per record, then counting the number of attributes (records) for a given user becomes quite easy of course. But even if you don't do this, and you have the file structure you described, you can code reader code that would internally store the attributes and the number of attributes for each user, enabling you to write code to create the UI with the appropriate number of entry widgets. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Gtk - relational data display and edit
On 26-04-11 17:34, bod...@googlemail.com wrote: Hi, I am reading in data from a csv file which will be in a format like this User1, attrib1, attrib2 User2, attrib1, attrib2 Etc. And need to display this in a window. My initial thought was to use gtk.Entry widgets because I will need to edit this data, but I don't think this will work as I will also need to read the data back, in relation to the user column, e.g I will have something like [Psuedocode] For I in entries: A = usercolumn B = attrib1column C = attrib2column Somefunction(A,B,C) [/psuedocode] I don't think I could use this method with entry boxes as I have no idea how many entries there will be (column length will be fixed) so I can't create the entry widgets beforehand Use a gtk.Treeview for displaying this kind of data. You can also have editable cells inside a treeview so the user can change the data. Cheers, Timo Anyone have any suggestions? Thanks, Bodsda Sent from my BlackBerry® wireless device ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Gtk - relational data display and edit
Am 26.04.2011 17:34, schrieb bod...@googlemail.com: Hi, I am reading in data from a csv file which will be in a format like this User1, attrib1, attrib2 User2, attrib1, attrib2 Etc. And need to display this in a window. My initial thought was to use gtk.Entry widgets because I will need to edit this data, but I don't think this will work as I will also need to read the data back, in relation to the user column, e.g I will have something like [Psuedocode] For I in entries: A = usercolumn B = attrib1column C = attrib2column Somefunction(A,B,C) [/psuedocode] I don't think I could use this method with entry boxes as I have no idea how many entries there will be (column length will be fixed) so I can't create the entry widgets beforehand You could use a table widget. I'm sure GTK has something like that (I'm using Qt). Otherwise, creating a "dynamic" widget for one set of your data is not as hard as it might sound. You need to keeping references to the composing widgets, for example in a dict. Here's some PyQt Pseudocode: # You can retrieve the dictionary and the fieldnames with help of the # csv module att_name_to_value = {"User": "User1", "Att1": "attrib1", "Att1": "attrib1"} fieldnames = ["User", "Att1", "Att2"] att_name_to_line_edit = {} # This is Qt specific layout = QtGui.QGridLayout() for row, fieldname in enumerate(fieldnames): label = QtGui.QLabel(fieldname) line_edit = QtGui.QLineEdit(att_name_to_value[fieldname] att_name_to_line_edit[fieldname] = line_edit # add the label to the first col layout.addWidget(label, row, 0) # add the line_edit to the second col layout.addWidget(line_edit, row, 1) my_main_widget = QtGui.QWidget() my_main_widget.setLayout(layout) Now, if you need to read the changed data from the widget (triggered by a button_clicked event or what ever) you can call a function to read the data like this: def read_data_from_widget(att_name_to_line_edit): for att_name, line_edit in att_name_to_line_edit.items(): # do the conversion, e.g. PyQt new_text = str(line_edit.text()) # do whatever you wish with the new data ... HTH, Jan Anyone have any suggestions? Thanks, Bodsda Sent from my BlackBerry® wireless device ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Gtk - relational data display and edit
Thanks for your reply. Unfortunately I can't change the data format because it is output from a closed source app. I can't figure out a way to create the entry widgets on the fly because they need to be bound to a variable to attach them to a gtk.Table and to be able to read the data from them Bodsda Sent from my BlackBerry® wireless device -Original Message- From: Walter Prins Date: Tue, 26 Apr 2011 16:55:36 To: Cc: Subject: Re: [Tutor] Gtk - relational data display and edit On 26 April 2011 16:34, wrote: > Hi, > > I am reading in data from a csv file which will be in a format like this > > User1, attrib1, attrib2 > User2, attrib1, attrib2 > Etc. > Why would the data be in this format? Are you defining it? Reason I ask is that, relationally speaking, (e.g. database design-wise) this is a denormalised representation and you'd do better to store a single attribute per record. E.g. have entities, User, Attrib, and have relationship table User_Attrib that contains only user, attrib pairs. But, maybe such ideas are overkill for your app. > And need to display this in a window. My initial thought was to use > gtk.Entry widgets because I will need to edit this data, but I don't think > this will work as I will also need to read the data back, in relation to the > user column, e.g I will have something like > > [Psuedocode] > For I in entries: >A = usercolumn >B = attrib1column >C = attrib2column > >Somefunction(A,B,C) > [/psuedocode] > > I don't think I could use this method with entry boxes as I have no idea > how many entries there will be (column length will be fixed) so I can't > create the entry widgets beforehand > > Anyone have any suggestions? > Well if you can count the number of entries on reading the file (whatever the shape of the file) then in principle you can dynamically create the correct number of entry boxes on the fly. Of course, if you store one attribe per record, then counting the number of attributes (records) for a given user becomes quite easy of course. But even if you don't do this, and you have the file structure you described, you can code reader code that would internally store the attributes and the number of attributes for each user, enabling you to write code to create the UI with the appropriate number of entry widgets. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor