Python print nth element from list of lists -
this question has answer here:
- how print column in python array? 2 answers
i have following list:
[[50.954818803035948, 55.49664787231189, 8007927.0, 0.0], [50.630482185654436, 55.133473852776916, 8547795.0, 0.0], [51.32738085400576, 55.118344981379266, 6600841.0, 0.0], [49.425931642638567, 55.312890225131163, 7400096.0, 0.0], [48.593467836476407, 55.073137270550006, 6001334.0, 0.0]]
i want print third element every list. desired result is:
8007927.0 8547795.0 6600841.0 7400096.0 6001334.0
i tried:
print data[:][2]
but not outputting desired result.
many way this. here's simple list way, without explict loop.
tt = [[50.954818803035948, 55.49664787231189, 8007927.0, 0.0], [50.630482185654436, 55.133473852776916, 8547795.0, 0.0], [51.32738085400576, 55.118344981379266, 6600841.0, 0.0], [49.425931642638567, 55.312890225131163, 7400096.0, 0.0], [48.593467836476407, 55.073137270550006, 6001334.0, 0.0]] print [x[2] x in tt] > [8007927.0, 8547795.0, 6600841.0, 7400096.0, 6001334.0]
and making safe potentially shorted lists
print [x[2] x in tt if len(tt) > 3]
more sophisticated output (python 2.7), prints values newline (\n) seperated
print '\n'.join([str(x[2]) x in tt]) > 8007927.0 > 8547795.0 > 6600841.0 > 7400096.0 > 6001334.0
Comments
Post a Comment