I have written a python code to count the number of possible pairs in a given sequence. But I have to run the code again and again, while changing the distance parameter, to get all the possible pairs with varying distances. I want to optimize my code but I am not able to figure out how to do that. Eg: When I run my code I get the pairs AB, BC, CD, DE, EF, FG, GH. this is the pair possibility with the distance of 1 between alphabets. But I also want to print other possibilities, such as, when distance is 2 the pairs will be AC, BD, CE, DF, EH, FG, and similarly for other distances as well. I want to write a single code to give me all the possible outputs. Currently, I have to change the line "if(0<= I <7)" to "if(0<=i<6)" and then "if(0<=i<5)". This becomes really cumbersome when the sequence length is really large. Any help will be much appreciated.
seq = "ABCDEFHG"
l = []
for i in range(len(seq)):
if(0<=i<7):
c1 = seq[i]
c2 = seq[i+1]
l.append(c1+c2)
print(l)
It's not the most efficient cause of the try-except but im guessing it doesn't really matter in your usecase.
Anyway, here you go:
seq = "ABCDEFHG"
l = []
for k in range(1, len(seq)):
for i in range(len(seq)):
if(i<len(seq)-1):
try:
c1 = seq[i]
c2 = seq[i+k]
l.append(c1+c2)
except:
pass
print(l)
l.clear()
Not sure if this what you want, but check this out:
seq = "ABCDEFHG"
length = len(seq)
for d in range(1,length):
l = []
for i in range(length):
if(0<=i<length-d):
c1 = seq[i]
c2 = seq[i+d]
l.append(c1+c2)
print("Distance {} : {}".format(d,l))
Output:
Distance 1 : ['AB', 'BC', 'CD', 'DE', 'EF', 'FH', 'HG']
Distance 2 : ['AC', 'BD', 'CE', 'DF', 'EH', 'FG']
Distance 3 : ['AD', 'BE', 'CF', 'DH', 'EG']
Distance 4 : ['AE', 'BF', 'CH', 'DG']
Distance 5 : ['AF', 'BH', 'CG']
Distance 6 : ['AH', 'BG']
Distance 7 : ['AG']
You can do this by using nested for loops.
seq = "ABCDEFHG"
l = []
for i in range(len(seq)):
for j in range(i + 1):
if j != i:
l.append(seq[j] + seq[i])
l.sort()
print(l)
Output
['AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'CD',
'CE', 'CF', 'CG', 'CH', 'DE', 'DF', 'DG', 'DH', 'EF', 'EG', 'EH', 'FG', 'FH', 'HG']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.