-3

I have a list with numbers like this :-

s = [5542, 5654, 7545]

The goal is to remove the first 5 from the number such that the resultant list is like this

s = [542, 654, 745]

What's the best way to achieve the following without using any external libraries?

0m3r
  • 12,286
  • 15
  • 35
  • 71
Somethingwhatever
  • 1,390
  • 4
  • 32
  • 58
  • 1
    Convert each number to a string, check if the digit `'5'` is in the string, if so, remove it from the string, and convert the string back to an integer. What is the difficulty? – John Gordon Jan 08 '23 at 22:06
  • @JohnGordon Well, Thinking of the best approach. :) – Somethingwhatever Jan 08 '23 at 22:08
  • 1
    Please read [ask] and [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822), and try to break problems down into logical steps before asking. For example, if you can solve the problem for a single input integer, do you see how to repeat that logic for the entire list? If not, then that is a separate question to [research](https://meta.stackoverflow.com/questions/261592/); if so, then it is a separate question to **omit** from the [mre]. Aside from that, what do you think it actually **means** to "remove a digit" from the integer? – Karl Knechtel Jan 09 '23 at 03:53
  • 1
    If, for example, you have understood that integers **do not have** digits (nor bits, nor nybbles), and that only a **textual representation of** an integer has those things, then you might think in terms of converting to and from a string. Each conversion is a separate question (and common duplicates at that); the string processing is arguably two questions (how to figure out where the `5` is, and how to remove a text character at that position). – Karl Knechtel Jan 09 '23 at 03:55
  • 1
    On the other hand, if you are thinking in terms of place-value arithmetic, then figuring out each numeric digit **value** is an **arithmetic** question. That, too, is a common duplicate. In fact, both digit extraction methods are addressed in [How to take the nth digit of a number in python](https://stackoverflow.com/questions/39644638). – Karl Knechtel Jan 09 '23 at 03:57

2 Answers2

0

Try this with str.replace(old, new, count) -

[int(str(i).replace('5','',1)) for i in s]
[542, 654, 745]

The str.replace(old, new, count) in this case has 3 parameters, where the count set to 1 will only replace the first instance of 5 it finds in the string.

Then you can convert it back to an integer.

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • 2
    Please read [ask] and [answer], and do not allow others to treat the site as a code-writing service. It would be better to go somewhere that you would at least get paid for it. If a problem obviously and directly breaks down into separate logical steps, then it inherently "Needs More Focus" and should be closed, not answered. – Karl Knechtel Jan 09 '23 at 03:59
0

Another solution:

s = [int(str(x)[:str(x).index("5")] + str(x)[str(x).index("5") + 1:]) for x in s]
jesy2013
  • 319
  • 2
  • 12