Thanks so much for the help! and suggestions I not use dictionary because I have some items are repeated inside the list
>----Messaggio originale---- >Da: tutor-requ...@python.org >Data: 27/05/2014 12.00 >A: <tutor@python.org> >Ogg: Tutor Digest, Vol 123, Issue 65 > >Send Tutor mailing list submissions to > tutor@python.org > >To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > >You can reach the person managing the list at > tutor-ow...@python.org > >When replying, please edit your Subject line so it is more specific >than "Re: Contents of Tutor digest..." > > >Today's Topics: > > 1. Help on best way to check resence of item inside list > (jarod...@libero.it) > 2. Re: Help on best way to check resence of item inside list > (Mark Lawrence) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Tue, 27 May 2014 10:05:30 +0200 (CEST) >From: "jarod...@libero.it" <jarod...@libero.it> >To: tutor@python.org >Subject: [Tutor] Help on best way to check resence of item inside > list >Message-ID: <975274200.13645911401177930495.JavaMail.actor@webmail48> >Content-Type: text/plain;charset="UTF-8" > >Dear All > >clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," >electra"] >clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," >Castiel","Sam"] > >I have a list of names that I would to annotate in function of presence in >different clubs: > >my input files is a long file where I have this : > >mary >luke >luigi >jane >jessica >rebecca >luis >################################################? > >with open("file.in") as p: >mit = [] >for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] >mit.append(G) > > >for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) >############################################################### >I just wonder if is appropriate to use a loops to check if is present the >value on a list. Is it the right way? I can use a dictionary because I have >many repeated names. > >In the end I wan to have > > >mary clubA clubB >luke clubA >luigi no >Thanks in advance for any help > > >------------------------------ > >Message: 2 >Date: Tue, 27 May 2014 09:48:27 +0100 >From: Mark Lawrence <breamore...@yahoo.co.uk> >To: tutor@python.org >Subject: Re: [Tutor] Help on best way to check resence of item inside > list >Message-ID: <lm1jgs$o8h$1...@ger.gmane.org> >Content-Type: text/plain; charset=UTF-8; format=flowed > >On 27/05/2014 09:05, jarod...@libero.it wrote: >> Dear All >> >> clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," >> electra"] >> clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," >> Castiel","Sam"] >> >> I have a list of names that I would to annotate in function of presence in >> different clubs: >> >> my input files is a long file where I have this : >> >> mary >> luke >> luigi >> jane >> jessica >> rebecca >> luis >> ################################################? >> >> with open("file.in") as p: >> mit = [] >> for i in p: >> lines =i.strip("\n").split("\t") >> if (lines[0] in clubA: >> G =lines[-1] +["clubA"] >> else: >> G = lines[-1] +["no"] >> mit.append(G) >> >> >> for i in mit: >> if i.strip("\n").split("\t")[0] in clubB: >> G =lines[-1] +["clubB"] >> else: >> G = lines[-1] +["no"] >> finale.append(G) >> ############################################################### >> I just wonder if is appropriate to use a loops to check if is present the >> value on a list. Is it the right way? I can use a dictionary because I have >> many repeated names. >> >> In the end I wan to have >> >> >> mary clubA clubB >> luke clubA >> luigi no >> Thanks in advance for any help > >You can use the in keyword to check for an item in a list. However a >very quick glance at your code suggests that you could cut out the list >completely and do the same using the in keyword against your dict. >Better still I think the defaultdict is what you need here, I'll leave >you to look it up as I must dash :) > > >-- >My fellow Pythonistas, ask not what our language can do for you, ask >what you can do for our language. > >Mark Lawrence > >--- >This email is free from viruses and malware because avast! Antivirus protection is active. >http://www.avast.com > > > > >------------------------------ > >Subject: Digest Footer > >_______________________________________________ >Tutor maillist - Tutor@python.org >https://mail.python.org/mailman/listinfo/tutor > > >------------------------------ > >End of Tutor Digest, Vol 123, Issue 65 >************************************** > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor