Search code examples
pythonlistcopytriangle-count

How I can copy triangle shape numbers in Python


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.


Solution

  • 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.