gpt4 book ai didi

python - 将 (x, y) 坐标的元组对设置为 dict 作为带有 id 值的键

转载 作者:行者123 更新时间:2023-12-02 18:12:57 24 4
gpt4 key购买 nike

数据如下所示:

d = {'location_id': [1, 2, 3, 4, 5], 'x': [47.43715, 48.213889, 46.631111, 46.551111, 47.356628], 'y': [11.880689, 14.274444, 14.371, 13.665556, 11.705181]}
df = pd.DataFrame(data=d)

print(df)
location_id x y
0 1 47.43715 11.880689
1 2 48.213889 14.274444
2 3 46.631111 14.371
3 4 46.551111 13.665556
4 5 47.356628 11.705181

预期输出:

{(47.43715, 11.880689): 1, (48.213889, 14.274444): 2, (46.631111, 14.371): 3, ...}

所以我可以简单地访问提供点坐标的ID。

我尝试过:

dict(zip(df['x'].astype('float'), df['y'].astype('float'), zip(df['location_id'])))
Error: ValueError: dictionary update sequence element #0 has length 3; 2 is required

or

dict(zip(tuple(df['x'].astype('float'), df['y'].astype('float')), zip(df['location_id'])))
TypeError: tuple expected at most 1 arguments, got 2

我用谷歌搜索了一段时间,但不太清楚。感谢您的帮助。

最佳答案

我认为是这样

result = dict(zip(zip(df['x'], df['y']), df['location_id']))

应该给你你想要的东西?结果:

{(47.43715, 11.880689): 1,
(48.213889, 14.274444): 2,
(46.631111, 14.371): 3,
(46.551111, 13.665556): 4,
(47.356628, 11.705181): 5}

关于python - 将 (x, y) 坐标的元组对设置为 dict 作为带有 id 值的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72025855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com