I am trying to compare two lists to see if there are any matching strings within the lists.
lets say list_1
is
['GAAGGTCGAA', 'GAAGGTCGA', 'AAGGTCGAA', 'GAAGGTCG', 'AAGGTCGA', 'AGGTCGAA', 'GAAGGTC', 'AAGGTCG', 'AGGTCGA', 'GGTCGAA', 'GAAGGT', 'AAGGTC', 'AGGTCG', 'GGTCGA', 'GTCGAA', 'GAAGG', 'AAGGT', 'AGGTC', 'GGTCG', 'GTCGA', 'TCGAA', 'GAAG', 'AAGG', 'AGGT', 'GGTC', 'GTCG', 'TCGA', 'CGAA', 'GAA', 'AAG', 'AGG', 'GGT', 'GTC', 'TCG', 'CGA', 'GAA', 'GA', 'AA', 'AG', 'GG', 'GT', 'TC', 'CG', 'GA', 'AA', 'G', 'A', 'A', 'G', 'G', 'T', 'C', 'G', 'A', 'A']
and list_2
is
['CCTCGGGA', 'CCTCGGG', 'CTCGGGA', 'CCTCGG', 'CTCGGG', 'TCGGGA', 'CCTCG', 'CTCGG', 'TCGGG', 'CGGGA', 'CCTC', 'CTCG', 'TCGG', 'CGGG', 'GGGA', 'CCT', 'CTC', 'TCG', 'CGG', 'GGG', 'GGA', 'CC', 'CT', 'TC', 'CG', 'GG', 'GG', 'GA', 'C', 'C', 'T', 'C', 'G', 'G', 'G', 'A', '', '', '', '', '', '', '', '', '', 'CCTCGGG', '', '', '', '', '', '', '', '', '']
I know that "TCG" is in both lists, but what type of function can I use to determine this within python? I tried a for loop and many other built in functions but I can't get it to work.
This is actually easy with set
print set(list_1) & set(list_2)
print bool(set(list_1) & set(list_2))
# set(['A', 'C', 'G', 'CG', 'GG', 'T', 'GA', 'TCG', 'TC']) //common strings
# true //Boolean result
DEMO
http://ideone.com/y0GltE
bool(set(list_1) & set(list_2))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With