添加到python中的词典列表字典

问题描述 投票:0回答:1

大家好,

这是我在python中完成所需的帮助。

稍微解释一下上下文:我以某个固定的间隔(假设为10秒)重复获取进程相关数据,这是当前存在的所有进程的信息。 (我主要对国家感兴趣)

我需要在python中创建数据结构,基本上为每个进程存储每个状态的总时间,每次我得到样本(比如每10秒),所以增加我在这个时间找到它的状态,如果存在早期记录,或者创建该州的新记录等

因此,对于每个进程,我需要将状态保存在字符串中,如“S”,“R”,“Z”或“UN”,以及每个状态中的时间(如果进程与上次状态相同,则增加相同的状态时间,或者在第一次进程出现在该状态时添加该状态,或者增加它现在的状态,如果它之前处于该状态并且我们已经在内部数据结构中创建了该记录)

我有一本双字典,

process_state_info = {}

然后我以10秒的间隔得到我的样本,说3个进程正在运行,在这样的状态下向我显示自从最后10秒以来,这些pid处于这种状态,

pid:1, state "S" 
pid:2, state "R"
pid 3, state "UN"

所以应该建立这种类型的结构

process_state_info = {1: [{"state":"S", "total_time": 10}],2:
[{"state":"R","total_time":10}],3:[{"state":"UN","total_time":10}]}

在下一个样本中,每个pid可能会处于不同的状态,因此我需要将pid映射到所有可能的状态和在这些状态中花费的时间。

例如,如果下一个样本给了我,

pid:1, state "R" ( changed state, so i need to add an entry in inner structure)
pid:2, state "R" ( in same state, so i will increment existing entry by10 seconds)
pid 3, state "Z" (changed state, add entry in inner structure)
pid 4, state "R" (new pid, add new outer structure)

所以process_state_info需要成为

process_state_info = {1: [{"state":"S", "total_time": 10}, {"state":R", "total"time":10}],2:[{"state":"R","total_time":20}],3:[{"state":"UN","total_time":10}, {"state":"Z","total_time":10}], 4:[{"state":"R", "total_time":10}]}

我正在粘贴我正在尝试的代码,以及我遇到的问题,

process_state_info = {} 

    for process in processes:
        found_pid = 0
        found_state = 0
        print "now process .. "
        print process.pid()
        for g_pid,g_state_info in process_state_info.items():
            if g_pid == process.pid(): #if we found the process already in our data structure
                print "found process in our data structure"
                found_pid = 1
                for state_iter in g_state_info:
                    print state_iter
                    if state_iter['state'] == process.s_name():
                        print "found state for the process in our data structure already"
                        #add interval to that record
                        current_total = state_iter['total_time']
                        state_iter['total_time'] = current_total + self.delta_time
                        found_state = 1
                        print "printing process table again"
                        print process_state_info
                        break
                if found_state == 0:
                    print "found pid but didnt find state inside, adding new state record"
                    inner_record = {"total_time":0,"state":""}
                    inner_record['state'] = process.s_name()
                    inner_record['total_time'] = delta_time
                    process_state_info[g_pid].update(inner_record)
                    print "printing process table now"
                    print process_state_info
                    break
        if found_pid == 0:
            print "didnt find it in our table"
            print "printing process table"
            print process_state_info
            inner_record = {"total_time":0,"state":""}
            inner_record['state'] = process.s_name()
            inner_record['total_time'] = self.delta_time
            g_pid=process.pid()
            print "gpid is"
            print g_pid
            process_state_info[g_pid].update(inner_record) # HOW TO ADD INNER RECORD ?
            print "printing process table again after adding new pid record"

输出我目前得到:

printing process table at start of function print_report
{}
now process .. 
1
didnt find it in our table
printing process table
{}
gpid is
1

错误追溯:

Traceback (most recent call last):   File "./pcp-pidstat.py", line 756, in <module>
    sts = manager.run()   File "/usr/lib64/python2.7/site-packages/pcp/pmcc.py", line 665, in run
    self._printer.report(self)   File "./pcp-pidstat.py", line 733, in report
    report.print_report(timestamp, header_indentation, value_indentation)   File "./pcp-pidstat.py", line 420, in print_report
    process_state_info[g_pid].update(inner_record) # HOW TO ADD INNER RECORD ? KeyError: 1L  <--- why does this happen, and why the "L" ?

有什么建议..蟒蛇专家?

python dictionary
1个回答
0
投票

在Python 2.x中,longint是单独的数据类型。 1L是第一名的字面意思,表示为long。数值相等的longint值仍然是单独的字母键。你的process.pid()电话给你一个long,但process_state_info现有的关键是int

© www.soinside.com 2019 - 2024. All rights reserved.