Python3-遍历字典,找到特定的动态值

时间:2019-06-25 15:04:50

标签: python python-3.x dictionary

我有以下格言:


wordPos = {}
words = [...] #Removed for simplicity

for i, word in enumerate(words):
     wordPos[i] = ({word[5]: word[4]})

最终成为:

>>> wordPos
{0: {1: 'Kontakt'},
 1: {2: 'email@domain.com'}, 
 2: {3: 'domain.com'}}

现在,我正在尝试在上述词典中进行搜索,如果字符串/表达式存在,则应返回“键”作为值。

例如,

string = "@domain.com"

if string in wordPos.values():
   print("The string: {}, exists in the dictionary. The key for this is: {}".format(string, key))

但是我不确定如何在字典中搜索并返回键(值)。

此外,我不确定是否需要使用RegEx进行实际匹配?

编辑

我看到我需要对自己要做的事情更加具体。

因此,基本上,我是逐个单词地读取整个文件,并将每个单词添加到字典中(以及特定单词的行号)-这样就得到了以下结构:

lineNumber:word 

例如1:'Kontakt'

现在我要使用此信息来做的是打开另一个文件,并获得该文件的 first 字(在我的示例中,第一个字为@domain.com)。 / p>

使用第一个单词,我想检查它是否存在于我的字典中(第一次出现)。如果是这样,我想返回行号。因此,在我的示例中,对于单词@domain.com,应返回的行号将为2

4 个答案:

答案 0 :(得分:1)

您可以创建如下功能。这将返回第一个匹配的行号。

import re

input_dict = {
    0: {1: 'Kontakt'},
    1: {2: 'email@domain.com'},
    2: {3: 'domain.com'}
}

def search_word(regex):
    for k, v in input_dict.items():
        for _, v1 in v.items():
            if re.match(regex, v1):
                return k

print(search_word('domain.com')) # 2 (domain.com)
print(search_word('\w+@domain.com')) # 1 (email@domain.com)



输出:

2
1

答案 1 :(得分:0)

如果您真的想在字典中搜索动态值,则需要遍历所有项目,检查这些值是否匹配,然后返回键。没有办法以更pythonic的方式做到这一点。

for key, value in wordPos.items():
    for inner_key, inner_value in value.items():
        if value == string:
            return key

如果您已经有一个单词数组,为什么不只使用index方法呢?

if string in words:
   print(f"The string: {string}, exists. The key for this is: {words.index(string)}")

如果字符串不存在,则会引发ValueError,因此您可以通过以下方式避免使用if

try:
   print(f"The string: {string}, exists. The key for this is: {words.index(string)}")
except ValueError as e:
    pass

答案 2 :(得分:0)

一种可能性是使用内置的python sqlite3模块和FTS5全文索引:

import sqlite3

in_memory = sqlite3.connect(':memory:')
c = in_memory.cursor()
c.execute('CREATE VIRTUAL TABLE "ftsentry" USING FTS5 (line_no UNINDEXED, data, tokenize="unicode61 tokenchars \'.\'")')

c.execute("INSERT INTO ftsentry VALUES (?, ?)", (1, 'Kontakt'))
c.execute("INSERT INTO ftsentry VALUES (?, ?)", (2, 'email@domain.com'))
c.execute("INSERT INTO ftsentry VALUES (?, ?)", (3, 'domain.com'))
c.execute("INSERT INTO ftsentry VALUES (?, ?)", (4, 'domain@sample.com'))

l = [*c.execute('SELECT line_no, data FROM ftsentry WHERE data MATCH ? ORDER BY line_no ASC LIMIT 1', ('"@domain.com"', ))]
print(l)

l = [*c.execute('SELECT line_no, data FROM ftsentry WHERE data MATCH ?', ('"kontakt"', ))]
print(l)

打印:

[(2, 'email@domain.com')]
[(1, 'Kontakt')]

答案 3 :(得分:0)

您需要遍历值的值(这很少是个好主意),

string = "@domain.com"
for key, word in enumerate(d.values()):
    # We must here make the dict_values a list, and take the first index
    if string in list(word.values())[0]:  
        print("The string: {}, exists in the dictionary. The key for this is: {}".format(string, key))

这是一种糟糕的方法。如果您可以解释所获得的数据的外观,可能会有更好的方法。 :)