аватар question@mail.ru · 01.01.1970 03:00

List of lines to the list of numbers

a - List of lines: ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'] for el int (el)

tried in this way Transform into numbers, but transforms in the framework of this cycle

& nbsp;

аватар answer@mail.ru · 01.01.1970 03:00

The most idiomatic and ideologically verified method:

  Result = [ int  (item)  for  item  in  a]  

for functional style lovers:

  Result =  line  ( map  ( int , a)   

here map applies the function of int to each element of the a object, then the result is converted to the list.

This option is considered less "" "" pythonic "" ", but also has the right to exist (at least, in many cases, recording through map is more compact than the option through the list of expression).


about your first option: when you write for el in a , then each time in the cycle in el the link to the list element is recorded. But when you write el = int (EL) , then another link is simply recorded in the variable el , the list of the list does not change.

How to understand that this is a link, not a copy of the element? Here is an example of how the element itself is changing by calling the list of the list:

   & gt; & gt; & gt;  a = [], [], []]   & gt; & gt; & gt;   for  el  in  a:  el.ppend ( 1 )   & gt; & gt;  a [ 1 ], [ 1 ], [ 1 ]]     

about the bastard (if it is suddenly necessary) On Python, it is more correct (easier and more pleasant, without any LEN invested in Range) through Enumerate:

   For  I, ITEM  in   enumerate  (a):  a [i] =  int  (item)     

& nbsp;

Latest

Similar