How to understand the list comprehensions in making list in python? -
i have list of tuples, converted list has elements of list type, since each element list, can insert natural number @ head. let's put:
l = [('c++', 'compiled'), ('python', 'interpreted')] lx = [] z in xrange(len(l)): y = [x x in l[z]] y.insert(0, z) lx.append(y) print lx [[0, 'c++', 'compiled'], [1, 'python', 'interpreted']]
look, job done, works in way. except of followings
neither:
l = [('c++', 'compiled'), ('python', 'interpreted')] lx = [] z in xrange(len(l)): y = [x x in l[z]] lx.append(y.insert(0, z)) print lx [none, none]
nor:
l = [('c++', 'compiled'), ('python', 'interpreted')] lx = [] z in xrange(len(l)): y = [x x in l[z]].insert(0, z) lx.append(y) print lx [none, none]
not mention:
l = [('c++', 'compiled'), ('python', 'interpreted')] lx = [] z in xrange(len(l)): lx.append([x x in l[z]].insert(0, z)) print lx [none, none]
works, why that? noticed such as:
y = [x x in l[z]]
is no one cycle execution in debug step step, beyond impression of expression in other languages.
the insert
method not return anything, in python equivalent of returning none
constant. so, example after line:
y = [x x in l[z]].insert(0, z)
y
always none
. , append lx
, hence result. first snippet correct approach. question has nothing list-comprehensions.
Comments
Post a Comment