It is necessary to unpack the line in the list. How to do it right?
mylist = [] stroka = "abcdef" *mylist = stroka like this
It is necessary to unpack the line in the list. How to do it right?
mylist = [] stroka = "abcdef" *mylist = stroka like this
Actually, if you carefully read the error message when using the expression
>>> s = "string" >>> *l = s *l = s ^ SyntaxError: starred assignment target must be in a list or tuple it can be understood that it is enough to have a left tuple or list:
>>> *l, = s >>> l ['s', 't', 'r', 'i', 'n', 'g'] >>> [*l] = s >>> l ['s', 't', 'r', 'i', 'n', 'g'] Since in Python in many cases you can create a tuple using only a comma, *l, = s simply a short form of the expression (*l, ) = s
By the way, in python you can work with strings as with an array:
In [2]: s = "Hello, world!" In [3]: s [1] # Already almost an array! Out [3]: 'e' In [4]: s [1: 4] Out [4]: 'ell' In [5]: mylist = list (s) # Here's what you need In [6]: mylist Out [6]: ['H', 'e', 'l', 'l', 'o', ',', '', 'w', 'o', 'r', 'l', ' d ','! '] In [8]: type (mylist) Out [8]: builtins.list # Array! In [9]: type (s) Out [9]: builtins.str # String!
Source: https://ru.stackoverflow.com/questions/606843/
All Articles
mylist = list(stroka)? - MaxU