Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将单词中的字母频率与R(或python)中的26个字母匹配_Python_R_String - Fatal编程技术网

将单词中的字母频率与R(或python)中的26个字母匹配

将单词中的字母频率与R(或python)中的26个字母匹配,python,r,string,Python,R,String,目前,我有一个字符串“Abditator”。我想找出这个单词中字母的频率,与所有英语字母表(即26个字母)进行比较,输出形式如下 输出: a b c d e f g h i ... o ... r s t ... x y z 2 1 1 0 0 0 0 0 1..0..1..0..1 0 1 ... 0 ... 此输出可以是数字向量(名称为26个字母)。我最初的尝试是首先使用strsplit函数将字符串拆分为单个字母(使用R): 然而,对于下一步该做什么,我有点困惑。有人能告诉我这件事吗?非

目前,我有一个字符串
“Abditator”
。我想找出这个单词中字母的频率,与所有英语字母表(即26个字母)进行比较,输出形式如下

输出:

a b c d e f g h i ... o ... r s t ... x y z
2 1 1 0 0 0 0 0 1..0..1..0..1 0 1 ... 0 ... 
此输出可以是数字向量(名称为26个字母)。我最初的尝试是首先使用
strsplit
函数将字符串拆分为单个字母(使用R):

然而,对于下一步该做什么,我有点困惑。有人能告诉我这件事吗?非常感谢。

在Python中:

>>> from collections import Counter
>>> s = "abdicator"
>>> Counter(s)
Counter({'a': 2, 'c': 1, 'b': 1, 'd': 1, 'i': 1, 'o': 1, 'r': 1, 't': 1})
>>> map(Counter(s).__getitem__, map(chr, range(ord('a'), ord('z')+1)))
[2, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]
或:

在R中:

并扩展该位以处理多个单词和/或大写字母的可能性:

words <- c("abdicator", "Syzygy")
letterCount <- function(X) table(c(letters, strsplit(tolower(X), "")[[1]]))-1
t(sapply(words,  letterCount))
#           a b c d e f g h i j k l m n o p q r s t u v w x y z
# abdicator 2 1 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0
# syzygy    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 3 1
wordsPython:

import collections
import string

counts = collections.Counter('abdicator')
chars = string.ascii_lowercase
print(*chars, sep=' ')
print(*[counts[char] for char in chars], sep=' ')
在Python 2中:

import string, collections
ctr = collections.Counter('abdicator')
for l in string.ascii_lowercase:
    print l,
print
for l in string.ascii_lowercase:
    print ctr[l],
print
在Python3中,只有
print
的语法改变


这将精确地生成您请求的输出。核心思想是
collections.Counter
,使用缺少的键进行索引,谦虚地返回
0
,带有明显的语义“此键已被看到0次”,与它对存在的键使用的语义完全一致(其中返回它们的计数,即它们被看到的次数).

非常感谢你,约翰。这正是我想要的。谢谢乔希,这是一个伟大的下一步。我只是想知道,您是否也知道如何将表函数的输出更改为包含单词中未出现的字母的0频率。或者
表(factor(strsplit(“Abditator”),“”[[1]],levels=letters))
嗨,Josh,这是关于钱的。非常感谢:)这段代码按原样打印一个大的未终止行,并且没有使用它导入的
字符串
模块。
words <- c("abdicator", "Syzygy")
letterCount <- function(X) table(c(letters, strsplit(tolower(X), "")[[1]]))-1
t(sapply(words,  letterCount))
#           a b c d e f g h i j k l m n o p q r s t u v w x y z
# abdicator 2 1 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0
# syzygy    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 3 1
import collections
import string

counts = collections.Counter('abdicator')
chars = string.ascii_lowercase
print(*chars, sep=' ')
print(*[counts[char] for char in chars], sep=' ')
import string, collections
ctr = collections.Counter('abdicator')
for l in string.ascii_lowercase:
    print l,
print
for l in string.ascii_lowercase:
    print ctr[l],
print