【每日一题20220615】字符编号

:mage:‍ 给定一个字符串列表,请根据每个元素的出现顺序进行编号,从1开始,返回编号后的字符串列表。编号元素的格式是:序号:元素

【示例】
输入:["a", "b", "c"]
输出:["1: a", "2: b", "3: c"]
解释:a的序号是1,b的序号是2,c的序号是3。

题目难度:简单
题目来源:codewars-Testing 1-2-3

def solution(msg: list)-> list:
    # your code here

assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def solution(msg: list)-> list:
    # your code here
    return [f"{i}:{e}" for i, e in enumerate(msg, start=1)]

assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
1 Like

def solution(msg: list) -> list:
    l = []
    for m, n in enumerate(msg):
        l.append(f'{m + 1}:{n}')
    return l

居然是不用重复字符的,是我想多了 :rofl:

def solution(msg: list)-> list:
    result = []
    for i, value in enumerate(msg, start=1):
        result.append("{}: {}".format(i, value))

    return result

assert solution([]) == []
assert solution(["a", "b", "c"]) == ['1: a', '2: b', '3: c']
def solution(msg: list)-> list:
    new_list = []
    for i, element  in enumerate(msg, start=1):
        obj = str(i) + ": " + element
        new_list.append(obj)
    return new_list
def solution(msg: list)-> list:
    # your code here

    return [f"{msg.index(i)+1}:{i}" for i in msg]


if __name__ == '__main__':
    assert solution([]) == []
    assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def solution(msg: list) -> list:
    return [f"{index + 1}:{value}" for index, value in enumerate(msg)]


assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def solution(msg: list)-> list:
    msg_change = []
    for i in range(len(msg)):
        msg_change.append(f"{i+1}:{msg[i]}")
    return msg_change

assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def solution(msg: list)-> list:
    return [f'{index}:{value}' for index, value in enumerate(msg, 1)]
def solution(msg: list)-> list:
    return [f"{msg.index(i)+1}:{i}" for i in msg]

assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
关闭