gpt4 book ai didi

python - 将元组列表转换为字典,给每个元组一个不同的键

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:11 25 4
gpt4 key购买 nike

我有一个由以特定方式排序的 x、y 坐标组成的元组列表,我想将其转换为字典,其中每个元组都有一个不同的键。

我应该怎么做?如果名称不可行,数字也可以。最终目标是将所有不同的点绘制为类别。

# list of tuples
ordered_points = [(1188.0, 751.0),(1000.0, 961.0),(984.0, 816.0),(896.0, 707.0),(802.0, 634.0),(684.0, 702.0),(620.0, 769.0)]


# what I want
orderder_points_dict = {'pointing finger':(1188.0, 751.0), 'middle finger':(1000.0, 961.0) etc...}

最佳答案

如果你有兴趣,只用数字作为索引,你可以使用enumerate来做到这一点

>>> ordered_points = [(1188.0, 751.0),(1000.0, 961.0),(984.0, 816.0),(896.0, 707.0),(802.0, 634.0),(684.0, 702.0),(620.0, 769.0)]
>>>
>>> dict(enumerate(ordered_points))
{0: (1188.0, 751.0), 1: (1000.0, 961.0), 2: (984.0, 816.0), 3: (896.0, 707.0), 4: (802.0, 634.0), 5: (684.0, 702.0), 6: (620.0, 769.0)}

或者如果您将键放在单独的列表中,

>>> keys
['key0', 'key1', 'key2', 'key3', 'key4', 'key5', 'key6']
>>>
>>> dict(zip(keys,ordered_points))
{'key0': (1188.0, 751.0), 'key1': (1000.0, 961.0), 'key2': (984.0, 816.0), 'key3': (896.0, 707.0), 'key4': (802.0, 634.0), 'key5': (684.0, 702.0), 'key6': (620.0, 769.0)}
>>>

关于python - 将元组列表转换为字典,给每个元组一个不同的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025634/

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