我正在尝试编写两个函数来将笛卡尔坐标转换为球坐标,反之亦然。以下是我用于转换的方程式(也可以在此维基百科页面上找到):
和
这是我的spherical_to_cartesian功能:
def spherical_to_cartesian(theta, phi):
    x = math.cos(phi) * math.sin(theta)
    y = math.sin(phi) * math.sin(theta)
    z = math.cos(theta)
    return x, y, z
Run Code Online (Sandbox Code Playgroud)
这是我的cartesian_to_spherical功能:
def cartesian_to_spherical(x, y, z):
    theta = math.atan2(math.sqrt(x ** 2 + y ** 2), z)
    phi = math.atan2(y, x) if x >= 0 else math.atan2(y, x) + math.pi
    return theta, phi
Run Code Online (Sandbox Code Playgroud)
并且,这是驱动程序代码:
>>> t, p = 27.500, 7.500
>>> x, y, z = spherical_to_cartesian(t, p)
>>> print(f"Cartesian coordinates:\tx={x}\ty={y}\tz={z}")
Cartesian coordinates:  x=0.24238129061573832 …Run Code Online (Sandbox Code Playgroud) python geometry polar-coordinates cartesian-coordinates spherical-coordinate
我需要将元素流保存在大小有限的列表中。流中可能有重复的元素,但我需要保留唯一的元素。此外,当列表的大小超过指定限制时,我需要删除最旧的元素并添加新元素。
我已经尝试过set并且list. 问题set是它没有大小限制,如果我想删除最旧的元素,我不知道如何检索它,因为集合是无序的;然而,它解决了唯一性问题。
另一方面,list保持项目的顺序,但每当我想插入新元素时,我都需要检查可能的重复项,这可能会花费大量时间。也list不受尺寸限制set。
我的第三个选择可能是,collections.deque但我不知道它是否保留订单。有什么办法可以保持物品的collections.deque独特性吗?
这些是我的代码示例list:
ids = list()
for item in stream:
    if item not in ids:
        ids.append(item)
    if len(ids) >= len_limit:
        del ids[0]
Run Code Online (Sandbox Code Playgroud)
和set:
ids = set()
for item in stream:
    ids.add(item)
    if len(ids) >= len_limit:
        ids.remove(list(ids)[0])
Run Code Online (Sandbox Code Playgroud)