我将如何计算数组中每个字母数字的数量?(APL) [英] How would I go about counting the amount of each alphanumerical in an array? (APL)

查看:80
本文介绍了我将如何计算数组中每个字母数字的数量?(APL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何获取矩阵并计算每一行的字母数字值的数量.我只会使用我正在计算的值来处理矩阵.例如,如果我得到了:

I can't figure out how to take a matrix and count the amount of the alphanumerical values for each row. I will only be taking in matrices with the values I'm counting. For example, if I got:

ABA455  
7L9O36G                                 
DZLFPEI

对于第一行,我会得到类似于A:2 B:1 4:1 5:2的信息,并且每一行都将被独立计数.我最想了解所使用的运算符,如果您也可以解释一下的话.谢谢.

I would get something like A:2 B:1 4:1 5:2 for the first row and each row would be counted independently. I would most like to understand the operators used if you could please explain them too. Thank you.

推荐答案

以下内容适用于任何主流APL实现.

The following should work in any mainstream APL implementation.

让我们从一个简单的字符向量开始:

Let's start with a simple vector of characters:

      m ← 3 7⍴'ABA455 7L9O36GDZLFPEI'
      v ← m[1;]
      v
ABA455 

我们可以通过过滤来查找唯一字符,以仅保留与首次出现的索引相同的元素:

We can find the unique characters by filtering to keep only elements that have the same index as the first occurrence of themselves:

      v ⍳ v
1 2 1 4 5 5 7
      ⍳ ⍴ v
1 2 3 4 5 6 7
      ( v ⍳ v ) = ⍳ ⍴ v
1 1 0 1 1 0 1
      ⎕ ← unique ← ( (v ⍳ v) = ⍳ ⍴ v ) / v
AB45 

现在,我们将唯一元素与每个元素进行比较:

Now we compare the unique elements to every element:

      unique ∘.= v
1 0 1 0 0 0 0
0 1 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 1 1 0
0 0 0 0 0 0 1

水平汇总此表可为我们提供每个唯一元素的出现次数:

Summing this table horizontally gives us the occurrence count for each unique element:

      +/ unique ∘.= v
2 1 1 2 1

现在,我们只需要将唯一元素与它们各自的数量配对:

Now we just need to pair up the unique elements with their respective counts:

       unique ,[1.5] +/ unique ∘.= v
A 2
B 1
4 1
5 2
  1

让我们将其放入实用程序功能中

Let's put that into a utility function:

      ∇ c ← Counts v; u
        u ← ( (v ⍳ v) = ⍳ ⍴ v ) / v
        c ← u ,[1.5] +/ u ∘.= v
      ∇ 
      Counts v
A 2
B 1
4 1
5 2
  1

现在,我们需要将此函数应用于矩阵的每一行.我们首先将矩阵拆分为向量的向量:

Now we need to apply this function on each row of the matrix. We start by splitting the matrix into a vector of vectors:

      ⊂[2] m
┌───────┬───────┬───────┐
│ABA455 │7L9O36G│DZLFPEI│
└───────┴───────┴───────┘

然后我们将实用函数应用于每个向量:

Then we apply the utility function to each vector:

      Counts¨ ⊂[2] m
┌───┬───┬───┐
│A 2│7 1│D 1│
│B 1│L 1│Z 1│
│4 1│9 1│L 1│
│5 2│O 1│F 1│
│  1│3 1│P 1│
│   │6 1│E 1│
│   │G 1│I 1│
└───┴───┴───┘

尝试在线!

如果您正在使用Dyalog APL,则非常需要Key操作符():

If you're using Dyalog APL, then the Key operator () is very much what you need:

      {⍺ ⍵}⌸ 'ABA455'
┌─┬───┐
│A│1 3│
├─┼───┤
│B│2  │
├─┼───┤
│4│4  │
├─┼───┤
│5│5 6│
└─┴───┘

它使用一个操作数,并针对每个唯一值对其调用一次,其中特定值作为左参数,并且出现索引列表作为右参数.但是,我们对实际事件不感兴趣,仅对它们的计数感兴趣:

It takes a single operand and calls it once per unique value, with the specific value as left argument and the list of occurrence indices as right argument. However, we're not interested in the actual occurrences, only in their counts:

      {⍺ (≢⍵)}⌸ 'ABA455'
A 2
B 1
4 1
5 2

现在,我们只需要在每行上应用此功能.我们可以通过拆分矩阵并将函数与Each一起使用来实现:

Now we simply have to apply this function on each row. We can do this by splitting the matrix and applying the function with Each:

      {⍺ (≢⍵)}⌸¨ ↓ m
┌───┬───┬───┐
│A 2│7 1│D 1│
│B 1│L 1│Z 1│
│4 1│9 1│L 1│
│5 2│O 1│F 1│
│  1│3 1│P 1│
│   │6 1│E 1│
│   │G 1│I 1│
└───┴───┴───┘

Try it online!

这篇关于我将如何计算数组中每个字母数字的数量?(APL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆