在for循环中从字典中同时调用两个键

时间:2018-01-11 00:59:54

标签: python dictionary for-loop next ordereddictionary

我有一个嵌套的OrderedDict数据:

OrderedDict([('6',
          OrderedDict([('idx2', ['6', '6', '6']),
                       ('val2', ['A', 'T', 'T']),
                       ('val1', ['C', 'A', 'T']),
                       ('pos', ['11', '15', '23']),
                       ('idx1', ['4', '4', '4'])])),
         ('3',
          OrderedDict([('idx2', ['3', '3']),
                       ('val2', ['G', 'C']),
                       ('val1', ['A', 'G']),
                       ('pos', ['28', '34']),
                       ('idx1', ['4', '4'])])),
         ('4',
          OrderedDict([('idx2', ['4', '4']),
                       ('val2', ['T', 'C']),
                       ('val1', ['C', 'C']),
                       ('pos', ['41', '51']),
                       ('idx1', ['4', '4'])]))])

我可以为每个键访问key, value对,例如:

for k, v in grouped_data.items():
    print(k, v)

并使用以下内容进一步筑巢:

for k, v in grouped_data.items():
print(k, v)
for x in v.items():
    print(x)
    for z in x:
        print(z)

但是,要处理我的数据,我需要在for循环中一次访问两个连续的键:首先是6-3然后是3-4,依此类推,直到结束。

我在想是否可以为密钥编制索引并获取密钥后面的值:

for k, v in grouped_data.items():
    print(k, v)
    ind = grouped_data.keys().index(k)

或者,

for k, v in grouped_data.items():
    print(k, v)
    print(next(k))

0 个答案:

没有答案