2014-07-07 69 views
1

嗨,大家只是想知道是否可以将下面的列表转换为字典,我知道它最初是一个字典,但它现在作为列表显示出来,如果它可以被转换为最简单的方法从列表中提取数据

我曾尝试d = dict(zip(*[iter(list[0])]*2))但这不起作用

这是列表:

[[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]] 

如果不是有反正从列表中检索数据,例如检索所有数据例如,与identityhash相关?

理想转换这回字典将是最好的解决办法

感谢

+0

内部列表中包含的字典 –

+1

@ user29644021,你必须比子表更是这样回答你接受访问只是一个和忽略列表中的所有其他字母。 –

+0

@ user29644021你仍然只获得一半的字迹 –

回答

3
list=[[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]] 
d = {} 
for dic in l[0]: 
    d.update(dic) 
print d 
{'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'ip': '195.154.12.66', 'orport': '9001', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'dirport': '9030', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'pubtime': '01:09:22'} 

您的列表中有多于一个子表,所以你可能需要压缩和更新, 但你如果您的字典使用相同的密钥,那么当您更新为新字典时,您将失去某些值。

如果你想所有类型的字典压扁成一个单一的列表中,您和创建多种类型的字典,以保持所有这些值,让你列表中的补偿需要的任何键的值:

flattened= ([x for y in l for x in y]) 

这将让所有的来自所有字典的散列:

print [x.get('identityhash') for x in flattened] 

['8039e43306e35287a6467d06dfd5add6681d4c82', '806c5945bbc758c1364774cdff8975ba68569a4e', '80a23969f2efb552ec536d1ce0f1669b2b3e6414', '8e52195b040add42b9e02693cf03dafead1b2c04', '8e5a802850f613bb8c2affbc4e62927a8ecd061a', '8e65b652226d8e32613e0ae454fd0f5cc2f6379c'] 
+0

这是不必要的复杂。 –

+2

@NateMara,列表中有更多的元素,不只是一个字典,你只是访问一个。 –

+0

AttributeError:'str'对象没有任何属性'get' – user2061913

1

如果组合字典,则会丢失数据,因为内部列表包含具有相同键的多个字典。如果你只是想在一个列表中的身份哈希值,你可以用一个列表理解:

>>> data = [[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]] 
>>> identityhash = [i.get('identityhash') for j in data for i in j] 
>>> print(identityhash) 
['8039e43306e35287a6467d06dfd5add6681d4c82','806c5945bbc758c1364774cdff8975ba68569a4e','80a269f2efb552ec536d1ce0f1669b2b3e6414','8e52195b040add42b9e02693cf03dafead1b2c04','8e5a802850f6bb8c2affbc4e62927a8ecd061a','8e65b652226d8e32613e0ae454fd0f5cc2f6379c'] 
+1

列表中有更多的数据[0] [0]字典,所以你确实需要一个循环。 –

+0

@PadraicCunningham现在我明白了。 –

+0

仍然没有访问所有的词典 –