其他分享
首页 > 其他分享> > 【每日一题20220615】字符编号

【每日一题20220615】字符编号

作者:互联网

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

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

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

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:
    new_list = []
    for i, element  in enumerate(msg, start=1):
        obj = str(i) + ": " + element
        new_list.append(obj)
    return new_list

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

  

标签:字符,20220615,list,solution,assert,msg,序号,new,一题
来源: https://www.cnblogs.com/wwwwtt/p/16378904.html