清单与其他6个列表的最大交叉点

发布于 2025-01-20 20:19:31 字数 2174 浏览 2 评论 0原文

所以我有 2 个列表,其中一个如下:

a = [[('e', 18.019570565395412), ('n', 9.987254997438297), 
      ('a', 7.7558209941102), ('r', 6.6337526622659), 
      ('i', 6.600725745328597), ('t', 6.501644994516685), 
      ('o', 6.348366226166633), ('d', 5.847034563938841), 
      ('s', 4.446777970199559), ('l', 3.72314975166299)], 
     [('e', 12.106222485002089), ('t', 8.957697044103082), 
      ('a', 8.370584890241286), ('n', 7.607979302319451), 
      ('o', 7.490104957124618), ('i', 7.3906837841807365), 
      ('s', 6.619604800837547), ('r', 6.519995330217634), 
      ('h', 4.5520963180272425), ('l', 4.174559477586928)], 
     [('e', 17.355137469595004), ('s', 8.143220837795097), 
      ('a', 7.8767560437690145), ('n', 7.549126676263891), 
      ('i', 7.163346641798983), ('t', 7.009814697935651), 
      ('r', 6.939253827661279), ('l', 5.823753838298597), 
      ('u', 5.566685341067845), ('o', 5.494351584605674)], 
     [('e', 11.726720365453488), ('i', 11.143857839435189),
      ('a', 10.481789164283027), ('o', 8.879509290276063), 
      ('n', 7.433536567715994), ('l', 6.861989205859677), 
      ('t', 6.660947684470068), ('r', 6.473326474063275), 
      ('s', 5.332336897171472), ('c', 4.1076677341515335)], 
     [('e', 16.01393585408341), ('n', 10.010042012501282), 
      ('i', 7.874987191310585), ('r', 7.499538887181063), 
      ('a', 6.538374833487037), ('s', 6.393687877856339), 
      ('t', 6.1842401885439084), ('d', 5.152577108310278), 
      ('u', 4.3455272056563174), ('l', 3.962701096423814)], 
     [('e', 13.02338360095244), ('a', 11.820318700775383), 
      ('o', 9.20172171683253), ('s', 7.635081506807498), 
      ('n', 7.547469320471335), ('i', 7.219915745772025), 
      ('r', 6.704927040722877), ('l', 5.650833384211491), 
      ('d', 5.098296599303987), ('t', 4.7109103119848585)]]

这是一个包含 6 个子列表的列表 我的另一个列表是:

b = {'e': 1636, 'a': 930, 'd': 581, 'i': 507, 'g': 298, 'h': 222, 'c': 145, 'b': 117, 'j': 104, 'f': 74}

我需要一些执行以下操作的代码:

  1. 告诉我 a 的哪个子列表与 b 具有最大的交集

  2. 打印该子列表的索引

重要说明:我只关心每个列表中的键(字符串),值不必匹配。所以通过交集我真正的意思是:列表 b 与 a 的每个子列表有多少个共同的键。

so i have 2 lists, one is the following:

a = [[('e', 18.019570565395412), ('n', 9.987254997438297), 
      ('a', 7.7558209941102), ('r', 6.6337526622659), 
      ('i', 6.600725745328597), ('t', 6.501644994516685), 
      ('o', 6.348366226166633), ('d', 5.847034563938841), 
      ('s', 4.446777970199559), ('l', 3.72314975166299)], 
     [('e', 12.106222485002089), ('t', 8.957697044103082), 
      ('a', 8.370584890241286), ('n', 7.607979302319451), 
      ('o', 7.490104957124618), ('i', 7.3906837841807365), 
      ('s', 6.619604800837547), ('r', 6.519995330217634), 
      ('h', 4.5520963180272425), ('l', 4.174559477586928)], 
     [('e', 17.355137469595004), ('s', 8.143220837795097), 
      ('a', 7.8767560437690145), ('n', 7.549126676263891), 
      ('i', 7.163346641798983), ('t', 7.009814697935651), 
      ('r', 6.939253827661279), ('l', 5.823753838298597), 
      ('u', 5.566685341067845), ('o', 5.494351584605674)], 
     [('e', 11.726720365453488), ('i', 11.143857839435189),
      ('a', 10.481789164283027), ('o', 8.879509290276063), 
      ('n', 7.433536567715994), ('l', 6.861989205859677), 
      ('t', 6.660947684470068), ('r', 6.473326474063275), 
      ('s', 5.332336897171472), ('c', 4.1076677341515335)], 
     [('e', 16.01393585408341), ('n', 10.010042012501282), 
      ('i', 7.874987191310585), ('r', 7.499538887181063), 
      ('a', 6.538374833487037), ('s', 6.393687877856339), 
      ('t', 6.1842401885439084), ('d', 5.152577108310278), 
      ('u', 4.3455272056563174), ('l', 3.962701096423814)], 
     [('e', 13.02338360095244), ('a', 11.820318700775383), 
      ('o', 9.20172171683253), ('s', 7.635081506807498), 
      ('n', 7.547469320471335), ('i', 7.219915745772025), 
      ('r', 6.704927040722877), ('l', 5.650833384211491), 
      ('d', 5.098296599303987), ('t', 4.7109103119848585)]]

which is a list containing 6 sub-lists
the other list i have is:

b = {'e': 1636, 'a': 930, 'd': 581, 'i': 507, 'g': 298, 'h': 222, 'c': 145, 'b': 117, 'j': 104, 'f': 74}

i need some code that does the following:

  1. tells me which sub-list of a has the biggest intersection with b

  2. prints the index of that sub-list

important remark: i only care about the keys in each list(the strings), the values dont have to match up. so by intersection i really mean: how many keys does the list b have in common with each sub-list of a.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2025-01-27 20:19:31

您可以将 a 中的子列表转换为字典,将 b 转换为字典;然后在字典键上使用 set.intersection 来获取交集的大小:

intersection_sizes = [len(dict(s).keys() & dict(b).keys()) for s in a]

输出:

[10, 9, 9, 9, 9, 10]

然后另一个列表理解来刷新具有最大交集的子集的索引:

index_of_the_subsets = [i for i, x in enumerate(intersection_sizes) if x == max(intersection_sizes)]

输出:

[0, 5]

You could convert the sublists in a into dictionaries and b into a dictionary; then use set.intersection on the dictionary keys to get the size of the intersections:

intersection_sizes = [len(dict(s).keys() & dict(b).keys()) for s in a]

Output:

[10, 9, 9, 9, 9, 10]

Then another list comprehension to flush out the index of subsets with the largest intersection:

index_of_the_subsets = [i for i, x in enumerate(intersection_sizes) if x == max(intersection_sizes)]

Output:

[0, 5]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文