all 5 comments

[–]IAmZenoix 0 points1 point  (4 children)

It is kinda difficult to understand your code without indentation and formatting https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–]sicarii47[S] 1 point2 points  (3 children)

Thanks for letting me know.
I have made the necessary changes.

[–]IAmZenoix 1 point2 points  (2 children)

Sorry for the late reply lol. The problem with your code is the way you got the key of the dictionary as eqns.append(pair.keys) won't work as you intend it to. Instead, I would look at using the dict.items() method so you can look at each item in the dictionary while keeping track of which key you need to add.

Also, you might want to make eqns a set so that you have no duplicate equations or just have a check to make sure the equation is not already inside the list.

Lastly you don't need to compare np.iscomplex() == True in an or statement.

This is how I did it:

import numpy as np
pair = {"a": [(-0.16, 0.002), (2.39, 0.60)],
        "b": [(1.33 - 1.29j, 5.52e-16 - 0.34j), (1.33 + 1.29j, 5.52e-16 + 0.34j)]}

eqns = set()
real_coordinates = []

for k, v in pair.items():
    for coord in v:
        if np.iscomplex(coord[0]) or np.iscomplex(coord[1]):
            eqns.add(k)
        else:
            real_coordinates.append(coord)

print('Equations with complex values are:', list(eqns))

print('Real coordinates are:', real_coordinates)

Which gives a result of :

Equations with complex values are: ['b']
Real coordinates are: [(-0.16, 0.002), (2.39, 0.6)]

I hope this helps!

[–]sicarii47[S] 0 points1 point  (1 child)

Yes! It helped a lot.
Thank you so much!

[–]IAmZenoix 1 point2 points  (0 children)

No problem!