Python 如何提取一组密钥的所有值

Python 如何提取一组密钥的所有值,python,dictionary,Python,Dictionary,如何从一组键中提取所有值,而不写入所有键,例如,对于包含两个(或更多)项的字典,不使用循环: dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}} dictionary[*]['a1'] 我可以返回“1”和“x”看起来您正在寻找列表 dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

如何从一组键中提取所有值,而不写入所有键,例如,对于包含两个(或更多)项的字典,不使用循环:

dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

dictionary[*]['a1']

我可以返回“1”和“x”

看起来您正在寻找列表

dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

output = [dictionary[x]['a1'] for x in dictionary]
print output
输出:

['1', 'x']

如果担心子键
a1
对于所有值都不存在,那么我们可能应该切换到
dict.get
,对于
a1
不在字典中的所有情况,它将返回
None

output = [dictionary[x].get('a1') for x in dictionary]
或者,如果我们不需要filler
None
值,我们可以按照JohnClements的建议使用过滤器

output = [dictionary[x]['a1'] for x in dictionary if 'a1' in dictionary[x]]

看起来您正在寻找一个列表

dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

output = [dictionary[x]['a1'] for x in dictionary]
print output
输出:

['1', 'x']

如果担心子键
a1
对于所有值都不存在,那么我们可能应该切换到
dict.get
,对于
a1
不在字典中的所有情况,它将返回
None

output = [dictionary[x].get('a1') for x in dictionary]
或者,如果我们不需要filler
None
值,我们可以按照JohnClements的建议使用过滤器

output = [dictionary[x]['a1'] for x in dictionary if 'a1' in dictionary[x]]

看起来您正在寻找一个列表

dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

output = [dictionary[x]['a1'] for x in dictionary]
print output
输出:

['1', 'x']

如果担心子键
a1
对于所有值都不存在,那么我们可能应该切换到
dict.get
,对于
a1
不在字典中的所有情况,它将返回
None

output = [dictionary[x].get('a1') for x in dictionary]
或者,如果我们不需要filler
None
值,我们可以按照JohnClements的建议使用过滤器

output = [dictionary[x]['a1'] for x in dictionary if 'a1' in dictionary[x]]

看起来您正在寻找一个列表

dictionary={'a':{'a1':'1','a2':'2','a3':'3'},'b':{'a1':'x','a2':'y','a3':'z'}}

output = [dictionary[x]['a1'] for x in dictionary]
print output
输出:

['1', 'x']

如果担心子键
a1
对于所有值都不存在,那么我们可能应该切换到
dict.get
,对于
a1
不在字典中的所有情况,它将返回
None

output = [dictionary[x].get('a1') for x in dictionary]
或者,如果我们不需要filler
None
值,我们可以按照JohnClements的建议使用过滤器

output = [dictionary[x]['a1'] for x in dictionary if 'a1' in dictionary[x]]
如果您需要多个项目

result = map(operator.itemgetter('a1', 'a2'), dictionary.itervalues())
zip(*result)

OUTPUT:
[('1', 'x'), ('2', 'y')]
如果你想成为字典的话

keys = ['a1', 'a2']
values = map(operator.itemgetter(*keys), dictionary.itervalues())
dict(zip(keys, zip(*values)))

OUTPUT:
{'a1': ('1', 'x'), 'a2': ('2', 'y')}
如果您需要多个项目

result = map(operator.itemgetter('a1', 'a2'), dictionary.itervalues())
zip(*result)

OUTPUT:
[('1', 'x'), ('2', 'y')]
如果你想成为字典的话

keys = ['a1', 'a2']
values = map(operator.itemgetter(*keys), dictionary.itervalues())
dict(zip(keys, zip(*values)))

OUTPUT:
{'a1': ('1', 'x'), 'a2': ('2', 'y')}
如果您需要多个项目

result = map(operator.itemgetter('a1', 'a2'), dictionary.itervalues())
zip(*result)

OUTPUT:
[('1', 'x'), ('2', 'y')]
如果你想成为字典的话

keys = ['a1', 'a2']
values = map(operator.itemgetter(*keys), dictionary.itervalues())
dict(zip(keys, zip(*values)))

OUTPUT:
{'a1': ('1', 'x'), 'a2': ('2', 'y')}
如果您需要多个项目

result = map(operator.itemgetter('a1', 'a2'), dictionary.itervalues())
zip(*result)

OUTPUT:
[('1', 'x'), ('2', 'y')]
如果你想成为字典的话

keys = ['a1', 'a2']
values = map(operator.itemgetter(*keys), dictionary.itervalues())
dict(zip(keys, zip(*values)))

OUTPUT:
{'a1': ('1', 'x'), 'a2': ('2', 'y')}

你说“不写所有键”是什么意思?你可以做
map(operator.itemgetter('a1'),dictionary.itervalues())
-它不使用显式循环,但不确定你为什么要避免循环?你说“不写所有键”是什么意思?你可以做
map(operator.itemgetter('a1'),dictionary.itervalues())
-它不使用显式循环,但不确定为什么要避免循环?你说的“不写所有键”是什么意思?嗯,你可以做
映射(operator.itemgetter('a1'),dictionary.itervalues())
-它不使用显式循环,但不确定为什么要尝试避免循环?这是什么意思“不写所有键?”好吧,你可以做
map(operator.itemgetter('a1'),dictionary.itervalues())
-它不使用显式循环,但不确定你为什么要避免循环?可能最好使用
dict.get
@padraiccnningham,更新。谢谢你的评论。如果
None
s(或者不需要其他默认值)那么甚至可能
[dictionary[x]['a1']对于dictionary中的x,如果dictionary[x]中的'a1',
@JonClements,使用上面的代码或者
[x.get(“a1”)对于dictionary.itervalues()如果x.get(“a1”)]
@PadraicCunningham检查dict中是否存在密钥比两次检索密钥的值更有效、更灵活。特别是在
None
是有效值的情况下,在这种情况下,您必须使用带有
get
的sentinel对象在list-comp中过滤掉它们。可能最好使用
dict.get
@PadraicCunningham,已更新。感谢您的评论。如果
s(或其他默认值不是必需的),那么可能甚至
[dictionary[x]['a1']对于dictionary中的x,如果dictionary[x]中的'a1',
@JonClements,使用您的上述内容或
[x.get(“a1”)对于dictionary中的x,还有什么效率更高呢a1“)]
@PadraicCunningham检查dict中是否存在密钥比两次检索密钥的值更有效、更灵活。特别是在
None
是有效值的情况下,在这种情况下,您必须使用带有
get
的sentinel对象在list-comp中过滤掉它们。可能最好使用
dict.get
@PadraicCunningham,已更新。感谢您的评论。如果
s(或其他默认值不是必需的),那么可能甚至
[dictionary[x]['a1']对于dictionary中的x,如果dictionary[x]中的'a1',
@JonClements,使用您的上述内容或
[x.get(“a1”)对于dictionary中的x,还有什么效率更高呢a1“)]
@PadraicCunningham检查dict中是否存在密钥比两次检索密钥的值更有效、更灵活。特别是在
None
是有效值的情况下,在这种情况下,您必须使用带有
get
的sentinel对象在list-comp中过滤掉它们。可能最好使用
dict.get
@PadraicCunningham,已更新。感谢您的评论。如果
s(或其他默认值不是必需的),那么可能甚至
[dictionary[x]['a1']对于dictionary中的x,如果dictionary[x]中的'a1',
@JonClements,使用您的上述内容或
[x.get(“a1”)对于dictionary中的x,还有什么效率更高呢a1“)]
@PadraicCunningham检查dict中是否存在键比两次检索键的值更高效、更灵活。尤其是在
None
是有效值的情况下,在这种情况下,您需要