下面是 Python 中的代码片段,它将 IP 前缀存储在基数树中,然后如果 IP 属于前缀,则将 IP 和 ASN 关联到字典中。
我想找出特定前缀的所有不同 ASN。更多详情如下:
#rtree is a radix tree which has prefixes stored.
rtree = radix.Radix()
with open(path-to-prefix-file,'r') as fp:
for line in fp:
rnode = rtree.add(line) # Eg, Prefixes like "192.168.2.0/24"
rnode.data["count"]= 0
...
# The code has several lines here for processing a capnproto - skipping them.
rnode.data[IP]=asn_complete # I read a Capnproto buffer and store IP and asn_complete
...
for rnode in rtree:
seen_list = [] # defining a list to store different val, i.e.,asn_complete values
if rnode.data["count"] > 1:
""" Iterate through the rnode.data dictionary """
for ip,val in rnode.data.iteritems():
if val not in seen_list: # Condition is always satisfied!!
seen_list.append(val)
例如:val
在多次迭代中具有来自 protobuf 的以下值:
[<capnp list reader [15169]>, <capnp list reader [1239]>, <capnp list reader [4837]>]
当我打印出seen_list
:
[[<capnp list reader [15169]>, <capnp list reader [1239]>, <capnp list reader [4837]>], [<capnp list reader [15169]>, <capnp list reader [1239]>, <capnp list reader [4837]>], [<capnp list reader [15169]>, <capnp list reader [1239]>, <capnp list reader [4837]>],....]
显然val
在seen_list
;但是,if val not in seen_list:
总是正确的并且val
被附加了seen_list
很多次。我不明白为什么条件总是返回真。是因为存储的对象的类型seen_list
吗?