【每日一题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 个赞

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"]
def solution(msg: list) -> list:
    # your code here
    # for k, v in enumerate(msg):
    #     msg[k] = f"{k + 1}:{v}"
    # return msg
    return [f"{k+1}:{v}" for k, v in enumerate(msg)]


assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def number(lines):
    return [ '{}: {}'.format(i,v) for i,v in enumerate(lines,start=1)]

def solution(lists:list):
reslut =
for i in lists:
index = lists.index(i)
key = i
value = str(index + 1)
element = (value + ‘:’ + key)
reslut.append(element)
print(reslut)

def solutions(lists:list):
reslut =
for key, value in enumerate(list, start=1):
reslut.append("{}:{}".format(key, value))
print(reslut)

list = [‘a’, ‘b’, ‘c’]
solutions(list)

def solution(msg: list) -> list:
    msg_index = 1
    res_list = []
    for i in msg:
        res_list.append(str(msg_index) + ":" + i)
        msg_index += 1
    return res_list


assert solution([]) == []
assert solution(["a", "b", "c"]) == ["1:a", "2:b", "3:c"]
def solution(msg: list)-> list:
    # your code here
    list_a = []
    for j in msg:
        c_index = msg.index(j) + 1
        list_a.append(f"{c_index}:{j}")
    return list_a
def solution(msg: list)-> list:
    return [f"{i+1}:{j}" for i,j in enumerate(msg)]

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