开发者

Python - uniquifying(!) dictionary keys

I have data coming in from a machine (via pexpect) and I parse it using regexes into a dictionary like this

for line in stream:
    if '/' in line:
        # some matching etc which results in getting the
        # machine name, an interface and the data for that interface
        key=str(hostname)+":"+r.groups()[0][0:2]+r.groups()[2]
        dict[key]=str(line[3])

And that all works ok, I get lots of lines like this when I read it back

machine1:fe0 <data>  

<data> is one string or integer

I now realise that multiple data can exist for the interface, and it seems that in this case, I am overwriting the value for the key every time I encounter it. What I would like is to make the key unique in a way which highlights the fact that multiple info exists for that interface. E.g. if fe0 has 3 instances or fe1 has 4

machine1:fe0:3 <data> <data> <data>
machine1:fe1:4 <data> <data> <data> <data>

To that end I don't mind if 开发者_如何学Pythona single instance has a 1 after it to tell me that.

hope this is clear and someone can point me in the right direction - many thanks


You can create a list for each key, holding all values for that key:

d = collections.defaultdict(list)
for line in stream:
    if '/' in line:
        #.....
        key =  str(hostname)+":"+r.groups()[0][0:2]+r.groups()[2]
        value = str(line[3])
        d[key].append(value)

Edit: If you want the keys/values exactly as specified in your question, you can then do something like:

d2 = {}
for key,values in d.iteritems():
    d2['%s:%d' % (key, len(values)] = ' '.join(str(v) for v in values)

I used ' '.join() here to join the values into a single string - it isn't really clear from your question if that's what you want.

I don't recommend doing things this way, as it will make accessing individual values more difficult.


for (lineno, line) in enumerate(stream):
    if '/' in line:
        # some matching etc which results in getting the
        # machine name, an interface and the data for that interface
        key=str(hostname)+":"+r.groups()[0][0:2]+r.groups()[2]
        dict[key + ":" + lineno]=str(line[3])

You won't end up with it smoothly increasing this way, but each dictionary key will be unique, and the numbers associated with each hostname+interface pair will be increasing. You could make the keys lexically sortable by changing the last line to dict[key + ":" + ('%06d' % (lineno,))=str(line[3])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜