【发布时间】:2021-05-30 12:17:07
【问题描述】:
我一直在从事一项涉及三元组的 NLP 任务,即来自文本语料库的 3 个连续字母的字符串。我有三个列表列表。第一个由两种语言的各种组合常见的频繁出现的三元组组成。第二个由他们在语言 1 中的计数组成。第三个由语言 3 中的计数组成。
我想对这些列表进行排序,将具有最多共同三元组的列表放在顶部。
让我们看一下这些列表的示例:
for i, j, k in zip(trigrams, lang1_counts, lang2_counts):
print(i,j,k)
['er_', 'n_d', '_de', 'in_', 'en_'] [1087, 1213, 2038, 903, 3855] [2996, 969, 2226, 951, 3872]
['in_', '_in', 'er_'] [903, 937, 1087] [1101, 1369, 1080]
['et_', 'de_', '_de', '_en'] [1314, 2359, 2038, 769] [880, 2254, 2881, 787]
如您所见,第一个三元组列表的长度分别为 5,3,4。我想对其进行排序,使其变为 5、4、3。对于绘图,还必须对三元组的计数进行排序。这只是一个小样本;我还有很多这样的清单。列表的列表长度相同。
到目前为止,我已经尝试过这些解决方案,但都不起作用:
trigrams, lang1_counts, lang2_counts = zip(*sorted(zip(trigrams, lang1_counts, lang2_counts), key=len, reverse=True))
trigrams, lang1_counts, lang2_counts = (list(t) for t in zip(*sorted(zip(trigrams, lang1_counts, lang2_counts), key=len, reverse=True)))
任何人都可以看到他们为什么不工作并提出一些建议吗?给定的方法不会引发错误;它们根本没有任何作用。
我的参考资料是:
How to sort list of lists according to length of sublists How to sort two lists (which reference each other) in the exact same way
【问题讨论】:
标签: python python-3.x list sorting nlp