如何将字符串拆分为连续的子字符串,例如:string_inputs:IN PYTHON3

时间:2018-03-05 10:53:16

标签: python-3.x

如何将字符串拆分为连续的子字符串,例如: 的 string_inputs:

AAAATTTT ATATATAT

output_needed:

AA,AA,AA,AT,TT,TT,TT

3 个答案:

答案 0 :(得分:0)

string_inputs = "AAAATTTTATATATAT"

#Note the `len(string_inputs) - 1` in the range; do according to what you desire as your output
d_out = [string_inputs[i:i+2] for i in range(0, len(string_inputs) - 1)]

d_out = ['AA', 'AA', 'AA', 'AT', 'TT', 'TT', 'TT', 'TA', 'AT', 'TA', 'AT', 'TA', 'AT', 'TA', 'AT']

答案 1 :(得分:0)

您可以在此处获取zip()的每两个子字符串:

>>> string = 'AAAATTTTATATATAT'
>>> ','.join(x + y for x, y in zip(string, string[1:]))
'AA,AA,AA,AT,TT,TT,TT,TA,AT,TA,AT,TA,AT,TA,AT'

这也是另一种方式:

>>> ','.join(map(''.join, zip(string, string[1:])))
'AA,AA,AA,AT,TT,TT,TT,TA,AT,TA,AT,TA,AT,TA,AT'

甚至是这样的事情:

>>> ','.join(map(lambda x, y: x + y, string, string[1:]))
'AA,AA,AA,AT,TT,TT,TT,TA,AT,TA,AT,TA,AT,TA,AT'

答案 2 :(得分:0)

这就是您正在寻找的。

string = "AAAATTTTATATATAT"
step = 2
split = [string[i:i+step] for i in range(0, len(string), step)]
print(",".join(split))

step是您要将字符串拆分为的块。最后一行将,与示例中的AA,AA,TT,TT,AT,AT,AT,AT连接起来,为您提供最终输出import xml.etree.ElementTree as ET xml_data = """<results status="passed"> <num-records>2</num-records> <records> <volume-info> <flexible-volume-info> <agg-name>aggr1_split</agg-name> </flexible-volume-info> <volume-name>volume1</volume-name> <volume-size> <actual-size>44</actual-size> <afs-avail>90</afs-avail> </volume-size> </volume-info> <volume-info> <flexible-volume-info> <agg-name>aggr2_split</agg-name> </flexible-volume-info> <volume-name>volume2</volume-name> <volume-size> <actual-size>10</actual-size> <afs-avail>14</afs-avail> </volume-size> </volume-info> </records> </results>""" root = ET.fromstring(xml_data) results = [] for child in root.iter("volume-info"): result = {} print(child) result['agg-name'] = child.find('flexible-volume-info/agg-name').text result['volume-name'] = child.find('volume-name').text result['actual-size'] = child.find('volume-size/actual-size').text results.append(result) print(results)