2017-09-01 89 views

回答

0
s = "CAXAAYXZA" 
a = numpy.array(list(s)) 
start_indexes= numpy.where(a=='A')[0] 
end_indexes= numpy.where(a=='X')[0] 
print(sum(1 for i in start_indexes for j in end_indexes if i < j)) 

一個方式,你可以做到這一點

0

你可以申請一個正則表達式,並在每個長度搜索組:

s = 'CAXAAYXZA' 
n = sum(len(re.findall('(A\w{%i}X)' % i, s)) for i in range(len(s))) 

執行後,n,在這種情況下,將等於4。或者以展開形式:

n = 0 
for i in range(len(s)): 
    # Increase the total by the groups at i 
    n += len(re.findall('(A\w{%i}X)' % i, s)) 
0
print "DO YOUR ASSIGNMENTS BY YOURSELF" 
string="CAXAAYXZA" 
string_char=list(string) 
string_length=len(string_char) 
print(string_char) 
char_first="A" 
char_second="X" 
for i in range(0,string_length): 
    for b in range(i,string_length): 
     if(string_char[i]==char_first and string_char[b]==char_second): 
      list=[] 
      for z in range(i,b+1): 
       list.append(string_char[z]) 
      print(''.join(list)) 

OUTPUT:

DO分配給你自己

[ 'C', 'A', 'X', 'A',「A 」, 'Y', 'X', 'Z', 'A']

AX

AXAAYX

AAYX

AYX

+0

請注意,如果你使用它,我就浪費了一些時間。 –

相關問題