English 中文(简体)
甲型六氯环己烷
原标题:Python - Searching Dictionary

抽样数据:

{
    10116079620: { ip.dst : [ 10.1.1.5 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]}, 
    10116882439: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.1.1.5 ],  category : [ Misc ]}, 
    10116080136: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]}, 
    10116884490: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 2.3.4.5 ],  alias : [ www.example.com ],  category : [ Misc ]}, 
    10117039635: { ip.dst : [ 2.3.4.5 ],  ip.src : [ 10.11.11.50 ],  alias : [ google.com ],  category : [ Misc ]}, 
    10118099993: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.11.11.49 ],  alias : [ www.google.com ],  category : [ Misc ]},
    10118083243: { ip.dst : [ 10.11.11.49 ],  ip.src : [ 4.3.2.1 ],  alias : [ www.google.com ],  category : [ Misc ]}}
}

目标:

我的目标是寻找已知存在的价值(IP地址)的样本词典,如果该词出现在ip或ip。 一旦发现我想把“选择”(其他)IP地址写到新的清单中......如果在P.src I想要捕捉,反之亦然。

查询地址不止一次,因此清单不必反映重复。

If 1.2.3.4 is searched then the following would be captured:
* 10.1.1.5
* 10.10.10.99
* 10.11.11.49

Searching on 10.10.10.99 would capture:
* 1.2.3.4
* 2.3.4.5

我确信,这很简单,但我 st着新鲜的 lo,需要比我的泥土更简明的例行公事。

感谢你们的援助。

感谢。

最佳回答

Step 1. Invert the dictionary.

dst= collections.defaultdict( list )
src= collections.defaultdict( list )
for k in original:
    for addr in original[k][ ip.dst ]:
        dst[addr].append( k )
    for addr in original[k][ ip.src ]:
        src[addr].append( k )

Step 2. Don t search, just get the value.

You do two nearly instant checks into dst[addr] and src[addr] and you know all the keys in the original dictionary where it occurred.

Inverting the dictionary takes time.

Building better dictionaries in the first place (i.e., indexed by ip.dst and ip.src) saves the cost of inverting the dictionary you already have.

问题回答

Just for fun, here s how you can do it in a one-liner comprehension!

set([v[ ip.dst ][0] for v in my_dict.values() if v[ ip.src ] == [search_ip]] + [v[ ip.src ][0] for v in my_dict.values() if v[ ip.dst ] == [search_ip]])

Output:

>>>search_ip =  1.2.3.4 
>>>my_dict = {10116079620: { ip.dst : [ 10.1.1.5 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]}, 10116882439: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.1.1.5 ],  category : [ Misc ]}, 10116080136: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]},  10116884490: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 2.3.4.5 ],  alias : [ www.example.com ],  category : [ Misc ]},  10117039635: { ip.dst : [ 2.3.4.5 ],  ip.src : [ 10.11.11.50 ],  alias : [ google.com ],  category : [ Misc ]},  10118099993: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.11.11.49 ],  alias : [ www.google.com ],  category : [ Misc ]}, 10118083243: { ip.dst : [ 10.11.11.49 ],  ip.src : [ 4.3.2.1 ],  alias : [ www.google.com ],  category : [ Misc ]}}
>>>set([v[ ip.dst ][0] for v in my_dict.values() if v[ ip.src ] == [search_ip]] + [v[ ip.src ][0] for v in my_dict.values() if v[ ip.dst ] == [search_ip]])
set([ 10.1.1.5 ,  10.10.10.99 ,  10.11.11.49 ])

>>>search_ip =  10.10.10.99 
>>>set([v[ ip.dst ][0] for v in my_dict.values() if v[ ip.src ] == [search_ip]] + [v[ ip.src ][0] for v in my_dict.values() if v[ ip.dst ] == [search_ip]])
set([ 1.2.3.4 ,  2.3.4.5 ])

I built on S.Lott s answer with some differences. I used sets to remove duplicates, and I kept the search indices together to better match the answers you suggested you wanted.

import collections

# data = your example data dictionary

index = collections.defaultdict(set)
for key in data:
    datum = data[key]
    for ip in datum[ ip.dst ]:
        index[ip].update(datum[ ip.src ])
    for ip in datum[ ip.src ]:
        index[ip].update(datum[ ip.dst ])

print index[ 1.2.3.4 ]
print index[ 10.10.10.99 ]

返回:

set([ 10.10.10.99 ,  10.1.1.5 ,  10.11.11.49 ])
set([ 1.2.3.4 ,  2.3.4.5 ])

下面是一份清单,其中<代码>为你的词典和ip是你正在寻求的:

from functools import partial

def search_row(results, ip, row):
    if row[ ip.dst ][0] == ip:
        results.add(row[ ip.src ][0])
    if row[ ip.src ][0] == ip:
        results.add(row[ ip.dst ][0])

def search(ip, data):
    results = set()
    aggregator = partial(search_row, results, ip)
    map(aggregator, data.values())    
    return results

print search( 1.2.3.4 , data)

print search( 10.10.10.99 , data)

没有任何图书馆(But S.Lott解决方案较短,更优,我爱它):

x={
    10116079620: { ip.dst : [ 10.1.1.5 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]}, 
    10116882439: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.1.1.5 ],  category : [ Misc ]}, 
    10116080136: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 1.2.3.4 ],  category : [ Misc ]}, 
    10116884490: { ip.dst : [ 10.10.10.99 ],  ip.src : [ 2.3.4.5 ],  alias : [ www.example.com ],  category : [ Misc ]}, 
    10117039635: { ip.dst : [ 2.3.4.5 ],  ip.src : [ 10.11.11.50 ],  alias : [ google.com ],  category : [ Misc ]}, 
    10118099993: { ip.dst : [ 1.2.3.4 ],  ip.src : [ 10.11.11.49 ],  alias : [ www.google.com ],  category : [ Misc ]},
    10118083243: { ip.dst : [ 10.11.11.49 ],  ip.src : [ 4.3.2.1 ],  alias : [ www.google.com ],  category : [ Misc ]}
}

y=[(i[ ip.dst ],i[ ip.src ]) for i in x.values()]

a,b=zip(*y)

#Looking for
lf=[ 1.2.3.4 ]
ips=[]


i=0
for ipsrc in a:
    if ipsrc == lf:
        ips.append(b[i])
    i+=1

i=0
for ipdst in b:
    if ipdst == lf:
        ips.append(a[i])
    i+=1

ips=set(ips)
print(ips)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签