【发布时间】:2017-09-04 23:21:36
【问题描述】:
我想在 Python 中创建一个结构或相关层次结构,例如:名称xxxx 附加到列表中,并且在xxxx 下存储了多个“ID”?并且在搜索 ID 时,它会返回它所存储的 xxxx。
为了更清楚地说明这一点,xxxx 和 yyyy:
xxxx - |- 112 yyyy - |- 123
|- 113 |- 124
|- 114 |- 125
在搜索任何这些 ID 时,它会返回它所属的组,即如果我搜索 114,它会返回它属于 yyyy。
class Post:
ID = []
incoming_data = ''
Buff = 0
IP = []
contact_info = ''
def Map(self, object):
object.contact_info = object.IP + object.ID
print object.contact_info
obj1 = Post()
obj1.incoming = '112113114115116'
obj1.IP.append('xxx.xxx.xxx.xxx')
for x in range(1,6): #seperates it into sets of 3 digit numbers.
obj1.ID.append(obj1.incoming_data[(3 * x) - 3: 3 * x])
obj1.Map(obj1) #Prints the concatenated value of the both the strings.
我已经到了将它作为字符串放置的地步,但我应该为此使用元组吗?我是新手,我只看了几个小时,感谢您的帮助。我想知道可能的方法。
目前的输出:
['xxx.xxx.xxx.xxx', '112', '113', '114', '115', '116']
【问题讨论】:
-
你的意思是元组。另外,我认为您正在寻找字典。
-
@COLDSPEED 关于我如何使用它的任何详细说明?
-
{'xxxx' : [112, 113, 114], 'yyyy' : [123, 124, 125]} -
啊哈..所以可以通过遍历另一个列表来完成搜索?
-
如果要搜索,可以将 ID 保存在一个集合(而不是列表)中,以便不断查找。
标签: python string list data-structures hierarchy