Python 如何访问嵌套字典中的元组中的值

Python 如何访问嵌套字典中的元组中的值,python,Python,我的要求是用户输入输入,例如PC3或PC1(可能有许多PC,这只是为了给出想法)。例如,我输入PC3 for item in h['PC3']: if isinstance(item, dict): for key in item.keys(): if item[key][1] < 1390618305.478: item.pop(key, None) else:

我的要求是用户输入输入,例如PC3或PC1(可能有许多PC,这只是为了给出想法)。例如,我输入PC3

for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
现在我需要查看与该PC相关联的嵌套字典(例如,本例中的PC3)。对于字典中的每个键,我需要检查元组的第二个值是否小于1390618305.478,如果为真,我将删除该条目(键),例如,它应该删除“192.168.0.1”,并且不打印任何内容

for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
否则 我想以以下格式打印元组和相关键的第一个值,例如04:04:04:04:04和192.168.0.4 04:04:04:04:04:04 192.168.0.4

for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
对于特定PC的嵌套字典中的每个条目,我都会这样做,如果条件为false,我会打印它。否则,如果条件为真,我将删除它。我打印的每个条目都应该在新行上

h={'PC3': [
      '03:03:03:03:03:03', '192.168.0.3', '200', {
        '192.168.0.1': ('01:01:01:01:01:01', 1390618305.477), 
        '192.168.0.4': ('04:04:04:04:04:04', 1390618305.481), 
        '192.168.0.5': ('05:05:05:05:05:05', 1390618305.480)
      }
   ], 
   'PC1': [
     '01:01:01:01:01:05', '192.168.0.1', '200', {}
   ]
}
for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
在这种情况下,192.168.0.1将被删除

04:04:04:04:04:04   192.168.0.4
05:05:05:05:05:05   192.168.0.5
for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
要同时删除其他键,请执行以下操作:

04:04:04:04:04:04 192.168.0.4
05:05:05:05:05:05 192.168.0.5
for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key
从复制导入deepcopy
h={'PC3':['03:03:03:03:03','192.168.0.3','200',{'192.168.0.1':('01:01:01:01:01',1390618305.477),'192.168.0.4':('04:04:04:04:04',1390618305.481),'192.168.0.5':('05:05:05:05',1390618305.480)},'
hcopy=深度复制(h)
对于h['PC3'][3]中的ip,(mac,t)。项()
如果t<1390618305.478:
del hcopy['PC3'][3][ip]
其他:
打印mac、ip
h=hcopy
上述操作将从h的副本中删除不需要的项目。这样做是因为从我们正在做的事情中删除项目,同时迭代可能会导致一些非常奇怪的错误。

试试这个

from copy import deepcopy
h={ 'PC3': ['03:03:03:03:03:03', '192.168.0.3', '200', {'192.168.0.1': ('01:01:01:01:01:01', 1390618305.477), '192.168.0.4': ('04:04:04:04:04:04', 1390618305.481), '192.168.0.5': ('05:05:05:05:05:05', 1390618305.480)}], 'PC1': ['01:01:01:01:01:05', '192.168.0.1', '200', {}]}

hcopy = deepcopy(h)
for ip, (mac, t) in h['PC3'][3].items():
    if t < 1390618305.478:
         del hcopy['PC3'][3][ip]
    else:
        print mac, ip
h = hcopy
for item in h['PC3']:
    if isinstance(item, dict):
        for key in item.keys():
            if item[key][1] < 1390618305.478:
                item.pop(key, None)
            else:
                print item[key][0], key

这就是你想要做的。到目前为止你都试过什么?是的,都试过了。我试过这样做,但遇到这样的情况就来不了。请始终发布您的代码并寻求帮助。开始时:
h['PC3'][1]
'192.168.0.3'
,而
h['PC3'][3]['192.168.0.1']
('01:01:01:01:01',1390618305.477)
。试着找出剩下的。我还想删除那些