使用 XQuery 获取序列中重复次数最多的元素

发布于 2024-09-06 13:59:04 字数 332 浏览 6 评论 0原文

我有一系列值。它们可以都是平等的……也可以不是。因此,通过 XQuery,我想获取序列中最常见的项目。

let $counter := 0, $index1 := 0 
for $value in $sequence 
if (count(index-of($value, $sequence))) 
then 
{ 
$counter := count(index-of($value, $sequence)) $index1 := index-of($value) 
} else {} 

我无法完成这项工作,所以我想我做错了什么。

预先感谢您能给我的任何帮助。

I've got a sequence of values. They can all be equal... or not. So with XQuery I want to get the most frequent item in the sequence.

let $counter := 0, $index1 := 0 
for $value in $sequence 
if (count(index-of($value, $sequence))) 
then 
{ 
$counter := count(index-of($value, $sequence)) $index1 := index-of($value) 
} else {} 

I can't make this work, so I suppose I'm doing something wrong.

Thanks in advance for any help you could give me.

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

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

发布评论

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

评论(2

鹿童谣 2024-09-13 13:59:04

使用

  for $maxFreq in 
           max(for $val in distinct-values($sequence)
                     return count(index-of($sequence, $val))
               )
   return
      distinct-values($sequence)[count(index-of($sequence, .)) eq $maxFreq]

更新,2015 年 12 月

这明显更短,但可能不太高效:

$pSeq[index-of($pSeq,.)[max(for $item in $pSeq return count(index-of($pSeq,$item)))]]

可以为 XPath 3.1 构造最短的表达式:

在此处输入图像描述

甚至更短且可复制 - 使用单字符名称:

$s[index-of($s,.)[max($s ! count(index-of($s, .)))]]

Use:

  for $maxFreq in 
           max(for $val in distinct-values($sequence)
                     return count(index-of($sequence, $val))
               )
   return
      distinct-values($sequence)[count(index-of($sequence, .)) eq $maxFreq]

Update, Dec. 2015:

This is notably shorter, though may not be too-efficient:

$pSeq[index-of($pSeq,.)[max(for $item in $pSeq return count(index-of($pSeq,$item)))]]

The shortest expression can be constructed for XPath 3.1:

enter image description here

And even shorter and copyable -- using a one-character name:

$s[index-of($s,.)[max($s ! count(index-of($s, .)))]]
鹊巢 2024-09-13 13:59:04

您从过于迫切的角度来处理这个问题。

在 XQuery 中,您可以设置变量的值,但永远无法更改它们。

执行迭代类型算法的正确方法是使用递归函数:

declare funciton local:most($sequence, $index, $value, $count)
{
  let $current=$sequence[$index]
  return
    if (empty($current))
    then $value
    else
      let $current-count = count(index-of($current, $sequence))
      return
        if ($current-count > $count)
        then local:most($sequence, $index+1, $current, $current-count)
        else local:most($sequence, $index+1, $value, $count)
}

但解决问题的更好方法是以非迭代方式描述问题。在这种情况下,对于序列中的所有不同值,您需要一个在任何不同值中出现最多次数的值。

前面的句子翻译成 XQuery 是

let $max-count := max(for $value1 in distinct-values($sequence)
                      return count(index-of($sequence, $value1)))
for $value2 in distinct-values($sequence)
where (count(index-of($sequence, $value2)) = $max-count
return $value2

You are approaching this problem from too much of an imperative standpoint.

In XQuery you can set the values of variables, but you can never change them.

The correct way to do iterative-type algorithms is with a recursive function:

declare funciton local:most($sequence, $index, $value, $count)
{
  let $current=$sequence[$index]
  return
    if (empty($current))
    then $value
    else
      let $current-count = count(index-of($current, $sequence))
      return
        if ($current-count > $count)
        then local:most($sequence, $index+1, $current, $current-count)
        else local:most($sequence, $index+1, $value, $count)
}

but a better way of approaching the problem is by describing the problem in a non-iterative way. In this case of all the distinct values in your sequence you want the one that appears maximum number of times of any distinct value.

The previous sentance translated into XQuery is

let $max-count := max(for $value1 in distinct-values($sequence)
                      return count(index-of($sequence, $value1)))
for $value2 in distinct-values($sequence)
where (count(index-of($sequence, $value2)) = $max-count
return $value2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文