R 比较两个字符串,但不考虑组成字符串的单词的位置
我知道,如果R 比较两个字符串,但不考虑组成字符串的单词的位置,r,string,R,String,我知道,如果 s1 <- "string" s2 <- "String" 然而,我想到的是另一种比较: s1 <- "John Smith" s2 <- "Smith John" s1我们创建一个函数,将字符串按空格(strsplit)拆分,然后取消列表,排序,获取唯一的元素,将它们再次粘贴在一起 f1 <- function(x) toString(unique(sort(unli
s1 <- "string"
s2 <- "String"
然而,我想到的是另一种比较:
s1 <- "John Smith"
s2 <- "Smith John"
s1我们创建一个函数,将字符串按空格(strsplit
)拆分,然后取消列表
,排序
,获取唯一的
元素,将它们再次粘贴在一起
f1 <- function(x) toString(unique(sort(unlist(strsplit(tolower(x), " ")))))
f1(s1) == f1(s2)
#[1] TRUE
f1我们创建一个函数,将字符串按空格(strsplit
)拆分,然后取消列表
,排序
,获取唯一的
元素,将它们再次粘贴在一起
f1 <- function(x) toString(unique(sort(unlist(strsplit(tolower(x), " ")))))
f1(s1) == f1(s2)
#[1] TRUE
f1这些字符串基本上是彼此的字谜。
您可以通过在map中存储这两个字符串并比较这两个map来检查它
dict(collection.Counter(S1)=dict(collection.Counter(S2)这些字符串基本上是彼此的字谜。
您可以通过在map中存储这两个字符串并比较这两个map来检查它
dict(collection.Counter(S1)=dict(collection.Counter(S2)使用utf8ToInt
:
check_string <- function(s1, s2) {
sum(utf8ToInt(tolower(s1))) == sum(utf8ToInt(tolower(s2)))
}
check_string("string", "string")
#[1] TRUE
check_string("string", "String")
#[1] TRUE
check_string("John Smith", "Smith John")
#[1] TRUE
使用utf8ToInt
检查字符串:
check_string <- function(s1, s2) {
sum(utf8ToInt(tolower(s1))) == sum(utf8ToInt(tolower(s2)))
}
check_string("string", "string")
#[1] TRUE
check_string("string", "String")
#[1] TRUE
check_string("John Smith", "Smith John")
#[1] TRUE
检查字符串