简体   繁体   English

如何根据某些条件合并两个字符串

[英]how to Merge two strings based on some conditions

this are two strings 这是两个字符串

a=  PVT corner         |    TYP_25    |    SLOW_125 |    SLOW_0_   |    SLOW_40 |    FAST_125 

b= Description         |  RD   |  WR | A   |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     

I need to check the length of each item in "a" and compare with "b". 我需要检查“ a”中每个项目的长度,并与“ b”进行比较。 And if any "|" 如果有“ |” found in between item in "b" then it has to be concatenated with a's item, like this, "RD" ,"WR" and "A" should be concatenated with TYP_25 of a. 如果在“ b”中的项目之间找到它,则必须将其与a的项目并置,例如,“ RD”,“ WR”和“ A”应与a的TYP_25并置。 how to Merge two strings based on this condition? 如何根据此条件合并两个字符串?

You have a | 您有一个| character every 20 positions; 每20个职位角色; split your strings into sections of 20 characters, and pair up the results: 将您的字符串分成20个字符,然后将结果配对:

def by_width(line, width=20, stripchars='|'):
    i = 0
    while i < len(line):
        yield line[i:i+width].strip(stripchars)
        i += width

Zip the results together gives: 将结果压缩在一起可得出:

>>> for column_a, column_b in zip(by_width(a), by_width(b)):
...     print [column_a.strip()] + [v.strip() for v in column_b.split('|')]
... 
['PVT corner', 'Description']
['TYP_25_0P85', 'RD', 'WR', 'A']
['SLOW_125_0P765', 'RD', 'WR']
['SLOW_0_0P765', 'RD', 'WR']
['SLOW_M40_0P765', 'RD', 'WR']
['FAST_125_0P935', 'RD', 'WR']

From there on out you can do what you want with the columns; 从那里开始,您可以对列进行所需的操作; in the above sample I merely put them together into lists of whitespace-stripped strings. 在上面的示例中,我只是将它们放到了空白字符串列表中。

if the format is confirmed, I think this would be ok: 如果格式被确认,我认为可以。

tmp_flag1 = "#"
tmp_flag2 = "|"
tmp_str1 = a.replace(tmp_flag1, "")
tmp_str2 = b.replace(tmp_flag1, "")

tmp_str3 = ""
tmp_pos_head = 0
tmp_pos_tail = 0
tmp_is_equal = False

tmp_ret = tmp_str1.find(tmp_flag2)
while tmp_ret != -1:
        tmp_pos_tail = tmp_ret
        if tmp_str2[tmp_ret] == tmp_flag2:
                tmp_buf1 = tmp_str1[tmp_pos_head:tmp_pos_tail].replace(tmp_flag2, "")
                tmp_buf2 = tmp_str2[tmp_pos_head:tmp_pos_tail].replace(tmp_flag2, "")
                tmp_str3 += tmp_buf1 + ":" + tmp_buf2 + "\n"
                tmp_pos_head = tmp_ret + 1
                tmp_is_equal = True

         tmp_ret = tmp_str1.find(tmp_flag2, tmp_ret + 1)

if tmp_is_equal == True:
        tmp_buf1 = tmp_str1[tmp_pos_tail:].replace(tmp_flag2, "")
        tmp_buf2 = tmp_str2[tmp_pos_tail:].replace(tmp_flag2, "")
else:
        tmp_buf1 = tmp_str1[tmp_pos_head:].replace(tmp_flag2, "")
        tmp_buf2 = tmp_str2[tmp_pos_head:].replace(tmp_flag2, "")
tmp_str3 += tmp_buf1 + ":" + tmp_buf2

print tmp_str3

Using a zip works, regardless of whether the widths are equal. 不管宽度是否相等,都可以使用zip

a = "  PVT corner         |    TYP_25    |    SLOW_125    SLOW_0_   |    SLOW_M40|    FAST_12 "
b = " Description         |  RD   |  WR | A   |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     |  RD     |  WR     "
head = 0
res = []
for i,(s,t) in enumerate(zip(a,b)):
    if (s,t) == ("|","|"):
        res.append([a[head:i].strip()]+[m.strip() for m in b[head:i].split("|")])
        head = i + 1
res.append([a[head:].strip()]+[m.strip() for m in b[head:].split("|")])

for r in res:
    print r

The output is 输出是

['PVT corner', 'Description']
['TYP_25', 'RD', 'WR', 'A']
['SLOW_125', 'RD', 'WR']
['SLOW_0', 'RD', 'WR']
['SLOW_40', 'RD', 'WR']
['FAST_125', 'RD', 'WR']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM