0

I am solving some questions in Python and I am beginner level. I wonder how I can copy number that I found on internet. When I try to copy triangle shape number like:

75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 I added the shape

I cant work with them in Python.It gives error invalid syntax on end of second line. I need to turn them to a list. In the example it is short enough to do myself but it can be 1000 numbers. Is there any function or something for do that. I know it is so easy think to ask but I cannot find it and I am beginner.

GAThrawn
  • 5
  • 1
  • 1
    What have you attempted? – ccl May 06 '20 at 15:24
  • I am trying to solve Project Euler Prob. 18 and I need the numbers of triangle in my Idle to coding it is 15 line triangle and problem 67 will be 100 I can't separate all number with "," myself is there any simple way to do that. – GAThrawn May 06 '20 at 15:39

1 Answers1

0
import re

string = r"""
23 17\n
98 """


ans = re.findall("\d+", string, re.MULTILINE)
list_int = [int(i) for i in ans]

print(ans) # ['23', '17', '98']
print(list_int) # [23, 17, 98]

I think you could try something like that, because it appears some text encoding problem, and with this snippet you can get just numbers from a copy and paste string.

  • Thank you it will be enough to go on with for now but still I need to write /n to end of lines. When numbers of the line will be 100 or 1000 it will be same situation again. – GAThrawn May 06 '20 at 15:45
  • I don't understand, if you could explain in a different way, maybe with some example I could understand it better. And if you want to keep `\n` you could change regex to `"\d+\n*"` – Rodrigo Moraes May 07 '20 at 19:13
  • 1
    the last thing you mentioned is enough thank you for your help. – GAThrawn May 10 '20 at 14:13