为什么.count()不能正确识别给定字符串中有多少个“)”?

时间:2019-06-24 05:04:45

标签: python-3.x

我的目标是将字符串转换为新字符串,其中新字符串中的每个字符为“(”(如果该字符在原始字符串中仅出现一次)或“)”(如果该字符在原始字符串中多次出现)串。而且在确定字符是否重复时我忽略了大写字母。

我的代码:

        FhirContext fhirCtx = FhirContext.forDstu3(); // DSTU3
        IParser p = fhirCtx.newJsonParser();
        p.setParserErrorHandler(new StrictErrorHandler());

        try {
            p.parseResource(jsonAsString); // Pass your Patient's JSON string here
        } catch (Exception e) {
            // Do something here
        }

以上输出:def duplicate_encode(word): word = word.lower() for i in word: if word.count(i) > 1: word = word.replace(i, ")") elif word.count(i) == 1: word = word.replace(i, "(") print(word) duplicate_encode( "S@k@T)ayFSSnvzyGP!nwSvcTH(") 而不是正确的:)))))))))))))))))))))))))) 那么为什么要正确计算和替换开括号而不是#闭括号呢?以及如何使它起作用?

3 个答案:

答案 0 :(得分:4)

您正在迭代word时对其进行了修改,因此在查看第一个字符后,word现在为:

)))gqgyltcgfr(yul

因此,第三个)在您到达时不再是唯一的。最终,您将所有先前的(替换为),更好的方法可能是随行构建新的字符串:

def duplicate_encode(word):
    word = word.lower()
    res = ''
    for i in word:
        if word.count(i) > 1:
            res +=  ")"
        elif word.count(i) == 1:
            res += "("
    return res


print(duplicate_encode("S@k@T)ayFSSnvzyGP!nwSvcTH("))

结果

))())(()())))()((()())()((

答案 1 :(得分:1)

在循环的第一次迭代中,将m替换为)q之前的(。然后到最后,您将所有(换成)(包括括号是输出字符串而不是输入字符串...)。

这是我的建议

from collections import Counter

def duplicate_encode(word):
    word = word.lower()
    counter = Counter(word)
    t_table = {char: ")" if i == 1 else "(" for char, i in counter.items()}
    return word.translate(str.maketrans(t_table))

产生:

word = "mm)GQGylTcGFR(yul"
ret = duplicate_encode(word)
print(word) # mm)GQGylTcGFR(yul
print(ret)  # ))()()))(()((()()

word = "S@k@T)ayFSSnvzyGP!nwSvcTH("
ret = duplicate_encode(word)
print(word)  # S@k@T)ayFSSnvzyGP!nwSvcTH(
print(ret)   # ))())(()())))()((()())()((

使用str.translate可以避免重新替换已经替换的字符。

请注意,仅对word进行一次迭代以计算字符的出现;然后一次以翻译字符。这种实现非常有效(因为大多数循环都委托给了一个库)。

答案 2 :(得分:0)

问题不是计数不能正常工作。相反,变量word处于循环中,导致if word.count(i) > 1:if "mm)GQGylTcGFR(yul".count(i) > 1:不一致。让我们看一下打印语句中的输出,看看发生了什么:

def duplicate_encode(word):
    word = word.lower()
    for i in word:
        print(word)
        if word.count(i) > 1:
            word = word.replace(i, ")")
        elif word.count(i) == 1:
            word = word.replace(i, "(")
    print(word)


duplicate_encode("S@k@T)ayFSSnvzyGP!nwSvcTH(")

输出:

s@k@t)ayfssnvzygp!nwsvcth(
)@k@t)ayf))nvzygp!nw)vcth(
))k)t)ayf))nvzygp!nw)vcth(
))()t)ayf))nvzygp!nw)vcth(
))()t)ayf))nvzygp!nw)vcth(
))()))ayf))nvzygp!nw)vc)h(
))()))ayf))nvzygp!nw)vc)h(
))()))(yf))nvzygp!nw)vc)h(
))()))()f))nvz)gp!nw)vc)h(
))()))()())nvz)gp!nw)vc)h(
))()))()())nvz)gp!nw)vc)h(
))()))()())nvz)gp!nw)vc)h(
))()))()()))vz)gp!)w)vc)h(
))()))()())))z)gp!)w))c)h(
))()))()())))()gp!)w))c)h(
))()))()())))()gp!)w))c)h(
))()))()())))()(p!)w))c)h(
))()))()())))()((!)w))c)h(
))()))()())))()((()w))c)h(
))()))()())))()((()w))c)h(
))()))()())))()((()())c)h(
))()))()())))()((()())c)h(
))()))()())))()((()())c)h(
))()))()())))()((()())()h(
))()))()())))()((()())()h(
))()))()())))()((()())()((
))))))))))))))))))))))))))

如您所见,您实际上并不是在计算原始字符串中的字符,而是在部分替换了字符的字符串中。

以下是一种明显的解决方案:

def duplicate_encode(word):
    word = word.lower()
    result = word
    for i in word:
        if word.count(i) > 1:
            result = result.replace(i, ")")
        elif word.count(i) == 1:
            result = result.replace(i, "(")
    print(result)


duplicate_encode("S@k@T)ayFSSnvzyGP!nwSvcTH(")

输出:

((((((()((())()((()(()((((

嗯,不太正确。让我们再次输入打印语句:

def duplicate_encode(word):
    word = word.lower()
    result = word
    for i in word:
        if word.count(i) > 1:
            result = result.replace(i, ")")
        elif word.count(i) == 1:
            result = result.replace(i, "(")
        print("After subbing for %s: %s" % (i, result))
    print(result)


duplicate_encode("S@k@T)ayFSSnvzyGP!nwSvcTH(")

输出:

After subbing for s: )@k@t)ayf))nvzygp!nw)vcth(
After subbing for @: ))k)t)ayf))nvzygp!nw)vcth(
After subbing for k: ))()t)ayf))nvzygp!nw)vcth(
After subbing for @: ))()t)ayf))nvzygp!nw)vcth(
After subbing for t: ))()))ayf))nvzygp!nw)vc)h(
After subbing for ): ((((((ayf((nvzygp!nw(vc(h(
After subbing for a: (((((((yf((nvzygp!nw(vc(h(
After subbing for y: ((((((()f((nvz)gp!nw(vc(h(
After subbing for f: ((((((()(((nvz)gp!nw(vc(h(
After subbing for s: ((((((()(((nvz)gp!nw(vc(h(
After subbing for s: ((((((()(((nvz)gp!nw(vc(h(
After subbing for n: ((((((()((()vz)gp!)w(vc(h(
After subbing for v: ((((((()((())z)gp!)w()c(h(
After subbing for z: ((((((()((())()gp!)w()c(h(
After subbing for y: ((((((()((())()gp!)w()c(h(
After subbing for g: ((((((()((())()(p!)w()c(h(
After subbing for p: ((((((()((())()((!)w()c(h(
After subbing for !: ((((((()((())()((()w()c(h(
After subbing for n: ((((((()((())()((()w()c(h(
After subbing for w: ((((((()((())()((()(()c(h(
After subbing for s: ((((((()((())()((()(()c(h(
After subbing for v: ((((((()((())()((()(()c(h(
After subbing for c: ((((((()((())()((()(()((h(
After subbing for t: ((((((()((())()((()(()((h(
After subbing for h: ((((((()((())()((()(()((((
After subbing for (: ((((((()((())()((()(()((((
((((((()((())()((()(()((((

啊哈,有问题。我们用括号括起来。嗯,看来我们需要以不同的方式处理问题了。代替替换字符串中的字符,让我们在遍历字符串时构建一个新的字符:

def duplicate_encode(word):
    word = word.lower()
    result = ""
    for i in word:
        if word.count(i) > 1:
            result += ")"
        elif word.count(i) == 1:
            result += "("
    print(result)


duplicate_encode("S@k@T)ayFSSnvzyGP!nwSvcTH(")

输出:

))())(()())))()((()())()((

我们去了!