[英]how to enumerate OrderedDict in python
So, I'm a php programmer who is trying to learn python. 所以,我是一个试图学习python的php程序员。 i have a dict of dict that i want sorted.
我有一个dict的词典,我想要排序。 I turned them into OrderedDict.
我把它们变成了OrderedDict。 They sort perfectly, The original dict look like this.
它们完美排序,原始字典看起来像这样。 This is just a 3 dimensional array right?
这只是一个三维数组吗?
a["01/01/2001"]["un"]=1
a["01/01/2001"]["nn"]=1
a["01/02/2001"]["aa"]=2
a["01/02/2001"]["bb"]=2
a["01/03/2001"]["zz"]=3
a["01/03/2001"]["rr"]=3
I can convert them into OrderedDict, and want to present them in the following format 我可以将它们转换为OrderedDict,并希望以下列格式呈现它们
"01/01/2001" un=1 nn=1
"01/02/2001" aa=2 bb=2
"01/03/2001" zz=3 rr=3
I can write a simple loop in php to go through this associative array, but i can't figure out how to do it in python. 我可以在php中编写一个简单的循环来完成这个关联数组,但我无法弄清楚如何在python中完成它。 Could someone help?
有人可以帮忙吗?
Loop through the keys and values using the dict.items()
or dict.iteritems()
methods; 使用
dict.items()
或dict.iteritems()
方法遍历键和值; the latter lets you iterate without building an intermediary list of key-value pairs: 后者允许您在不构建键值对的中间列表的情况下进行迭代:
for date, data in a.iteritems():
print date,
for key, value in data.iteritems():
print '{}={}'.format(key, value),
print
Looping directly over dictionaries gives you keys instead; 直接在字典上循环可以为您提供密钥; you can still access the values by using subscription:
您仍然可以使用订阅来访问这些值:
for date in a:
print date,
for key in a[date]:
print '{}={}'.format(key, a[date][key]),
print
I think rather than OrderedDict, you will be better off with a defaultdict: 我认为而不是OrderedDict,你最好使用defaultdict:
from collections import defaultdict
a = defaultdict(dict)
a["01/03/2001"]["zz"]=3
a["01/01/2001"]["un"]=1
a["01/02/2001"]["aa"]=2
a["01/01/2001"]["nn"]=1
a["01/02/2001"]["bb"]=2
a["01/03/2001"]["rr"]=3
# a is now a dict of dicts, each key is a date and each value is a dict of all
# subkey-values
# print out in date order
for k,v in sorted(a.items()):
# for each subdict, print key=value in sorted key order
print k, ' '.join("%s=%s" % (kk,vv) for kk,vv in sorted(v.items()))
Prints: 打印:
01/01/2001 nn=1 un=1
01/02/2001 aa=2 bb=2
01/03/2001 rr=3 zz=3
EDIT: 编辑:
Ah! 啊! My bad, you want the k=v values shown in insertion order, so you need a defaultdict of OrderedDict's:
我的不好,你想要以插入顺序显示的k = v值,所以你需要一个OrderedDict的默认值:
from collections import defaultdict, OrderedDict
a = defaultdict(OrderedDict)
a["01/01/2001"]["un"]=1
a["01/01/2001"]["nn"]=1
a["01/02/2001"]["aa"]=2
a["01/02/2001"]["bb"]=2
a["01/03/2001"]["zz"]=3
a["01/03/2001"]["rr"]=3
# print out in date order
for k,v in sorted(a.items()):
# for each subdict, print key=value in as-inserted key order, so no sort requred
print k, ' '.join("%s=%s" % (kk,vv) for kk,vv in v.items())
Prints: 打印:
01/01/2001 un=1 nn=1
01/02/2001 aa=2 bb=2
01/03/2001 zz=3 rr=3
Try This Code. 试试此代码。
a = {
"01/01/2001": {"un": 1, "nn": 1}, \
"01/02/2001": {"aa": 2, "bb": 2}, \
"01/03/2001": {"zz": 3, "rr": 3}
}
class decor(object):
def __init__(self, a):
self.a = a
def __call__(self, fun):
def wrapper(key, _list):
print key, ' '.join(["{}={}".format(ele, self.a[key][ele]) \
for ele in _list
])
fun(key)
return wrapper
@decor(a)
def main_fun(key):
pass
for key in sorted(a.keys()):
main_fun(key, [inner_key for inner_key in sorted(a[key].keys())])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.