Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Performing an index operation in Python

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)],
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading