python - Why this statement doesnt work? -
why following list comprehension expression doesnt work?
[col1*col2 (col1, col2) in zip(row1, row2) (row1, row2) in zip(m,n)]
python says:
nameerror: name 'row1' not defined
with:
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] n = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]
indeed, row1
not defined , row2
.
let's simplify you're trying do:
for row1, row2 in zip(m, n): col1, col2 in zip(row1, row2): result = col1*col2 print result
the above code works fine because first selected row1
, row2
zip(m, n)
. , selected col1
, col2
zip(row1, row2)
.
so if want compress code in 1 single line, will have follow same approach above. code below:
[col1*col2 (row1, row2) in zip(m, n) (col1, col2) in zip(row1, row2)]
Comments
Post a Comment