改变数组的内容
Change the contents of the array
我很困惑如何把它变成星号(*)符号
数组中的输入:
array = [12345, 12354, 12334]
数组中的输出:
['123**', '123**', '123**']
您可以申请 zip
两次 set
:
s = [12345, 12354, 12334]
r = [[i if len(set(b)) == 1 else '*' for i in b] for b in zip(*map(str, s))]
result = list(map(''.join, zip(*r)))
输出
['123**', '123**', '123**']
您可以 map
to cast to string followed by string splicing then ljust
根据长度填充缺失值。
input_ = [12345, 12354, 12334]
[i[:3].ljust(len(i), "*") for i in map(str, input_)]
['123**', '123**', '123**']
我不能完全理解这个任务,所以我想它要求将最后两位数变成星号。
为此,您必须将元素转换为字符串。
[str(n)[:-2]+"**" for n in list]
这将截断号码的最后两位。
我很困惑如何把它变成星号(*)符号
数组中的输入:
array = [12345, 12354, 12334]
数组中的输出:
['123**', '123**', '123**']
您可以申请 zip
两次 set
:
s = [12345, 12354, 12334]
r = [[i if len(set(b)) == 1 else '*' for i in b] for b in zip(*map(str, s))]
result = list(map(''.join, zip(*r)))
输出
['123**', '123**', '123**']
您可以 map
to cast to string followed by string splicing then ljust
根据长度填充缺失值。
input_ = [12345, 12354, 12334]
[i[:3].ljust(len(i), "*") for i in map(str, input_)]
['123**', '123**', '123**']
我不能完全理解这个任务,所以我想它要求将最后两位数变成星号。 为此,您必须将元素转换为字符串。
[str(n)[:-2]+"**" for n in list]
这将截断号码的最后两位。