python - String assignment in a list in a 'for' loop -
I have a list ['1', '2', '3', '4'] < / Code> and I want to convert it to:
['1.0', '2.0', '3.0', '4.0']
. In the code below, why does the second attempt work and not the first?
& gt; & Gt; & Gt; List = ['1', '2', '3', '4'] >> gt; & Gt; For element in the list: ... element = element + '.0' ... & gt; & Gt; & Gt; Print (list) ['1', '2', '3', '4']> gt; & Gt; & Gt; For element in element (lane (list)): ... list [element] = list [element] + '.0' ... ... gt; & Gt; & Gt; Print (list) ['1.0', '2.0', '3.0', '4.0']
element
is the local variable and only the current element of the list is specified.
In the second case list [element]
is assigned to indicate the current indicator of the element
list in the list.
Comments
Post a Comment