There is a line:

'45 30 55 20 80 20 '.

You must create an array of numbers included in this line:

[45, 30, 55, 20, 80, 20].

I thought for a long time and only came to this code, but it does not work:

for i in range(len(q)): if q[i]!=(' '): e+=q[i] else: w.append(int(e)) e='' print(w) 

the problem is that the last 20 he does not see, please help

  • 2
    The split () method to split the input string and int () to convert the string to a number. - Vladimir Martyanov
  • Thanks to all. Problem solved - MoRGan

2 answers 2

You can use the list generator:

 s = '45 30 55 20 80 20' a = [int(x) for x in s.split()] 

    UPD. corrected based on comments

     s = '45 30 55 20 80 20' print (list(map(int, s.split()))) 

    [45, 30, 55, 20, 80, 20]

    • Thank you very much, it helped a lot - MorGan
    • four
      @ MoRGAN In Python 3.x map returns an iterator (map object), you can call list() on it to convert to a list - Pavel Karateev
    • @jfs, yes indeed. - iksuy