Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python中的编号索引访问此词典词典?_Python_Python 2.7_Dictionary_Tuples - Fatal编程技术网

如何使用Python中的编号索引访问此词典词典?

如何使用Python中的编号索引访问此词典词典?,python,python-2.7,dictionary,tuples,Python,Python 2.7,Dictionary,Tuples,我有一本这样的词典 { 'A': {'xx': '2', 'yy': '3'}, 'B': {'xx': '4', 'yy': '5'} } [{'key': 'A', 'xx': '2', 'yy': '3'}, {'key':'B', 'xx': '4', 'yy': '5'}] 我希望能够通过数字索引访问此词典 我想把它改成这样的东西 { 'A': {'xx': '2', 'yy': '3'}, 'B': {'xx': '4', 'yy': '5'} } [{'key': 'A',

我有一本这样的词典

{ 'A': {'xx': '2', 'yy': '3'}, 'B': {'xx': '4', 'yy': '5'} }
[{'key': 'A', 'xx': '2', 'yy': '3'}, {'key':'B', 'xx': '4', 'yy': '5'}]
我希望能够通过数字索引访问此词典

我想把它改成这样的东西

{ 'A': {'xx': '2', 'yy': '3'}, 'B': {'xx': '4', 'yy': '5'} }
[{'key': 'A', 'xx': '2', 'yy': '3'}, {'key':'B', 'xx': '4', 'yy': '5'}]
如何在Python中实现这一点?我正在使用Python 2.7

多谢各位


编辑:在初始答案之后编辑问题。很抱歉,在最初的答案出现之前,我还不清楚我想要什么。

根据您的评论,您可以使用
dict.values()

现在,您可以通过
x[0]
x[1]
访问值,而不是
x['A']
x['B']

b={ 'A': {'xx': '2', 'yy': '3'}, 'B': {'xx': '4', 'yy': '5'} }
下面是检查您的索引,如“A”、“B”是否在dict中,如果它们在dict中,您可以通过B[]获取它们

下面的函数用于获取dict的所有键和值

 for key,value in b.items():
        c.append(value)

print c
#output:[{'xx': '2', 'yy': '3'}, {'xx': '4', 'yy': '5'}]
编辑后

c=[]

for key,value in b.items():
        value['key']=key
        c.append(value)
#output:[{'yy': '3', 'xx': '2', 'key': 'A'}, {'yy': '5', 'xx': '4', 'key': 'B'}]

这是不正确的语法。您不能有
(a:…)
。请重新表述你的问题。我认为你最初的方法很好。不需要改变anything@sshashank124:很抱歉,我的Python在纠正语法方面仍然很弱。我只需要能够访问带有索引的词典。不要在意使用什么结构。谢谢。你的意思是像
my_dict[1]
而不是
my_dict['B']
?你想访问什么,例如A及其值如果我想将输出更改为
[{'key':'A','xx':'2','yy':'3',{'key':'B','xx':'4','yy':'5'}
?我已相应地修改了问题。谢谢你的帮助。对不起,我对原来的问题做了一些更改。谢谢您的帮助。@user3293156这很简单。你也可以把它弄出来put@user3293156现在检查我的答案