Avatar ·

Create a list of numbers from a string with numbers separated by spaces

📁 строка, python, работа

There is a string:

'45 30 55 20 80 20'.

It is necessary to create an array of numbers from this string:

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

I thought for a long time and only came up with this code, but it doesn't 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 it doesn't see the last 20, please help

Avatar ·

Off the top of my head (since set, the output elements will be unique, i.e. without duplicates) + it doesn't matter what indices the elements that are in both lists have:

list1 = [u'Вася', u'Петя', u'Маша', u'Саша']   list2 = [u'Вася', u'Петя'] list3=list(set(list1)-set(list2))  print list3  [u'Маша', u'Саша']

Log in to leave an answer

Blogs