简体   繁体   English

Python:使用while循环从单词中删除某些字符

[英]Python: Removing certain characters from a word using while loop

Given that certain characters are 'abcdef' = char I would like to 1) remove 3rd char of the 3 chars in a row in a word, and 2) from changed word, remove 2nd char of the 2 chars in a row in the word. 鉴于某些字符是“ABCDEF” = 字符我想在一个字为1)取出3 个字符的第三成一排,和2)从改变后的字,在该字中删除2 个字符的第二字符连续。

Eg If there is a word 'bacifeaghab' it should 例如,如果有单词“ bacifeaghab”,则应

1) first remove 'c' and 'a', which is ba(c)ife(a)hab and change word into 'baifehab' 1)首先删除'c'和'a',即ba(c)ife(a)hab并将单词更改为'baifehab'

2) remove 'a','e', and 'b', which is b(a)if(e)ha(b) and change word into 'bifha' 2)删除'a','e'和'b',即b(a)if(e)ha(b)并将单词更改为'bifha'

This is what I have done so far, but when I run this and put word in it, it doesn't pint anything. 到目前为止,这是我所做的事情,但是当我运行此命令并输入文字时,它什么都没有。 Not even error or blank(' '), it just goes to next line without '>>>'. 甚至没有错误或为空白(''),它仅跳至下一行而不包含'>>>'。

def removal(w):
    x,y = 0,0
    while y < len(w)-2:
        if (w[y] and w[y+1] and w[y+2]) in 'abcdef':
            w = w[:y+2] + w[y+3:]
    while x < len(w):
        if  w[x] in 'abcedf':
            w = w[:x+1] + w[x+2:]
            x = x+1
        else :
            x = x+1
    return(w)

Could anyone find out what's wrong?? 谁能找出问题所在?

Since it was first time for me to use while loop, I thought that using double while loop can be a problem, so also tried, 由于这是我第一次使用while循环,因此我认为使用double while循环可能会出现问题,因此也尝试过,

def removal(w):
    x,y,z = 0,0,0
    while y < len(w)-2:
        if (w[y] and w[y+1] and w[y+2]) in 'abcdef':
            w = w[:y+2] + w[y+3:]
    return(w)

But same result. 但结果相同。 I also tried print(w) at the end of function. 我还在函数结尾尝试了print(w)。 same result. 同样的结果。

You had a couple of mistakes: 您有几个错误:

There were 2 errors that I have corrected in your code. 我在您的代码中纠正了2错误。 The first being that you weren't incrementing x properly (as you did not need the elif ) and you had forgotten completely to increment y ! 第一个是您没有正确地递增x (因为您不需要elif ),并且您完全忘记了递增y

I corrected these and then also, in your if conditions, your syntax was incorrect. 我更正了这些,然后在您的if条件下,您的语法不正确。 The part in the brackets evaluated to the last element and then just this was checked to see if it was in the string 'abcdef' . 括号中的部分求值到最后一个元素,然后仅检查该部分以查看它是否在字符串'abcdef' I have corrected that now to check each individual element in turn. 我现在已更正此问题,以依次检查每个单独的元素。

So now the function is: 所以现在的功能是:

def removal(w):
    chars = 'abcdef'
    x,y = 0,0
    while y < len(w)-2:
        if w[y] in chars and w[y+1] in chars and w[y+2] in chars:
            w = w[:y+2] + w[y+3:]
        y += 3
    while x < len(w):
        if  w[x] in 'abcedf' and w[x+1] in 'abcdef':
            w = w[:x+1] + w[x+2:]
        x += 2
    return w

and calling it with 'bacifeahab' (without the 'g' which I think was a typo in your eg): 并以'bacifeahab' (不包含'g' ,我认为是您的错字)来称呼它:

removal("bacifeahab")

returns what you wanted: returns您想要的:

'bifha'

Hope this helps! 希望这可以帮助!

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

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