如何计算数组中的字符串频率(密码,neo4j)



我有一个字符串数组,我想计算在数组中找到一个字符串的次数。

例如:

["Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Gone with the Wind", "Gone with the Wind", "Gone with the Wind", "Gone with the Wind",""Shopaholic to the Stars (Shopaholic, #7)"", "The Farm", "The Farm", "Men Explain Things to Me", "The Valley of Fear", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", "Mastering the Art of French Cooking", "Play With Me", "Play With Me", "Play With Me", "The Perfect Play", "The Perfect Play", "The Perfect Play", "Dream a Little Dream", "Natural Born Charmer", "Collected Poems", "The Friend Zone"]

我想返回书名以及在数组中出现的次数。

例如:

  • 哈利波特与凤凰社 - 5
  • 指环王团契 - 1

等。。。

您可以使用 UNWIND,然后使用 count 进行聚合:

UNWIND ["Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Order of the Phoenix", "Gone with the Wind", "Gone with the Wind", "Gone with the Wind", "Gone with the Wind","Shopaholic to the Stars (Shopaholic, #7)", "The Farm", "The Farm", "Men Explain Things to Me", "The Valley of Fear", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", " The Fellowship of the Ring", "Mastering the Art of French Cooking", "Play With Me", "Play With Me", "Play With Me", "The Perfect Play", "The Perfect Play", "The Perfect Play", "Dream a Little Dream", "Natural Born Charmer", "Collected Poems", "The Friend Zone"]  as book
RETURN book, count(*) as book_cnt

APOC 过程库提供了在这里有用的集合帮助程序函数,特别是apoc.coll.occurrences()

假设您正在传入参数,并且$bookToFind是您要查找的书籍的字符串,$bookList是书籍列表(尽管这可能来自您的图表中的某个地方(,您可以使用:

RETURN apoc.coll.occurrences($bookList, $bookToFind) as occurrences

或者,如果你想获取所有书籍的频率,而不仅仅是某一本书,你可以在收藏中使用apoc.coll.frequencies(),它会给你一个地图列表,每个地图由一个item和列表中该项目的count组成。

最新更新