python2.7 list转成成元组处理方法

python2.7下
现有列表:
[{'storage1': '0000:05:00.1', 'storage0': '0000:05:00.0', 'data1': '0000:04:00.1', 'control1': '0000:02:00.1', 'control0': '0000:02:00.0', 'data0': '0000:04:00.0'}, {'storage1': '0000:03:00.1', 'storage0': '0000:03:00.0', 'data1': '0000:06:00.1', 'control1': '0000:81:00.1', 'control0': '0000:81:00.0', 'data0': '0000:06:00.0'}, {'storage1': '0000:03:00.1', 'storage0': '0000:03:00.0', 'data1': '0000:06:00.1', 'control1': '0000:81:00.1', 'control0': '0000:81:00.0', 'data0': '0000:06:00.0'}, {'storage1': '0000:04:00.1', 'storage0': '0000:04:00.0', 'data1': '0000:81:00.1', 'control1': '0000:01:00.1', 'control0': '0000:01:00.0', 'data0': '0000:81:00.0'}, {'storage1': '0000:08:00.1', 'storage0': '0000:08:00.0', 'data1': '0000:05:00.1', 'control1': '0000:02:00.1', 'control0': '0000:02:00.0', 'data0': '0000:05:00.0'}]

想要以{"nic_list":[('0000:05:00.1','0000:05:00.1'),('0000:05:00.0','0000:05:00.0')]}....这种结构显示所有的数据,请问如何处理呢,谢谢!

阅读 1.5k
1 个回答
l = [{'storage1': '0000:05:00.1', 'storage0': '0000:05:00.0', 'data1': '0000:04:00.1', 'control1': '0000:02:00.1',
      'control0': '0000:02:00.0', 'data0': '0000:04:00.0'},
     {'storage1': '0000:03:00.1', 'storage0': '0000:03:00.0', 'data1': '0000:06:00.1', 'control1': '0000:81:00.1',
      'control0': '0000:81:00.0', 'data0': '0000:06:00.0'},
     {'storage1': '0000:03:00.1', 'storage0': '0000:03:00.0', 'data1': '0000:06:00.1', 'control1': '0000:81:00.1',
      'control0': '0000:81:00.0', 'data0': '0000:06:00.0'},
     {'storage1': '0000:04:00.1', 'storage0': '0000:04:00.0', 'data1': '0000:81:00.1', 'control1': '0000:01:00.1',
      'control0': '0000:01:00.0', 'data0': '0000:81:00.0'},
     {'storage1': '0000:08:00.1', 'storage0': '0000:08:00.0', 'data1': '0000:05:00.1', 'control1': '0000:02:00.1',
      'control0': '0000:02:00.0', 'data0': '0000:05:00.0'}]

d = {'nic_list': []}

all_poi = []

for item in l:
    for k, v in item.items():
        if 'storage' in k:
            all_poi.append(v)

for i, j in zip(all_poi, all_poi):
    a = (i, j)
    d['nic_list'].append(a)

print(d)
运行结果 {'nic_list': [('0000:05:00.1', '0000:05:00.1'), ('0000:05:00.0', '0000:05:00.0'), ('0000:03:00.1', '0000:03:00.1'), ('0000:03:00.0', '0000:03:00.0'), ('0000:03:00.1', '0000:03:00.1'), ('0000:03:00.0', '0000:03:00.0'), ('0000:04:00.1', '0000:04:00.1'), ('0000:04:00.0', '0000:04:00.0'), ('0000:08:00.1', '0000:08:00.1'), ('0000:08:00.0', '0000:08:00.0')]}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题