0

I have a string that contains a random amounts of numbers, these numbers can be like 1, 2, 3 and so, but they can even be 100, 20, 25... so if I'm searching for 14, I will find a 14 in 144, if the string contains it. I Have to split them into a list of integers. Here's what I tried:

def to_list(string):
    result_list = []
    for idx in range(len(string)):
      if string[idx] != "0":
        zeros = 0
        for j in string[idx + 1:len(string)]:
          if j == "0":
            zeros += 1
          else:
            result_list.append(string[idx] + "0" * zeros)
            break

    result_list.append(string[-1])
    return result_list
4
  • so, a "complete" number is any number that ends with 0 or more 0's? Commented Oct 27, 2022 at 17:27
  • You'll need to provide a definition of complete number. Commented Oct 27, 2022 at 17:28
  • yes, sorry for the poor explanation Commented Oct 27, 2022 at 17:28
  • 1
    So, when the loop exits, if zeros is not zero, do your append to arr[-1]. Commented Oct 27, 2022 at 17:29

1 Answer 1

5

You can use regex for that

import re
a = "1001010050101"
a = list(map(int, re.findall(r"[1-9]+0*", a)))
print(a)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.