I have the following liste
d=[[(1.0,1.1,1223),(2.0,1.1,1224)],[(3.0,1.1,1222),(4.0,1.1,1222)],[(5.0,1.1,1222),(1.0,1.1,1222)]]
I want to obtain the following result using list comprehension:
[[(1.0, 1.1), (2.0, 1.1)], [(3.0, 1.1), (4.0, 1.1)], [(5.0, 1.1), (1.0, 1.1)]]
I have done this
g= [d[i][y][:2] for i in range(len(d)) for y in range(len(d[i]))]
However, i got this output:
[(1.0, 1.1), (2.0, 1.1), (3.0, 1.1), (4.0, 1.1), (5.0, 1.1), (1.0, 1.1)]
Where is the error?
question from:
https://stackoverflow.com/questions/65647267/problem-in-list-comprehension-output-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…