简体   繁体   English

计算子列表中的出现次数

[英]Count occurrence in sub-list

I'm new for python,Can you please help me implement this program using python.我是 python 的新手,你能帮我使用 python 实现这个程序吗?

I have a list: The input我有一个清单:输入

list=[["word1", "word2"],["word1", "word2","word1"], ["word4", "word5","word4", "word5", "word2", "word3"]]

The output: output:

out=[
   {"word1", "word2",2}, {"word2", "word1",1},{"word4", "word5",2},{"word5", "word4",1},
{"word5", "word2",1},{"word2", "word3",1}

]

how it works: it calculates the successive words in a sub-list, and it updates if it finds a new occurrence in a other sub-list它是如何工作的:它计算子列表中的连续单词,如果在另一个子列表中找到新的出现,它会更新

If I understand you right, you want to count sorted pairs of words across all lists.如果我理解正确,您想计算所有列表中排序的单词对。 You can do so, among others, with a collections.Counter and a generator expression.您可以使用collections.Counter和生成器表达式来执行此操作。

>>> import collections
>>> lst = [["word1", "word2"],
...        ["word1", "word2", "word1"],
...        ["word4", "word5", "word4", "word5", "word2", "word3"]]
...
>>> collections.Counter((a, b) for l in lst for a, b in zip(l, l[1:]))
Counter({('word1', 'word2'): 2,
         ('word2', 'word1'): 1,
         ('word4', 'word5'): 2,
         ('word5', 'word4'): 1,
         ('word5', 'word2'): 1,
         ('word2', 'word3'): 1})

The format of the result is a bit different as in your example, but also a lot more useful.结果的格式与您的示例略有不同,但也更有用。 If you actually want a list of sets instead, you can use [{a,b,c} for (a, b), c in _.items()] .如果你真的想要一个集合列表,你可以使用[{a,b,c} for (a, b), c in _.items()] Note that the sets might be "ordered" differently though (or actually not ordered at all).请注意,这些集合可能会以不同的方式“排序”(或者实际上根本没有排序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM