从复数列表中删除非共轭
Remove non-conjugates from complex numbers list
我有两个列表,一个包含虚数的实部,另一个包含相同数字的虚部。我想从两个列表中删除没有共轭的虚数。
例如,下面的列表x = [3, 4, 2, 7, 4]
和y = [2, -1, 0, 6, 1]
表示数字:
3 + 2j <- no conjugate (to remove)
4 - 1j <- conjugate (to keep)
2 + 0j <- real (to keep)
4 + 1j <- conjugate (to keep)
7 + 6j <- no conjugate (to remove)
预期结果如下:
new_x = [4, 2, 4]
new_y = [-1, 0, 1]
知道如何实现吗?谢谢
此脚本将从列表 x
和 y
:
中找到复共轭
x = [3, 4, 2, 7, 4]
y = [2, -1, 0, 6, 1]
tmp = {}
for r, i in zip(x, y):
tmp.setdefault(i, set()).add(r)
x_out, y_out = [], []
for r, i in zip(x, y):
if i==0 or r in tmp.get(-i, []):
x_out.append(r)
y_out.append(i)
print(x_out)
print(y_out)
打印:
[4, 2, 4]
[-1, 0, 1]
我有两个列表,一个包含虚数的实部,另一个包含相同数字的虚部。我想从两个列表中删除没有共轭的虚数。
例如,下面的列表x = [3, 4, 2, 7, 4]
和y = [2, -1, 0, 6, 1]
表示数字:
3 + 2j <- no conjugate (to remove)
4 - 1j <- conjugate (to keep)
2 + 0j <- real (to keep)
4 + 1j <- conjugate (to keep)
7 + 6j <- no conjugate (to remove)
预期结果如下:
new_x = [4, 2, 4]
new_y = [-1, 0, 1]
知道如何实现吗?谢谢
此脚本将从列表 x
和 y
:
x = [3, 4, 2, 7, 4]
y = [2, -1, 0, 6, 1]
tmp = {}
for r, i in zip(x, y):
tmp.setdefault(i, set()).add(r)
x_out, y_out = [], []
for r, i in zip(x, y):
if i==0 or r in tmp.get(-i, []):
x_out.append(r)
y_out.append(i)
print(x_out)
print(y_out)
打印:
[4, 2, 4]
[-1, 0, 1]