Python 如何从纯文本中获取字典键?

Python 如何从纯文本中获取字典键?,python,dictionary,Python,Dictionary,这项研究的主题取自这个主题。也许我误解了OP的问题,但我已经尝试改进代码。所以,也许我的问题可以有点不同。在解释我想做什么之前,让我与您分享以下代码: dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"} list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'a

这项研究的主题取自这个主题。也许我误解了OP的问题,但我已经尝试改进代码。所以,也许我的问题可以有点不同。在解释我想做什么之前,让我与您分享以下代码:

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
for i in dict_1:
    if i.lower() in " ".join(list_1).lower():
        print("Key: {}\nValue: {}\n".format(i,dict_1[i]))
这些代码可以从
list\u 1
中编写的纯文本中捕获字典键。然而,当我学习这些代码时,我想知道如果一些字典键在
list_1
中重复会怎样。然后我在这个
列表中写了两次相同的键。上述代码无法识别重复的代码,程序给出的结果如下

Key: cfDNA
Value: Blood for analysis

Key: Liquid Biopsy
Value: Blood for analysis


Process finished with exit code 0
然后,我尝试更改我的方法,并编写了不同的代码,如下所示:

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
for i in list_1:
    for j in dict_1:
        for k in j.split():
            count=0
            if k.lower() in i.lower():
                count+=1
                print("Key: {}\nValue: {}\nCount: {}\nDescription: Came from '{}'\n".format(j, dict_1[j],str(count),i))
但很明显,最后的代码会给出不理想的结果。如下图所示,程序从
列表_1
中捕获
液体
活检
单词
cfDNA
列表_1
中被第二次写入,因此程序捕获两次。但是,有没有可能只写一次结果,而把捕获时间加起来呢

Key: Liquid Biopsy
Value: Blood for analysis
Count: 1
Description: Came from 'Liquid'

Key: Liquid Biopsy
Value: Blood for analysis
Count: 1
Description: Came from 'biopsy'

Key: cfDNA
Value: Blood for analysis
Count: 1
Description: Came from 'cfdna'

Key: cfDNA
Value: Blood for analysis
Count: 1
Description: Came from '(cfDNA)'


Process finished with exit code 0

我希望你明白我想做什么。我想捕捉所有写在文本中的键。我还想数一数,这些键在文本中重复了多少次。

如果我理解正确,你想知道“关键字”在文本中出现的次数。您可以使用“re”模块进行此操作

import re

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis", "asfdafaf":"dunno"}
list_1=[u'Liquid', u'biopsy',u'based', u'on', u'circulating', u'cell-free', "cfdna",u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']

text = ' '.join(list_1).lower()

for key in dict_1:
    n = len(re.findall(key.lower(), text))
    if n > 0:
        print('Key:', key)
        print('Value:', dict_1[key])
        print('n:', n)
        print()

最近,我学习了一种新方法,可以计算在不导入“re”模块的情况下,字典键在纯文本中重复多少次。也许在这个主题中使用另一种方法是合适的

dict_1={"Liquid Biopsy":"Blood for analysis","cfDNA":"Blood for analysis"}
list_1=[u'Liquid', u'biopsy', u'liquid', u'biopsy',u'based',u'cfdna' ,u'on', u'circulating', u'cell-free', u'DNA', u'(cfDNA)', u'analysis', u'are', u'described', u'as', u'surrogate', u'samples', u'for', u'molecular', u'analysis.']
string_1=" ".join(list_1).lower()
for i in dict_1:
    if i.lower() in string_1:
        print("Key: {}\nValue: {}\nCount: {}\n".format(i,dict_1[i],string_1.count(i.lower())))
上述代码给出的结果与导入re模块的方法几乎相同。不同的是,它不会写两次键。所以它有点类似于第一篇文章中写的第一个代码结构

Key: Liquid Biopsy
Value: Blood for analysis
Count: 2

Key: cfDNA
Value: Blood for analysis
Count: 2


Process finished with exit code 0

你能分享你的意见吗?如果它是一个文件、一个列表或其他什么?它是一个在代码中定义为list_1的列表。是的,这就是我想要做的。非常感谢你。