I want to perform an index operation using the list I5 to create a new list I6. However, there seems to be an error in notation.
This is the logic
I6=[[(I5[0][0]),(I5[0][1]-I5[0][0])],[(I5[1][0]),(I5[1][1]-I5[1][0])],...]
The code is
I5 = [[(0.5, -0.5), (1.5, -0.5)], [(0.5, -0.5), (0.5, -1.5)], [(1.5, -0.5), (1.5, -1.5)], [(0.5, -1.5), (1.5, -1.5)]]
for i in range (0,len(I5)):
I6=I5[i][0]-I5[i][1]
print(I6)
The error is
in <module>
I6=I5[i][0]-I5[i][1]
TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'
The expected output is
I6 = [[(0.5, -0.5), (1, 0)], [(0.5, -0.5), (0, -1)], [(1.5, -0.5), (0, -1)], [(0.5, -1.5), (1, 0)]]
>Solution :
Try:
I5 = [
[(0.5, -0.5), (1.5, -0.5)],
[(0.5, -0.5), (0.5, -1.5)],
[(1.5, -0.5), (1.5, -1.5)],
[(0.5, -1.5), (1.5, -1.5)],
]
I6 = []
for i in range(0, len(I5)):
I6.append(
[I5[i][0], (I5[i][1][0] - I5[i][0][0], I5[i][1][1] - I5[i][0][1])]
)
print(I6)
Prints:
[
[(0.5, -0.5), (1.0, 0.0)],
[(0.5, -0.5), (0.0, -1.0)],
[(1.5, -0.5), (0.0, -1.0)],
[(0.5, -1.5), (1.0, 0.0)],
]