【发布时间】:2019-05-22 06:03:14
【问题描述】:
我有一个这样的默认字典:
{('Montag', '17.30'): [True, False], ('Dienstag', '16.30'): [True, False], ('Mittwoch', '15.30'): [True, False], ('Donnerstag', '14.30'): [True, False, False, True], ('Freitag', '13.30'): [True, False], ('Samstag', '12.30'): [True, False], ('Sonntag', '11.30'): [True, False], ('Sonntag', '17.30'): [False, True], ('Samstag', '16.30'): [False, True], ('Freitag', '15.30'): [False, True], ('Mittwoch', '13.30'): [False, True], ('Dienstag', '12.30'): [False, True], ('Montag', '11.30'): [False, True], ('Donnerstag', '16.30'): [False, True], ('Samstag', '11.25'): [True,True]})
我想以这样的表格形式打印它:
Montag Dienstag Mittwoch Donnerstag Freitag Samstag Sonntag
0 0 0 0 0 100 0 11.25
50 0 0 0 0 0 50 11.30
0 50 0 0 0 50 0 12.30
0 0 50 0 50 0 0 13.30
0 0 0 50 0 0 0 14.30
0 0 50 0 50 0 0 15.30
0 50 0 50 0 50 0 16.30
50 0 0 0 0 0 50 17.30
在 x 轴上,我想输出字典中相邻的所有日期。 (每天只有一次)
在Y轴上,dict中出现的每一次都应该相互输出。
表格应该填写 False 和 True 的比率(也许用 statistics.mean())。
我只解决了用这段代码打印轴:
WOCHENTAGE = {0: "Montag",
1: "Dienstag",
2: "Mittwoch",
3: "Donnerstag",
4: "Freitag",
5: "Samstag",
6: "Sonntag"}
set_for_day = set()
set_for_time = set()
for k, v in testdict.items():
set_for_day.add(k[0])
set_for_time.add(k[1])
order = list(WOCHENTAGE.values())
for day in sorted(set_for_day, key = lambda x: order.index(x)):
print(f"{day} ", end ="")
print()
for times in sorted(set_for_time):
print(f" {times}")
【问题讨论】:
标签: python python-3.x dictionary printing defaultdict