Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
Python 有效地合并子字符串上的表,而不是完美匹配_Python_R_String_Optimization - Fatal编程技术网

Python 有效地合并子字符串上的表,而不是完美匹配

Python 有效地合并子字符串上的表,而不是完美匹配,python,r,string,optimization,Python,R,String,Optimization,我想在R或Python中合并两个表,每个表都有上万行。但是,我将无法在完美匹配上进行合并。我正在寻找一个键是另一个键的子串的情况。匹配的子字符串可以包含多个单词我正在寻找一种比下面的暴力代码更快的解决方案。 根据我最初建议的玩具数据给出了一个很好的答案。但是,它只匹配单个单词子字符串。(我最初没有明确提出这一要求。) 下面是我在这种情况下使用的代码 library(SPARQL) library(parallel) library(Hmisc) library(tidyr) library(dp

我想在R或Python中合并两个表,每个表都有上万行。但是,我将无法在完美匹配上进行合并。我正在寻找一个键是另一个键的子串的情况。匹配的子字符串可以包含多个单词我正在寻找一种比下面的暴力代码更快的解决方案。

根据我最初建议的玩具数据给出了一个很好的答案。但是,它只匹配单个单词子字符串。(我最初没有明确提出这一要求。)

下面是我在这种情况下使用的代码

library(SPARQL)
library(parallel)
library(Hmisc)
library(tidyr)
library(dplyr)

my.endpoint <- "http://sparql.hegroup.org/sparql/"

go.query <- 'select *
where { graph <http://purl.obolibrary.org/obo/merged/GO>
{ ?goid
<http://www.geneontology.org/formats/oboInOwl#hasOBONamespace>
"biological_process"^^<http://www.w3.org/2001/XMLSchema#string> .
?goid rdfs:label ?goterm}}'
go.result <- SPARQL(url = my.endpoint, query = go.query)
go.result.frame <- go.result[[1]]

anat.query <- 'select distinct ?anatterm ?anatid
where { graph <http://purl.obolibrary.org/obo/merged/UBERON>
{ ?anatid <http://www.geneontology.org/formats/oboInOwl#hasDbXref> ?xr .
?anatid rdfs:label ?anatterm}}'

anat.result <- SPARQL(url = my.endpoint, query = anat.query)
anat.result.frame <- anat.result[[1]]

# slow but recognizes multi-word substrings
loop.solution <-
  mclapply(
    X = sort(anat.result.frame$anatid),
    mc.cores = 7,
    FUN = function(one.anat.id) {
      one.anat.term <-
        anat.result.frame$anatterm[anat.result.frame$anatid == one.anat.id]
      temp <-
        grepl(pattern = paste0('\\b', one.anat.term, '\\b'),
              x = go.result.frame$goterm)
      temp <- go.result.frame[temp , ]
      if (nrow(temp) > 0) {
        temp$anatterm <- one.anat.term
        temp$anatid   <- one.anat.id
        return(temp)
      }
    }
  )

loop.solution <- do.call(rbind, loop.solution)

# from Brandon
# fast, but doesn't recognize multi-word matches
sep.gather.soln <-
  separate(go.result.frame,
           goterm,
           letters,
           sep = " ",
           remove = FALSE) %>%
  gather(goid, goterm) %>%
  na.omit() %>%
  setNames(c("goid", "goterm", "code", "anatterm")) %>%
  select(goid, goterm, anatterm) %>%
  left_join(anat.result.frame) %>%
  na.omit()
库(SPARQL)
图书馆(平行)
图书馆(Hmisc)
图书馆(tidyr)
图书馆(dplyr)
my.endpoint
库(tidyr)
图书馆(dplyr)
df1%设置名称(c(“用餐时间”、“菜肴”、“代码”、“配料”))%>%
选择(用餐时间、菜肴、配料)%>%
左联合(df2)%>%na.omit()
df1

进餐菜肴配料类别
一份早餐奶酪煎蛋卷奶酪乳制品
午餐火鸡三明治火鸡肉
三餐豆汤豆豆类
5份早餐奶酪煎蛋卷
午餐火鸡三明治面包

我正在使用您的原始帖子数据。
第一个拆分项
再次检查字典中的关联项
三合一

terms =["cheese omelette","turkey sandwich","bean soup",]
dictionary ={'turkey': 'meat', 'cheese': 'dairy', 'sandwich': 'bread', 'beef': 'meat', 'omelette': 'eggs', 'bean': 'legume', 'carrot': 'vegetable', 'milk': 'dairy'}

res = set( term +' '+ cat for term in terms for cat in set([ dictionary .get(word,'') for word in term.split()]) if cat) 

for i in res:
    print i


output:
cheese omelette dairy
bean soup legume
turkey sandwich meat
turkey sandwich bread
cheese omelette eggs

set(term+''+cat表示术语中的术语,cat表示术语中的术语([dictionary.get(word.))表示术语中的单词。split()]),如果是cat)谢谢。我在计算代码中的换行符和缩进位置时遇到困难。这是一行代码。请使用真实数据更新您的示例。我不打算安装所有这些软件包,所以只需关注解决问题最后一部分所需的数据。显示非常清晰的输入和输出示例。谢谢,Brandon。对不起,我的示例表不清楚。我的问题的关键是连接列之间不会有完美的匹配,因此标准合并不会起作用。不,不总是两个字。我试图在不同的生物医学本体之间进行匹配。例如,“抑制肾上皮发育”和“肾上皮”之间的匹配。或者在“突发性心脏病”和“心脏病”之间。改变了方法,融化了所有的可能性,丢失了信息,合并了,丢失了信息。哇,看起来不错。我将很快在“真实”数据上试用它。我将根据两个嵌套循环和一个grepl对其进行基准测试。当然,您可以通过将“into”字段限制为dish的最大字长来获得一些改进。现在我只是用返回a:z的信来回答。谢谢你的回复。是的,当字典对的关键部分是一个单词时,这很好地工作。一开始我对关键词可以是多个单词的要求不够清楚,所以我更新了问题。对不起,移动的目标。我现在正在生产中使用我的循环解决方案,但我仍然愿意接受使用多单词键的建议。
terms =["cheese omelette","turkey sandwich","bean soup",]
dictionary ={'turkey': 'meat', 'cheese': 'dairy', 'sandwich': 'bread', 'beef': 'meat', 'omelette': 'eggs', 'bean': 'legume', 'carrot': 'vegetable', 'milk': 'dairy'}

res = set( term +' '+ cat for term in terms for cat in set([ dictionary .get(word,'') for word in term.split()]) if cat) 

for i in res:
    print i


output:
cheese omelette dairy
bean soup legume
turkey sandwich meat
turkey sandwich bread
cheese omelette eggs