【每日一题1210】对位替换

:mage:t2: 给定一段文本words:“天_青_色_等_烟_雨,_而_我_在_等_你”,请编写一个函数,接收两个文字一一对应的字符串 sentence1 和 sentence2,请把words中的与sentence1的相同汉字换成sentence2对应的汉字。

示例:
输入:sentence1 = “天青色等烟雨”,sentence2 = “炊烟袅袅升起”,
输出:“炊_烟_袅_袅_升_起,_而_我_在_袅_你”

题目难度:简单
题目来源:蚊子

def solution(before, after) -> str:
    # your code

assert solution("天青色等烟雨",  "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
words = "天_青_色_等_烟_雨,_而_我_在_等_你"


def solution(before, after) -> str:
    new_list = list(words)
    for j in range(len(words)):
        for i in range(len(before)):
            if words[j] == before[i]:
                new_list[j] = after[i]
    return ''.join(new_list)


assert solution("天青色等烟雨", "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
#该方法存在局限性,因为字典的key的唯一性
def solution(before,after):
    words="天_青_色_等_烟_雨,_而_我_在_等_你"
    temp_dict={}
    result=""
    for i in range(len(before)):
        temp_dict[before[i]]=after[i]
    for word in words:
        if word in before:
            result=result+temp_dict[word]
        else:
            result=result+word
    return result

assert solution("天青色等烟雨",  "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
def solution(before: str, after: str) -> str:
    # your code
    words = "天_青_色_等_烟_雨,_而_我_在_等_你"
    w = list(words)
    for i in range(len(w)):
        if w[i] in before:
            w[i] = after[before.find(w[i])]
    words = "".join(w)
    return words


assert solution("天青色等烟雨",  "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
def solution(before, after) -> str:
    words='天_青_色_等_烟_雨,_而_我_在_等_你'
    words_list=list(words)
    for i in range(len(words_list)):
        for j in range(len(before)):
            if words_list[i]==before[j]:
                words_list[i]=after[j]
                break
    return ''.join(words_list)

assert solution("天青色等烟雨",  "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
def solution(before, after) -> str:
    words = "天_青_色_等_烟_雨,_而_我_在_等_你"
    charD = dict(zip(list(before),list(after)))
    return "".join([charD[item] if item in charD else item for item in list(words) ])

assert solution("天青色等烟雨",  "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"
1 个赞
def solution(before, after) -> str:
    str1 = "天_青_色_等_烟_雨,_而_我_在_等_你"
    for i in range(len(before)):
        str1 = str1.replace(before[i],before[i]+str(i))
    for i in range(len(after)):
        str1 = str1.replace(before[i]+str(i),after[i])
    return str1
def solution(before, after) -> str:
    words = "天_青_色_等_烟_雨, _而_我_在_等_你"
    result = ""
    trans = dict(zip(before, after))

    for str_i in words:
        if str_i in trans.keys():
            result += trans[str_i]
        else:
            result += str_i
    return result
def solution(before, after) -> str:
    words = "天_青_色_等_烟_雨,_而_我_在_等_你"
    data_dict = dict(zip(before, after))
    result = ""
    for str_i in words:
        if str_i in data_dict.keys():
            result = result + data_dict.get(str_i)
        else:
            result = result + str_i
    return result


assert solution("天青色等烟雨", "炊烟袅袅升起") == "炊_烟_袅_袅_升_起,_而_我_在_袅_你"

def solution(before, after) → str:
before_list = list(before)
after_list = list(after)
words_list = list(words)
for index, info in enumerate(words_list):
if info in before_list:
words_list[index] = after_list[before_list.index(info)]
return ‘’.join(words_list)

words = ‘天_青_色_等_烟_雨,_而_我_在_等_你’
assert solution(“天青色等烟雨”, “炊烟袅袅升起”) == “炊_烟_袅_袅_升_起,_而_我_在_袅_你”