Question

I have 2 arrays and I need to switch the last digit of the integers in one array with the integers in another. Its better if I show you the output to get a better understanding of what I'm trying to do. I'm not sure this is even possible to do at the least.

Output of arrays:

first_array=['3', '4', '5', '2', '0', '0', '1', '7']
second_array=['527', '61', '397', '100', '97', '18', '45', '1']

What it then look like:

first_array=['3', '4', '5', '2', '0', '0', '1', '7']
second_array =['523', '64', '395', '102', '90', '10', '41', '7']
Was it helpful?

Solution

>>> [s[:-1]+f for (f,s) in zip(first_array, second_array)]
['523', '64', '395', '102', '90', '10', '41', '7']

OTHER TIPS

If it is actual integers, you could try "rounding down" each element of the second list to nearest multiple of 10, then adding each element from the first list. For example:

>>> first = [3,4,5,6]
>>> second = [235,123,789,9021]
>>> second = [x - (x%10) for x in second]   
>>> second
[230, 120, 780, 9020]
>>> [x + y for (x,y) in zip(first, second)]
[233, 124, 785, 9026]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top