小编Kev*_*vin的帖子

首先检查字符串中每一行的几位数字,如果它们相等,则将这些行的一部分打印在一起

例如我有一个字符串是

textstring= """
0000 Onn ch=1 n=60 v=50   
0000 Onn ch=1 n=67 v=50
9600 Off ch=1 n=67 v=00
9600 Off ch=1 n=60 v=00
9600 Onn ch=1 n=62 v=50
9600 Onn ch=1 n=69 v=50
1920 Off ch=1 n=69 v=00
1920 Off ch=1 n=62 v=00
"""   
Run Code Online (Sandbox Code Playgroud)

例如9600,当行的前四位数字相等时,如何将 67/60/62/69 打印在一起?(来自 之后的四行中的每一行n=)

我尝试过类似下面的方法,但我认为它没有按预期工作

for i, char in enumerate(textstring):
    if char=="O" and (textstring[i+1]=="f" or textstring[i+1]=="n"):
        if textstring[i-5]==textstring[i+18] and textstring[i-4]==textstring[i+19] and textstring[i-3]==textstring[i+20] and textstring[i-2]==textstring[i+21]:
            if char=="n" and textstring[i+1]=="=":  #we look for "n=" …
Run Code Online (Sandbox Code Playgroud)

python string python-3.x

5
推荐指数
1
解决办法
61
查看次数

标签 统计

python ×1

python-3.x ×1

string ×1