简体   繁体   English

在列表中搜索值并输出其键

[英]searching a value in a list and outputting its key

i have a dictionary, in which each key has a list as its value and those lists are of different sizes. 我有一本字典,其中每个键都有一个列表作为其值,并且这些列表的大小不同。 I populated keys and values using add and set(to avoid duplicates). 我使用add和set(以避免重复)填充键和值。 If i output my dictionary, the output is: 如果我输出字典,输出为:

blizzard set(['00:13:e8:17:9f:25', '00:21:6a:33:81:50', '58:bc:27:13:37:c9', '00:19:d2:33:ad:9d'])
alpha_jian set(['00:13:e8:17:9f:25'])  

Here, blizzard and alpha_jian are two keys in my dictionary. 在这里,暴雪和alpha_jian是我字典中的两个键。

Now, i have another text file which has two columns like 现在,我有另一个文本文件,其中有两列

00:21:6a:33:81:50    45  
00:13:e8:17:9f:25    59  

As you can see, the first column items are one of the entries in each list of my dictionary. 如您所见,第一列项目是字典的每个列表中的条目之一。 For example, 00:21:6a:33:81:50 belongs to the key 'blizzard' and 00:13:e8:17:9f:25 belongs to the key 'alpha_jian'. 例如,00:21:6a:33:81:50属于键“暴风雪”,而00:13:e8:17:9f:25属于键“ alpha_jian”。

The problem i want is, go through first column items in my text file, and if that column entry is found in dictionary, find its corresponding key, find the length of that corresponding list in the dictionary, and add them in new dictionary, say newDict. 我想要的问题是,检查文本文件中的第一列项目,如果在字典中找到该列条目,请找到其对应的键,在字典中找到该对应列表的长度,然后将其添加到新字典中,例如newDict。
For example 00:21:6a:33:81:50 belongs to blizzard. 例如00:21:6a:33:81:50属于暴风雪。 Hence, newDict entry will be: 因此,newDict条目将为:

newDict[blizzard] = 4  // since the blizzard key corresponds to a list of length 4.  

This is the code i expected to do this task: 这是我期望执行此任务的代码:

newDict = dict()
# myDict is present with entries like specified above
with open("input.txt") as f:
    for line in f:  
        fields = line.split("\t")  
        for key, value in myDict.items():
            if fields[0] == #Some Expression:
                newdict[key] = len(value)  
print newDict  

Here, my question is what should be #Some Expression in my code above. 在这里,我的问题是上面的代码中的#Some Expression应该是什么。 If values are not lists, this is very easy. 如果值不是列表,这很容易。 But how to search in lists? 但是如何在列表中搜索? Thanks in advance. 提前致谢。

You are looking for in 您正在寻找in

if fields[0] in value:

But this isn't a very efficient method, as it involves scanning the dict values over and over 但这不是一种非常有效的方法,因为它涉及一遍又一遍地扫描dict值

You can make a temporary datastructure to help 您可以建立一个临时数据结构来帮助

helper_dict = {k: v for v, x in myDict.items() for k in x}

So your code becomes 所以你的代码变成

helper_dict = {k: v for v, x in myDict.items() for k in x}
with open("input.txt") as f:
    for line in f:  
        fields = line.split("\t")
        key = fields[0]
        if key in helper_dict:
            newdict[helper_dict[key]] = len(myDict[helper_dict[key]])

Doesn't

if fields[0] in value:

solve your problem ? 解决你的问题? Or I don't understand your question ? 还是我不明白你的问题?

if fields[0] in value: should do the trick given that from what you say above every value in the dictionary is a set, whether of length 1 or greater. if fields[0] in value:鉴于上面所说的,字典中的每个值都是一个集合,无论长度是1还是更大,都应该这样做。

It would probably be more efficient to build a new dictionary with keys like '00:13:e8:17:9f:25' (assuming these are unique), and associated values being the number of entries in their set before you start though - that way you will avoid recalculating this stuff repeatedly. 使用“ '00:13:e8:17:9f:25' (假设它们是唯一的)来构建新字典的效率更高(假设它们是唯一的),并且关联的值是开始之前它们集合中条目的数量-这样,您将避免重复计算这些东西。 Obviously, if the list isn't that long then it doesn't make much difference. 显然,如果列表没有那么长,那么并没有太大的区别。

Looks like 看起来像

if fields[0] in value:

should do the trick. 应该可以。 Ie check if the field is a member of the set (this also works for lists, but a bit slower at least if the lists are large). 即检查该字段是否是集合的成员(这也适用于列表,但至少在列表较大时要慢一些)。

(note that lists and sets are two different things; one is an ordered container that can contain multiple copies of the same value, the other an unordered container that can contain only one copy of each value.) (请注意,列表和集合是两种不同的事物;一种是有序容器,可以包含相同值的多个副本,另一种是无序容器,只能包含每个值的一个副本。)

You may also want to add a break after the newdict assignment, so you don't keep checking all the other dictionary entries. 您可能还希望在newdict分配之后添加一个break ,因此您不必一直检查所有其他字典条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM