Re: [Tutor] Tutor Digest, Vol 25, Issue 54
On Wednesday 22 March 2006 14:14, [EMAIL PROTECTED] wrote: > Hi Sophon, > > You may want to look at: > > http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > > Many of the tutorials there talk about reading from files, including Alan > Gauld's "How to Program": > > http://www.freenetpages.co.uk/hp/alan.gauld/tutfiles.htm > > How are you learning how to program? Is there a particular tutorial that > you're reading from? i am reading from a python document in SuSE 10.1. Phon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi
It would be "ercal". The first number n denotes the position where we start. The first position is always zero. So 3 would be the letter at the 4th position which is e. The second number is I guess what confused you. m denotes where we end, counted the same way but by having 1 for the first position. 8 in your case would then be l (8th actual position). Try this mystring[3:4] to understand the working -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kaushal Shriyan Sent: Wednesday, March 22, 2006 12:45 PM To: tutor@python.org Cc: Danny Yoo Subject: Re: [Tutor] Hi On 3/22/06, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > I am new to python and I am going through the URL > > http://www.ibiblio.org/obp/thinkCSpy/index.htm, At the moment I am going > > through the Chapter 7 Strings, I am stuck with understanding slice which > > is a part of a String, I am not able to understand how it functions > > Hello, > > > A "slice" takes a string and returns a "substring" of that string. For > example, if we have a string like: > > ## > >>> mystring = "supercalifragilisticexpialidocious" > ## > > then we can select different portions of the string by indicating the > start and end positions. For example, the first ten characters of > mystring can be extracted using a slice: > > ## > >>> mystring[0:10] > 'supercalif' > ## > > > Is this what you are confused about? > > Please give us more details on where you're getting stuck, and we will do > what we can to help clarify. > > > Please continue to reply to '[EMAIL PROTECTED]': do not email me > directly. Although I may not personally have the time to answer, I'm sure > at least one of the other helpers on the list do, so by continuing the > conversation on python-help, we'll be able to guarantee that your > questions are not lost. > > Thanks Danny Yoo I got this, Lets say if its mystring[n:m] where n may be another number and m may be another number so how will it work in that case, so this becomes general and makes more clear Lets say if its mystring[3:8] so how will i evaluate it Thanks for all the help Regards Kaushal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 25, Issue 54
On Wednesday 22 March 2006 14:14, [EMAIL PROTECTED] wrote: > Hi Sophon, > > Secondary question: why are you trying to do this? Are you trying to > represent a collection or "array" of things? i am trying to port a program written in C to python language. The program is reading a plain text file that uses legacy font then converting it to a unicode font file. I don't have any experience with programming besides just knowing C a little bit and just learning python in 3 days ago. Anyway, how can i declare a global variable and assign a value to it in a function? Thanks Sophon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to get a line text from input file.
> i am new to programming language and python. I wonder how to get a line > text > from input file. f = open('somefile.txt') line = f.readline() For more details on using files see the Handling Files topic in my web tutor. Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to set a value to a block of memory
> How can i set a value to a bytes of block of memory. In C, i think they > use > memset like this. Python is a high level programming language and does not support direct memory access in the way that C does. Can you explain why you think you need to do this? Is it essential to access a specific memory location or do you simply want to fill a data table with values? If the latter then Python can help if you need direct memory access you will need to use C I think.. Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi
> I got this, Lets say if its mystring[n:m] where n may be another > number and m may be another number so how will it work in that case, > so this becomes general and makes more clear > n is the index of the first character and m is the index *beyond* the last character > Lets say if its mystring[3:8] so how will i evaluate it It will return the characters at indexes 3,4,5,6, and 7. In other words it operates in the same way as the range function. you could get the same set of characters back using for index in range(n,m): print mystring[index] Does that help? Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi
On 3/22/06, Alan Gauld <[EMAIL PROTECTED]> wrote: > > I got this, Lets say if its mystring[n:m] where n may be another > > number and m may be another number so how will it work in that case, > > so this becomes general and makes more clear > > > n is the index of the first character and m is the index *beyond* > the last character > > > Lets say if its mystring[3:8] so how will i evaluate it > > It will return the characters at indexes 3,4,5,6, and 7. > > In other words it operates in the same way as the range > function. > > you could get the same set of characters back using > > for index in range(n,m): >print mystring[index] > > Does that help? > > Alan G > Author of the learn to program web tutor > http://www.freenetpages.co.uk/hp/alan.gauld > > > > Thanks a Lot for all the help, Its excellent and too good Appreciate all of you for making me clear and better in understanding. Regards Kaushal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 25, Issue 54
Keo Sophon wrote: > Anyway, how can i declare a global variable and assign a value to it in a > function? The usual way to do this is to return a value from the function and assign it to the variable. For example: def double(x): return x*2 x=2 doublex = double(x) You really should look at one of the beginners tutorials listed here, they will answer many of your questions: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi
> It helps to think of the indices as pointing to in between the letters. > Not to the letters themselves. Like so, where '|' represents the mark: 'corrupt'[0:] -> '|corrupt' 'corrupt'[1:] -> 'c|orrupt' So an index of 1 moves the mark one place up. > Negative indices start from the position shown. There's a bit of asymmetry > here since foo[0] refers to the first element of the string but foo[-1] > refers to the last one. If you take both of your comments above you'll see that its quite consistent. -1 moves the marker back one place from the end to between the 2nd last and last chars. (it also helps to think of the sequence as circular so that zero is between the first and last characters!) So, 'corrupt'[-1:] -> 'corrup|t' returns from the -1 position to the end of the string - which is 't'. -2 moves the mark back 2 places and so on, so the idea of a mark between the characters is consistent with both forward and negative indexing. > I'm wrong) is that the slice operator actually creates a new string and > doesn't return a piece of the existing string. Correct and is the canonical way to shallow copy a sequence Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 25, Issue 54
> Anyway, how can i declare a global variable and assign a value to it in a > function? Variables in Python are only global in the sense of a file. You cannot declare a global that spans multiple files. However you can import a variable reference from one file to another. This is "A Good Thing(TM)" since it avoids most of the evils of global variables. You can assign to a global variable from within a function by telling the function to use the global name: x = 42 def f(): global x # say use the external variable called x x = 27 f() # actually assigns the value But that's bad practice in most cases and its better to pass the variable in as an argument and return the new value: def g(n): n = 27 return n x = g(x) # same effect as calling f() above You can read more anbout this in my tutorial topic "What's in a Name?" (And more about functions and modules in the "Modules & Functions" topic) Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple way for i18n ?
Hello all,I wish to translate a Python script from English to French. I've read the offical documentation (python.org doc) but I must admit that I'm lost now ...I've found some simple explanations here but I can't make it work: http://karrigell.sourceforge.net/en/internationalization.htmHere's where I'm stuck:Let's imagine my app is: myapp.py -- import gettext_ = gettext.gettextprint _("hello friends")print _("Bye Bye")---Here are my folders on a windows box:C:\myappfolder\---\Translations\francais\LC_MESSAGES My myapp.py is in myappfolderIn this folder I've used pygettext.py to produce a messages.pot file => I add the translation in it => I have a messages.po file.I then used msgfmt.py to produce messages.mo file.I then copied messages.po and messages.mo in LC_MESSAGES folderC:\myappfolder\ ---\Translations\francais\LC_MESSAGESI now come back to myapp.py and add two lines:---import gettext_ = gettext.gettextt=gettext.translation("messages","c:\myappfolder\Translations","francais") t.install()print _("hello friends")print _("Bye Bye")---When I do that Python anwers:>>> Traceback (most recent call last): File "C:\myappfolder\myapp.py", line 4, in ? t=gettext.translation("messages","c:\myappfolder\Translations","francais") File "C:\Python24\lib\gettext.py", line 456, in translation raise IOError(ENOENT, 'No translation file found for domain', domain) IOError: [Errno 2] No translation file found for domain: 'messages'>>> I'm stuck here.I understand it doesn't find the translation file but I can't figure out why.Can you tell me what I'm doing wrong or direct me to an URL (or a Python book) where simple internationalisation for Python is gently explained Thanks francois (...from France) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple way for i18n ?
On 3/22/06, francois schnell <[EMAIL PROTECTED]> wrote: > > Hello all, > > I wish to translate a Python script from English to French. I've read the > offical documentation (python.org doc) but I must admit that I'm lost now > ... > I've found some simple explanations here but I can't make it work: > http://karrigell.sourceforge.net/en/internationalization.htm > > Here's where I'm stuck: > > Let's imagine my app is: myapp.py > -- > import gettext > _ = gettext.gettext > > print _("hello friends") > print _("Bye Bye") > --- > > Here are my folders on a windows box: > > C:\myappfolder\ > ---\Translations\francais\LC_MESSAGES > > My myapp.py is in myappfolder > > In this folder I've used pygettext.py to produce a messages.pot file => I > add the translation in it => I have a messages.po file. > I then used msgfmt.py to produce messages.mo file. > > I then copied messages.po and messages.mo in LC_MESSAGES folder > C:\myappfolder\ > ---\Translations\francais\LC_MESSAGES > > I now come back to myapp.py and add two lines: > > --- > import gettext > _ = gettext.gettext > > t=gettext.translation("messages","c:\myappfolder\Translations","francais") > t.install() > > print _("hello friends") > print _("Bye Bye") > --- > > When I do that Python anwers: > > >>> > Traceback (most recent call last): > File "C:\myappfolder\myapp.py", line 4, in ? > > t=gettext.translation("messages","c:\myappfolder\Translations","francais") > File "C:\Python24\lib\gettext.py", line 456, in translation > raise IOError(ENOENT, 'No translation file found for domain', domain) > IOError: [Errno 2] No translation file found for domain: 'messages' > >>> > > I'm stuck here. > I understand it doesn't find the translation file but I can't figure out > why. I've always seen "standard directory structures" (e.g. \locales\fr, etc.) rather than custom ones as above, so I can't comment for sure ... but: try to either write r"c:\myappfolder\Translations" or "c:\\myappfolder\\Translations" to avoid possible backslash problems... Bonne chance! André > > Can you tell me what I'm doing wrong or direct me to an URL (or a Python > book) where simple internationalisation for Python is gently explained > > Thanks > francois (...from France) > > > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] i need help please read
ok does anyone know how to righ a script were a box pops up then if it goes over a number on the screen it pops up in the box become the input number so you can alter the number that popped up in the box? if you do can you right it and send it to me and if you cant doit all but know how i can do part will you please help me do it but remeber im new so i dont know to much ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] html and mod_python
Hi! I created a form in a python file that takes values selected from 6 different drop down boxes: for target in all_targets: s = s + "" s = s + "" % target s = s + "" for host in hosts: if target in ants_map[host]: s = s + printbox() else: s = s + printemptybox() s = s + "" s = s + "" My printbox method contains the following: def printbox(): tag = """ - 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% """ return tag When the values are entered, my program stores them into a database. Everything worked well, but now I have been asked to retain the values selected in the drop down boxes. This means that when I open the web page, the drop down boxes should display the recent values entered. I'm not sure how to do it, and I'm really hoping that someone can point me to the right direction. Thanks, Patty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] efficient method to search between two lists
Dear group, I have a question for solving a problem in more simplistic and efficient way. I have two lists: list_a = ['S83513\tNM_001117', 'X60435\tNM_001117', 'U75370\tNM_005035', 'U05861\tNM_001353', 'S68290\tNM_001353', 'D86864\tNM_145349', 'D86864\tNM_003693', 'D86864\tNM_145351', 'D63483\tNM_145349', 'D63483\tNM_003693', 'D63483\tNM_145351', 'S66427\tNM_002892', 'S57153\tNM_002892'] list_b = ['HC_G110\t1000_at\tS83513', 'HC_G110\t1001_at\tD63483', 'HC_G110\t1002_f_at\tD86864', 'HC_G112\t1003_s_at\tX60435', 'HC_G112\t1004_at\tS57153'] >>> for x in list_b: ... cola = x.split('\t')[2] ... for m in list_a: ... colb = m.split('\t')[0] ... if cola == colb: ... print x+'\t'+m ... HC_G110 1000_at S83513 S83513 NM_001117 HC_G110 1001_at D63483 D63483 NM_145349 HC_G110 1001_at D63483 D63483 NM_003693 HC_G110 1001_at D63483 D63483 NM_145351 HC_G110 1002_f_at D86864 D86864 NM_145349 HC_G110 1002_f_at D86864 D86864 NM_003693 HC_G110 1002_f_at D86864 D86864 NM_145351 HC_G112 1003_s_at X60435 X60435 NM_001117 HC_G112 1004_at S57153 S57153 NM_002892 method b: for m in list_b: cols = m.split('\t')[2] for x in list_a: if x.startswith(cols): print m+'\t'+x HC_G110 1000_at S83513 S83513 NM_001117 HC_G110 1001_at D63483 D63483 NM_145349 HC_G110 1001_at D63483 D63483 NM_003693 HC_G110 1001_at D63483 D63483 NM_145351 HC_G110 1002_f_at D86864 D86864 NM_145349 HC_G110 1002_f_at D86864 D86864 NM_003693 HC_G110 1002_f_at D86864 D86864 NM_145351 HC_G112 1003_s_at X60435 X60435 NM_001117 HC_G112 1004_at S57153 S57153 NM_002892 Problem: # of elements in list_a = 246230 # of elements in list_b = 213612 This is more brute force and hihghly time consuming method. Although dictionary is superfast, due to duplications in both columns of list_a, a dictionary option falls out. I cannot think of any other smart method since these are the only two ways I know possible. Would any one please help me suggesting a neat and efficient way. thanks in advance. Srini __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] urlopen: where are the results?
Anybody care to comment on the following? >>> from urllib2 import * >>> urlopen("http://www.kermitrose.com";) > >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] efficient method to search between two lists
> Although dictionary is superfast, due to duplications > in both columns of list_a, a dictionary option falls > out. Hi Srinivas, A dictionary method can work perfectly well. The assumption that I think is being made is that dictionary values are restricted to single values. But dictionaries are more general than this. We can make dictionaries that map from strings to lists of things. For example: ### sample = { 'a': ['alphabet', 'artichoke', 'antelope'], 'b': ['beta', 'brocolli', 'brontosaurus'], 'g': ['gamma', 'grub', 'gnome'] } ### is a perfectly good dictionary. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urlopen: where are the results?
On Wed, 22 Mar 2006, Kermit Rose wrote: > Anybody care to comment on the following? > > >>> from urllib2 import * Don't do this. *grin* Using 'from [modulename] import *' is not so good in Python because there's no warning if one of the contents in the module is overriding an existing definition. We can use 'import urllib2', or if we really want urlopen(): ## from urllib2 import urlopen ## > >>> urlopen("http://www.kermitrose.com";) > 0x00CDD768>> This is fine so far. What we're getting back is a file-like object. But Once we have this, we can use all the file-like methods we know about, like read(), in order to pull out things from the file object. (Aside: the reason that urlopen() doesn't give us the file's contents directly is being there may be other things we may want to do besides read() the whole thing at once. Concretely, we might want to progressively scan through the web site's contents without reading the whole thing in one shot if we're concerned about memory consumption. Or we might just open the file and watch for an error, as a check to see that the file exists on the web site.) But is there something else that's bothering you? Because we can't read minds very well, you'll have to make up for our deficiency by telling us what is surprising to you. *grin* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] i need help please read
On Wed, 22 Mar 2006, Tom Bachik wrote: > ok does anyone know how to righ a script were a box pops up then if it > goes over a number on the screen it pops up in the box become the input > number so you can alter the number that popped up in the box? if you do > can you right it and send it to me and if you cant doit all but know how > i can do part will you please help me do it but remeber im new so i dont > know to much Hi Tom, Frankly, I'm having a very hard time understanding your question. You are going to have to try harder if you really want a good response from us. I do not mean to be mean: I simply don't understand the terms you are using; I'm having too hard enough time trying to parse your paragraph. My mental model of what you are trying to ask is not strong. If you want help, show us what you've tried so far, and we'll do what we can to point you toward resources that can help you. If this is homework, know that we can not do it for you. If you'd like pointers to tutorial resources, you may find: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers useful; this link contains most of the beginner-level tutorials. If you go through and work through the problems in a tutorial, you should be better equipped to play with Python. You've probably already been pointed to the "How to Ask Questions the Smart Way" page, but just in case, I'll be redundant and point it out: http://www.catb.org/~esr/faqs/smart-questions.html Reading it may help you write your question in a way that will make it easier to approach and answer. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] efficient method to search between two lists
On Wed, 22 Mar 2006, Srinivas Iyyer wrote: > I cannot think of any other smart method since these > are the only two ways I know possible. > > Would any one please help me suggesting a neat and > efficient way. I'm thinking: 1) use sets to get subsets of both lists down to only those elements that will match at least one element in another list; 2) loop and compare, but only on those elements you know will match somewhere (because of step #1) One try: list_a = ['S83513\tNM_001117', 'X60435\tNM_001117', 'U75370\tNM_005035', 'U05861\tNM_001353', 'S68290\tNM_001353', 'D86864\tNM_145349', 'D86864\tNM_003693', 'D86864\tNM_145351', 'D63483\tNM_145349', 'D63483\tNM_003693', 'D63483\tNM_145351', 'S66427\tNM_002892', 'S57153\tNM_002892'] list_b = ['HC_G110\t1000_at\tS83513', 'HC_G110\t1001_at\tD63483', 'HC_G110\t1002_f_at\tD86864', 'HC_G112\t1003_s_at\tX60435', 'HC_G112\t1004_at\tS57153'] set_a = set([(x.split('\t'))[0] for x in list_a]) set_b = set([(x.split('\t'))[2] for x in list_b]) intersection = list(set_a & set_b) for (whole_b, keypart_b) in \ [(x, x.split('\t')[2]) for x in list_b if x.split('\t')[2] in intersection]: for (whole_a, keypart_a) in \ [(x, x.split('\t')[0]) for x in list_a if x.split('\t')[0] in intersection]: if keypart_b == keypart_a: print whole_b+whole_a Gives as output: HC_G110 1000_at S83513S83513NM_001117 HC_G110 1001_at D63483D63483NM_145349 HC_G110 1001_at D63483D63483NM_003693 HC_G110 1001_at D63483D63483NM_145351 HC_G110 1002_f_at D86864D86864NM_145349 HC_G110 1002_f_at D86864D86864NM_003693 HC_G110 1002_f_at D86864D86864NM_145351 HC_G112 1003_s_at X60435X60435NM_001117 HC_G112 1004_at S57153S57153NM_002892 (Note, my tab settings are different from yours, hence the different output.) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html and mod_python
On 3/23/06, Patty <[EMAIL PROTECTED]> wrote: > Hi! > > I created a form in a python file that takes values selected from 6 different > drop down boxes: > > for target in all_targets: >s = s + "" >s = s + "" % target >s = s + "" >for host in hosts: >if target in ants_map[host]: >s = s + printbox() >else: > s = s + printemptybox() >s = s + "" >s = s + "" > > My printbox method contains the following: > def printbox(): > >tag = """ > >- >0% >10% >20% >30% >40% >50% >60% >70% >80% >90% >100% >""" >return tag > > When the values are entered, my program stores them into a database. > Everything worked well, but now I have been asked to retain the values > selected in the drop down boxes. This means that when I open the web page, the > drop down boxes should display the recent values entered. I'm not sure how to > do it, and I'm really hoping that someone can point me to the right direction. > > Thanks, > Patty > I've just done something similar with a radio button. You will need to query your database to find the value. Then with that value, you can have a series of 'if' statements which print the particular option selected if it equals that value. More than likely, there's a much more efficient way to do it than this, but this worked for me. I'd be interested to hear if there were any other ways. HTH Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] basic question
Hello everybody, I loaded IDLE (python GUI) on my portable installed with Windows XPpro but when I go in Python shell to file, and New wondow ,I do not find run or execute or something like that. I suppose the link with with WSH is not made. Please can and will anybody help a seventy one year young hopefull dummy? Michel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] efficient method to search between two lists
Hi Danny, Thanks for reminding me the tip from Kent :-) da = {} for m in list_a: cols = m.split('\t') ter = cols[0] da.setdefault(ter,[]).append(m) >>> da {'S66427': ['S66427\tNM_002892'], 'U05861': ['U05861\tNM_001353'], 'D63483': ['D63483\tNM_145349', 'D63483\tNM_003693', 'D63483\tNM_145351'], 'S68290': ['S68290\tNM_001353'], 'S83513': ['S83513\tNM_001117'], 'S57153': ['S57153\tNM_002892'], 'X60435': ['X60435\tNM_001117'], 'U75370': ['U75370\tNM_005035'], 'D86864': ['D86864\tNM_145349', 'D86864\tNM_003693', 'D86864\tNM_145351']} >>> for i in list_b: ... co = i.split('\t')[2] ... items = da.get(co) ... for x in items: ... print i+'\t'+x ... HC_G110 1000_at S83513 S83513 NM_001117 HC_G110 1001_at D63483 D63483 NM_145349 HC_G110 1001_at D63483 D63483 NM_003693 HC_G110 1001_at D63483 D63483 NM_145351 HC_G110 1002_f_at D86864 D86864 NM_145349 HC_G110 1002_f_at D86864 D86864 NM_003693 HC_G110 1002_f_at D86864 D86864 NM_145351 HC_G112 1003_s_at X60435 X60435 NM_001117 HC_G112 1004_at S57153 S57153 NM_002892 Thanks again danny and Kent. --- Danny Yoo <[EMAIL PROTECTED]> wrote: > > Although dictionary is superfast, due to > duplications > > in both columns of list_a, a dictionary option > falls > > out. > > Hi Srinivas, > > A dictionary method can work perfectly well. > > The assumption that I think is being made is that > dictionary values are > restricted to single values. But dictionaries are > more general than this. > We can make dictionaries that map from strings to > lists of things. > > For example: > > ### > sample = { 'a': ['alphabet', 'artichoke', > 'antelope'], >'b': ['beta', 'brocolli', > 'brontosaurus'], >'g': ['gamma', 'grub', 'gnome'] } > ### > > is a perfectly good dictionary. > > __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] efficient method to search between two lists
Thank you Terry, I learned some stuff from your snippet. I used 'Set' but never got to use its power using LC. Thanks again Srini --- Terry Carroll <[EMAIL PROTECTED]> wrote: > On Wed, 22 Mar 2006, Srinivas Iyyer wrote: > > > I cannot think of any other smart method since > these > > are the only two ways I know possible. > > > > Would any one please help me suggesting a neat and > > efficient way. > > I'm thinking: > > 1) use sets to get subsets of both lists down to > only those elements that > will match at least one element in another list; > > 2) loop and compare, but only on those elements you > know will match > somewhere (because of step #1) > > One try: > > list_a = ['S83513\tNM_001117', 'X60435\tNM_001117', > 'U75370\tNM_005035', 'U05861\tNM_001353', > 'S68290\tNM_001353', 'D86864\tNM_145349', > 'D86864\tNM_003693', 'D86864\tNM_145351', > 'D63483\tNM_145349', 'D63483\tNM_003693', > 'D63483\tNM_145351', 'S66427\tNM_002892', > 'S57153\tNM_002892'] > > list_b = ['HC_G110\t1000_at\tS83513', > 'HC_G110\t1001_at\tD63483', > 'HC_G110\t1002_f_at\tD86864', > 'HC_G112\t1003_s_at\tX60435', > 'HC_G112\t1004_at\tS57153'] > > set_a = set([(x.split('\t'))[0] for x in list_a]) > set_b = set([(x.split('\t'))[2] for x in list_b]) > intersection = list(set_a & set_b) > > for (whole_b, keypart_b) in \ > [(x, x.split('\t')[2]) for x in list_b > if x.split('\t')[2] in intersection]: > > for (whole_a, keypart_a) in \ > [(x, x.split('\t')[0]) for x in list_a > if x.split('\t')[0] in intersection]: > > if keypart_b == keypart_a: > print whole_b+whole_a > > Gives as output: > > HC_G110 1000_at S83513S83513NM_001117 > HC_G110 1001_at D63483D63483NM_145349 > HC_G110 1001_at D63483D63483NM_003693 > HC_G110 1001_at D63483D63483NM_145351 > HC_G110 1002_f_at D86864D86864NM_145349 > HC_G110 1002_f_at D86864D86864NM_003693 > HC_G110 1002_f_at D86864D86864NM_145351 > HC_G112 1003_s_at X60435X60435NM_001117 > HC_G112 1004_at S57153S57153NM_002892 > > > (Note, my tab settings are different from yours, > hence the different > output.) > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor