简体   繁体   English

在 python 中,如何创建和维护可由一对 IP 地址索引的 IP 地址列表?

[英]In python, how can I create & maintain a list of IP addresses indexable by a pair of IP addresses?

I'm looking for a way to maintain and update a list of IP addresses that I can index into (via a pair of IP addresses) to add a new address to the list.我正在寻找一种方法来维护和更新 IP 地址的列表,我可以将其编入索引(通过一对 IP 地址)以将新地址添加到列表中。

For example, suppose I have an arbitrary index of (10.0.0.1 & 11.0.0.1).例如,假设我有一个任意索引 (10.0.0.1 & 11.0.0.1)。 For that pair of indexes, I want a list of other IP addresses.对于那对索引,我想要一个其他 IP 地址的列表。 I need to be able to add to the list for the pair 10.0.0.1 & 11.0.0.1 and also use something like the "in" operator to see if the addr I want to add is already present.我需要能够将 10.0.0.1 和 11.0.0.1 对添加到列表中,还需要使用类似“in”运算符的东西来查看我要添加的地址是否已经存在。

Something like this (the indexes are on the left and each index is a list)像这样(索引在左边,每个索引都是一个列表)

(10.0.0.1, 11.0.0.1) --> [1.1.1.1, 6.7.8.9, 120.2.10.7, ...] (10.0.0.1, 11.0.0.1) --> [1.1.1.1, 6.7.8.9, 120.2.10.7, ...]

(17.7.1.3, 119.4.3.1) --> [33.5.1.81, 14.76.1.28, 14.9.65.19, ...]. (17.7.1.3, 119.4.3.1) --> [33.5.1.81, 14.76.1.28, 14.9.65.19, ...]。 . . . .

I don't really want to bother creating a database at the moment, I just want a quick temporary solution.我现在真的不想费心创建数据库,我只是想要一个快速的临时解决方案。 Some time later, I'll store the info into a database.一段时间后,我会将信息存储到数据库中。

Wondering if there are any ideas of collections that I can try.想知道我是否可以尝试 collections 的任何想法。

(See the solution that I posted below, thx to the answers.) (请参阅我在下面发布的解决方案,感谢答案。)

Dictionaries are adequate for this, as long as the key, ie the pair of addresses you want to index by is hashable (eg a tuple containing strings).只要键,即您要索引的一对地址是可散列的(例如,包含字符串的元组),字典就足够了。

EDIT: If you haven't used dicts much yet, be aware that its elements are unordered before Python 3.6, and orderedness is only a documented feature starting with 3.7.编辑:如果您还没有经常使用字典,请注意它的元素在 Python 3.6 之前是无序的,而有序性只是从 3.7 开始的记录功能。 If you only need to be compatible with Python 3.6 upwards, you may rely on the elements being in ascending order of insertion.如果你只需要与 Python 3.6 向上兼容,你可能依赖于插入的升序排列的元素。 Otherwise, you may use collections.OrderedDict instead, from the standard library.否则,您可以改用标准库中的collections.OrderedDict

This appears to be working as I was hoping.这似乎像我希望的那样工作。 I appreciate the help and gladly appreciate any additional comments.感谢您的帮助,并很高兴收到任何其他评论。

Here's what I just tried out (using the arbitrary addresses from my original question):这是我刚刚尝试过的(使用我原来问题中的任意地址):

>>> my_addr_dict = {}
>>> print(my_addr_dict)
{}
>>> print(type(my_addr_dict))
<class 'dict'>
>>>
>>> my_addr_dict[('10.0.0.1','11.0.0.1')]=['1.1.1.1']
>>> my_addr_dict[('10.0.0.1','11.0.0.1')].append('6.7.8.9')
>>> my_addr_dict[('10.0.0.1','11.0.0.1')].append('120.2.10.7')
>>>
>>> print(my_addr_dict)
{('10.0.0.1', '11.0.0.1'): ['1.1.1.1', '6.7.8.9', '120.2.10.7']}
>>>
>>> print(type(my_addr_dict['10.0.0.1','11.0.0.1']))
<class 'list'>
>>>
>>> my_addr_dict[('17.7.1.3','119.4.3.1')]=['33.5.1.81']
>>> my_addr_dict[('17.7.1.3','119.4.3.1')].append('14.76.1.28')
>>> my_addr_dict[('17.7.1.3','119.4.3.1')].append('14.9.65.19')
>>>
>>> print(my_addr_dict)
{('10.0.0.1', '11.0.0.1'): ['1.1.1.1', '6.7.8.9', '120.2.10.7'], ('17.7.1.3', '119.4.3.1'): ['33.5.1.81', '14.76.1.28', '14.9.65.19']}
>>>
>>> print(my_addr_dict[('10.0.0.1','11.0.0.1')])
['1.1.1.1', '6.7.8.9', '120.2.10.7']
>>> print(my_addr_dict[('17.7.1.3','119.4.3.1')])
['33.5.1.81', '14.76.1.28', '14.9.65.19']
>>>

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

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