第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何轉(zhuǎn)義括號python

如何轉(zhuǎn)義括號python

呼喚遠(yuǎn)方 2023-03-16 09:45:53
我是編程的新手。本練習(xí)的目標(biāo)是將字符串轉(zhuǎn)換為新字符串,其中新字符串中的每個字符為“(”(如果該字符在原始字符串中僅出現(xiàn)一次)或“)”(如果該字符在原始字符串中出現(xiàn)多次)細(xì)繩。在確定字符是否重復(fù)時忽略大寫。但是當(dāng)代碼遇到 ) -- 右括號時,它會產(chǎn)生錯誤的輸出。正如我發(fā)現(xiàn)的那樣,問題出在正則表達(dá)式上,但我不知道如何修復(fù)代碼。from collections import Counterdef duplicate_encode(word):    counter = Counter(word.lower())    counter2 = dict.copy(counter)    print(counter2)    for k,v in counter2.items():        if counter2[k]==1:            counter2[k]='('        else:            counter2[k]=')'      for key in counter2.keys():        word = str(word.lower()).replace(key, str(counter2[key]))         return word例如:duplicate_encode('yy! R)!QvdG')應(yīng)該回來)))((()((((但我得到了(((((((((((。
查看完整描述

3 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗 獲得超3個贊

另一個解決方案。從一組(唯一的)字符開始,遍歷字符串中的字符,從該組中刪除字符,如果它已經(jīng)被刪除,那么它必須是重復(fù)的。使用它來構(gòu)建一組重復(fù)的字符。


def duplicate_encode(word):

    word = word.upper()

    s = set(word)

    dups = set()


    for char in word:

        if char in s:

            s.remove(char)

        else:

            dups.add(char)


    return "".join(")" if char in dups else "("

                   for char in word)


print(duplicate_encode("'yy! R)!QvdG'"))

給出:


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


查看完整回答
反對 回復(fù) 2023-03-16
?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗 獲得超6個贊

from collections import Counter



def duplicate_encode(word):

    res = list(word.lower())

    counter = Counter(word.lower())

    counter2 = dict.copy(counter)


    print(counter2)


    for k, value in enumerate(res):

        if counter2[value] == 1:

            res[k] = '('

        else:

            res[k] = ')'


    # for key in counter2.keys():

    #     word = str(word.lower()).replace(key, str(counter2[key]))


    return "".join(res)



res = duplicate_encode('yy! R)!QvdG')

print("res", res)


查看完整回答
反對 回復(fù) 2023-03-16
?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊

(當(dāng)您的輸入字符串包含大括號(如or )時,就會出現(xiàn)問題)。

當(dāng)發(fā)生這種情況時,就像在您的示例中一樣,錯誤的字符將被替換,您可以通過print()在代碼中添加語句來驗證,每次更改時word。

我已經(jīng)在你的代碼中替換了那部分。


from collections import Counter


def duplicate_encode(word):

    counter = Counter(word.lower())

    counter2 = dict.copy(counter)


    print(counter2)


    for k,v in counter2.items():

        if counter2[k]==1:

            counter2[k]='('

        else:

            counter2[k]=')'

    

    print(counter2)

    

    new_word = ''

    for character in word:

        new_word += counter2[character.lower()]

    

    return new_word

但是請注意,該代碼有很多冗余和不必要的內(nèi)存使用。它可以簡化為以下內(nèi)容:


from collections import Counter


def duplicate_encode(word):

    lower_word = word.lower()

    new_word = ''

    counter = Counter(lower_word)

    

    for char in lower_word:

        if counter[char] > 1:

            new_word += ')'

        else:

            new_word += '('

    

    return new_word


查看完整回答
反對 回復(fù) 2023-03-16
  • 3 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號