gpt4 book ai didi

python - 在字典中追加多个值,但避免python中字典值中的重复元素

转载 作者:行者123 更新时间:2023-12-04 00:19:38 25 4
gpt4 key购买 nike

我有这样的列表:

[('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0018_RIGHT_CC', [(1388, 1653), (1894, 2197)]),
('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)])]

从这里我想创建一个这样的字典.. 例如;

0106_LEFT_CC: [[(911, 1135), (0, 86)], [(1091, 1198), (65, 160)], [(1679, 1833), (964, 1102)]]

请注意,应避免在字典的value 中出现重复的元素。我做了如下;

from collections import defaultdict

d = defaultdict(list)
for key, value in data:
d[key].append(value)

我得到了以下结果。

defaultdict(list,
{'0023_RIGHT_CC': [[(2574, 2798), (1324, 1545)],
[(2574, 2798), (1324, 1545)]],
'0021_LEFT_CC': [[(1180, 1420), (883, 1140)],
[(1180, 1420), (883, 1140)]],
'0106_LEFT_CC': [[(911, 1135), (0, 86)],
[(1679, 1833), (964, 1102)],
[(1091, 1198), (65, 160)],
[(911, 1135), (0, 86)],
[(1679, 1833), (964, 1102)],
[(1091, 1198), (65, 160)]],
'0026_LEFT_CC': [[(3738, 3968), (2144, 2352)],
[(3738, 3968), (2144, 2352)]],
'0021_RIGHT_CC': [[(2170, 2314), (1642, 1795)],
[(2170, 2314), (1642, 1795)]],
'0018_RIGHT_CC': [[(1388, 1653), (1894, 2197)]]})

但是结果显示,例如,在 key 0106_LEFT_CC 中,一些 values 重复了。如何避免它。

最佳答案

您可以将它们转换成一个set,然后再转换成一个list。这将使您摆脱重复的元素。

from collections import defaultdict

data = [('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0018_RIGHT_CC', [(1388, 1653), (1894, 2197)]),
('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)])]

d = defaultdict(list)
for key, value in data:
d[key] = list(set(d[key] + value))


编辑:我稍微更改了@JST99 的答案,现在它使用set 进行in 检查,然后将值添加到列表.这在大型数据集上更快,但对于您当前的数据,@JST99 的答案更快。

from collections import defaultdict


data = [('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0021_LEFT_CC', [(1180, 1420), (883, 1140)]),
('0021_RIGHT_CC', [(2170, 2314), (1642, 1795)]),
('0106_LEFT_CC', [(911, 1135), (0, 86)]),
('0106_LEFT_CC', [(1679, 1833), (964, 1102)]),
('0106_LEFT_CC', [(1091, 1198), (65, 160)]),
('0018_RIGHT_CC', [(1388, 1653), (1894, 2197)]),
('0023_RIGHT_CC', [(2574, 2798), (1324, 1545)]),
('0026_LEFT_CC', [(3738, 3968), (2144, 2352)])]


d = defaultdict(list)
s = defaultdict(set)
for key, value in data:
s_value = tuple(value) # lists are unhasable, so we will use tuple
if s_value not in s[key]:
d[key].append(value)
s[key].add(s_value)

del s # we dont need s anymore

关于python - 在字典中追加多个值,但避免python中字典值中的重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61514837/

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