Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x_List_Csv_Dictionary - Fatal编程技术网

Python 如何在字典中查找每个字母并正确使用

Python 如何在字典中查找每个字母并正确使用,python,python-3.x,list,csv,dictionary,Python,Python 3.x,List,Csv,Dictionary,如果我要带一本字典,比如 living_beings= {"Reptile":"Snake","mammal":"whale", "Other":"bird"} 并希望搜索单个字符(如“a”)(例如 for i in living_beings: if "a" in living_beings: print("a is here") 有没有一种效率最高、速度最快的方法 输入只是如上所述的搜索(尽管我的方法不起作用) 我的(失败)代码如下所示: animals=[] f

如果我要带一本字典,比如

living_beings= {"Reptile":"Snake","mammal":"whale", "Other":"bird"}
并希望搜索单个字符(如“a”)(例如

for i in living_beings:
    if "a" in living_beings:
        print("a is here")
有没有一种效率最高、速度最快的方法

输入只是如上所述的搜索(尽管我的方法不起作用)

我的(失败)代码如下所示:

animals=[]
for row in reader: #'reader' is simply what was in the dictionary
    animals.append(row) #I tried to turn it into a list to sort it that way
for i in range(1, len(animals)):
    r= animals[i] 
    for i in r:
        if i== "a": #My attempt to find "a". This is obviously False as i= one of the strings in 
            k=i.replace("'","/") #this is my attempt at the further bit, for a bit of context
            test= animals.append(k)
            print(test)
如果你想知道, 下一步是在该字母前插入一个字符-“/”(在本例中为“a”),尽管这是一个稍有不同的问题,因此与我的问题无关,只是为了更好地理解问题

编辑 我发现了另一个与字典相关的错误。如果字典中有撇号('),那么输出会受到影响,因为它会在引号(“”)中打印特定的单词,而不是正常的撇号。例如:
living_beings={“爬行动物”:“Snake's”,“哺乳类”:“whale”,“Other”:“bird”}
如果您使用以下代码(我需要):


然后输出是
“snake's”,“whale”,“bird'
(注意第一个输出和其他输出之间的区别)。因此我的问题是:如何阻止撇号影响输出。

我的方法是使用dict理解来映射字典,并用
'a'
替换
'a'
出现的每一个
'a'

我不认为有什么显著的性能改进可以从那里完成。您的算法将与
dict
的键和项中的字符总数成线性关系,因为您需要遍历整个字典,无论输入是什么

living_beings= {"Reptile":"Snake","mammal":"whale", "Other":"bird"}
new_dict = { 
    kind.replace('a', '/a'): animal.replace('a', '/a') for kind, animal in living_beings.items()
}
# new_dict: {"Reptile":"Sn/ake","m/amm/al":"wh/ale", "Other":"bird"}

您可以使用一个更复杂的解决方案进行优化,该解决方案在
dict
中循环以对其进行变异,而不是创建一个新的,但一般来说,我建议不要尝试在Python中执行此类操作。只需编写具有良好实践的好代码,并让Python在幕后进行优化。毕竟,这告诉我们:简单就是最好的比复杂更好。

使用正则表达式匹配可以非常有效地完成此操作,例如:

import re
re_containsA = re.compile(r'.*a.*')
for key, word in worddict.items():
    if re_containsA.match(word):
        print(key)

然后,可以使用re.match对象查找匹配文本的位置。

请提供一个清晰的预期输出示例。您是否尝试搜索键或值?作为旁注,您不应该对范围内的i(1,len(动物))使用
而不是
用于动物中的i
。事实上,你几乎不应该使用
range()
@DYZ,为什么?怎么了?@DYZ为什么?b/c你把错误的索引设为1-off?在枚举(l)中使用
用于(idx,item):
获取索引更好?我根本不知道这将如何提高效率。我们仍然需要在线性时间内进行搜索。这可能是Python中最简单的方法。它是线性的,但您必须浏览整个字典,因此无法避免。如果我们需要将效率提高一个常数,我建议使用sw我认为你应该做
re.compile(r'a')
并使用
re.findall
而不是
re.match
来查找所有发生的情况。
import re
re_containsA = re.compile(r'.*a.*')
for key, word in worddict.items():
    if re_containsA.match(word):
        print(key)