読者です 読者をやめる 読者になる 読者になる

毎日Learning

学んだことを共有します

P15 (**)リストの各要素を指定回数連続させる。

def repli(lista, num):
    return reduce(lambda l,v: l+[v for i in range(num)], lista, []) if isinstance(lista, list) else None

if __name__ == "__main__":
    print repli("a", 3) == None
    print repli([], 0) == []
    print repli(["a"], 0) == []
    print repli(["a"], 1) == ["a"]
    print repli(["a"], 2) == ["a", "a"]
    print repli(["a", "b", "c"], 3) == ["a", "a", "a", "b", "b", "b", "c", "c", "c"]