python - check if list of strings is found exactly n times -


i'm struggling synthesise several answers i've been reading come close i'm trying do, , can't formulate google-fu that'll me existing answer! know it's simple i'm @ loss now.

very similar this question, want find several strings i've read tuple occurr in file, however, want lines each string matched only once. any , all don't fit bill far can tell.

what i've got far close, line.count giving me numbers of occurrences each line, it's wrong in 2 ways:

firstly, line.count under 1 somehow given line?

i know i'm doing wrong how i'm iterating/searching each key and/or using == 1 test, can't figure out.

the tuple of strings i'm looking is:

['ag49', 'ag51', 'agbd', 'aght', 'agjn', 'agkc', 'agnp', 'agti', 'lg01', 'lg33', 'lg45'] 

and example lines of file search (they have 2 many tens of entries (og_1000 below longest line/most members):

og_1000: ag49|00461 ag49|03016 ag49|03395 ag49|01465 ag49|01485 ag49|02179 ag49|02513 ag49|03071 ag49|03396 ag49|02649 ag51|00302 ag51|00779 ag51|01746 ag51|02077 ag51|02502 ag51|01654 ag51|01963 ag51|01965 agbd|01544 agbd|02407 agbd|02722 agbd|03152 agbd|02292 agbd|03607 agbd|03608 agbd|03609 aght|00130 aght|00873 aght|00911 aght|01291 aght|02476 aght|02881 aght|02477 aght|02973 aght|02974 aght|02975 agjn|00381 agjn|00633 agjn|01876 agjn|02007 agjn|02058 agjn|02059 agjn|02060 agjn|02398 agjn|02399 agjn|02433 agjn|02418 agkc|00658 agkc|00659 agkc|00660 agkc|01985 agkc|02826 agkc|02881 agkc|01323 agkc|01327 agkc|01324 agkc|02267 agkc|02827 agkc|02880 agkc|04269 agkc|02428 agnp|00290 agnp|02833 agnp|03160 agnp|03601 agnp|03987 agnp|03988 agnp|03989 agnp|04108 agti|00388 agti|01459 agti|03163 agti|03688 agti|00570 agti|04026 agti|03715 agti|03716 agti|03717 lg01|00908 lg01|00909 lg01|00910 lg01|01116 lg01|03323 lg01|03588 lg01|03589 lg01|03590 lg01|03591 lg01|01118 lg01|01908 lg01|03182 lg01|03189 lg01|01906 lg33|01192 lg33|01786 lg33|01787 lg33|01973 lg33|03700 lg33|04518 lg33|04759 lg33|01756 lg33|01760 lg33|01971 lg33|02055 lg33|02056 lg33|02057 lg45|00001 lg45|01508 lg45|01643 lg45|00233 lg45|00786 lg45|01599 lg45|01600 lg45|01601 lg45|04210 lg45|04212 lg45|04213 lg45|04637 lg45|03265 lg45|04211 lg45|03255 lg45|03261 ag51|00629 agkc|04214 ag49|02651 agbd|01546 agkc|02430 agnp|02835 agti|01461 lg45|00784 lg33|04104 lg45|00192 lg45|00193 lg33|00381 lg33|01750 og_1082: ag49|00880 ag49|02960 ag51|02815 ag51|04137 agnp|00113 agnp|03735 agti|00006 agti|02047 agbd|01827 aght|00357 agjn|03158 agkc|02788 lg01|01472 lg33|02682 lg45|01009 og_7229: lg33|04676 lg45|01800 

an example valid line be:

og_1264: ag49|00061 ag51|03472 agbd|01583 aght|03015 agjn|02348 agkc|00003 agnp|02702 agti|02067 lg01|00073 lg33|02222 lg45|04062 

where each string occurs once.

my code @ moment (minus option parsing etc):

# tuple of strings iterate on def getkeys(namefile):     open(namefile, "r") namehandle:         names = []             line in namehandle:                 strip = line.rstrip('\n')             names.append(strip)      return names  # main code: keys = getkeys(namefile)  matchedlines = []  open(args.infile, "r") clusterfile:     line in clusterfile:         key in keys:             if line.count(key) == 1:                 matchedlines.append(line) 

in code, matchedlines have many times same line , still doesn't give lines match of keys once. purpose, can still use all:

with open(args.infile, "r") clusterfile:     matchedlines = [line line in clusterfile if all([line.count(key) == 1 key in keys])] 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -