Change the contents of the array

Viewed 56

I'm very confused how to turn it into an asterisk(*) symbol

Inputs in array:

array = [12345, 12354, 12334]

Outputs in array:

['123**', '123**', '123**']
3 Answers

You could just do, map to cast to string followed by string splicing then ljust to fill the missing values based on length.

input_ = [12345, 12354, 12334]

[i[:3].ljust(len(i), "*") for i in map(str, input_)]

['123**', '123**', '123**']

You can apply zip twice with 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)))

Output

['123**', '123**', '123**']

I can't fully understand the task, so I suppose it's asking to turn the last two digits into asterisks. In order to do so, you have to turn the elements into strings.

[str(n)[:-2]+"**" for n in list]

This will cut off the last two digits of the number.

Related