python - Compare groups of 4 characters to see if they're the same in a string -
i'm trying use python compare 2 strings , see if of them have common groups of 4 letters.
sequence1 = "acacgcgtctccttgcgggtaaat" sequence2 = "gttaccaatttcttgtttccgaat" in range(0,24,4): print list1.append(sequence1[i:i+4]) in range(0,24,4): print list2.append(sequence2[i:i+4]) but doesn't seem it. want return groups of letters equal in both strings, ideas?
you can iterate on groups of 4 using list comprehension slicing. have edited sequences contain 2 common 4 letter elements:
s1 = "acacgcggtctcttgcgggaaatt" s2 = "gttaccaatttcttgcttccaaat" c = [s1[i:i+4] in range(0, len(s1), 4) if s1[i:i+4] in s2] the list c contains common entries: ['ttgc', 'aaat']
do note, not discriminate on position; if required change if statement in list comprehension define that:
c = [s1[i:i+4] in range(0, len(s1), 4) if s1[i:i+4] == s2[i:i+4]] now contains ['ttgc'].
Comments
Post a Comment