Python - 使用列表作为值创建字典

时间:2016-11-18 05:57:18

标签: python

我有一个格式低于

的绑定文件
BIND REQ conn=8349228 op=0 msgID=1 version=3 type=SIMPLE dn="uid=test1,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349229 op=0 msgID=1 version=3 type=SIMPLE dn="uid=test1,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349230 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349231 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349232 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349233 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349235 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349234 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349236 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349237 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349238 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349239 op=0 msgID=1 version=3 type=SIMPLE dn="uid=COVESEOS,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349240 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349241 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"
BIND REQ conn=8349242 op=0 msgID=1 version=3 type=SIMPLE dn="uid=xdev,ou=Users,ou=Internal,o=example"`

现在我要做的是创建一个格式{'uid' : [connections ids]}的字典,例如下面的

{'test1' : [8349228,8349229,...],
 'xdev' : [8349230,8349231,...],
 ...so on  }`

有人可以指导我这个!!非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

这种方法应该这样做。当您读取该文件时,请调用此方法,它将以您指定的格式返回一个dict。

import re
def input_to_dict(inp):  # inp = input from text file
    for line in inp.split("\n"):
        pattern = re.compile("uid=([A-Za-z0-9]{1,}),")
        id_pattern = re.compile("conn=([0-9]{1,})")
        name = pattern.search(line).group(1)
        c_id =id_pattern.search(line).group(1)
        if name in d.keys():
            d[name].append(c_id)
        else:
            d[name] = [c_id]
    return d

使用示例:

with open("file.txt", "r") as file:
    lines = file.readlines()
    d = input_to_dict(lines)

答案 1 :(得分:0)

这些步骤是:

Create empty dictionary 
Loop through lines in input file 
    Find values of `uid` and `conn` (easiest way using regex)
    Check if current `uid` exists in dictionary 
        It doesn't, create new entry with current `uid` as key and empty list as value.
    Append `conn` to list associated to `uid` key

为dict

添加键和值

添加按键& dict的值,你应该使用:

if somekey not in dict1.keys():
    dict1[somekey] = []
dict1[somekey].append(somevalue)

您不应该使用任何“外部”列表,只能使用在字典中创建的列表。

答案 2 :(得分:0)

评论建议您可以使用defaultdict list默认值。然后只需为文件中的每一行运行正则表达式并捕获uid&连接到两个组的连接ID,添加到结果:

import re
from collections import defaultdict

res = defaultdict(list)
with open('log.txt') as f:
    for line in f:
        m = re.search('conn=([\w]*).*uid=([^,]*)', line)
        conn_id, uid = m.group(1, 2)
        res[uid].append(conn_id)

print(res)

输出:

defaultdict(<type 'list'>, {
   'test1': ['8349228', '8349229'], 
   'xdev': ['8349230', '8349231', '8349233', '8349235', '8349237', '8349238', '8349240', '8349241', '8349242'], 
   'COVESEOS': ['8349232', '8349234', '8349236', '8349239']
})
相关问题