2014-11-08 52 views
0

我开始学习python,特别是我开始学习字典。我看到一个练习,我决定解决它。练习要求使用字典在python中创建一个一周的议程。没有太复杂,但我也要插入用户想要插入的约会。我创造了一些东西,不是太难,但我不知道如何创建议程设计。我做了一件:在Python中创建一个一周的议程

from collections import OrderedDict 
line_new = '' 
d = {} 
d = OrderedDict([("Monday", "10.30-11.30: Sleeping"), ("Tuesday", "13.30-15.30: Web Atelier"), ("Wednsday", "08.30-10.30: Castle"), ("Thursday", ""), ("Friday", "11.30-12.30: Dinner"), ("Saturday",""), ("Sunday","")]) 
for key in d.keys(): 
    line_new = '{:>10}\t'.format(key) 
    print(line_new) 
print("|    |       |        |       |      |     |      |") 
print("|    |       |        |       |      |     |      |") 
print("|    |       |        |       |      |     |      |") 

当输出为:

周一
周二
Wednsday
周四
周五
周六
周日

而且线条营造的想法一张桌子。我怎么能把这些日子都放在一个字典上?我知道如何使用字符串(使用格式),但我不知道如何使用字典中的键来完成它 你能帮助我吗?

编辑 我要找的输出是这样的:

Monday  Tuesday  Wednesday Thursday  Friday Saturday Sunday 

|   |   |    |   |   |   |  | 
|   |   |    |   |   |   |  | 
|   |   |    |   |   |   |  | 
|   |   |    |   |   |   |  | 

有了一定的空间,更多的天(我不能插入在这里),而天之间创建一个分裂的线条

Update to the output after Wasosky's solution

更新到输出后Wasowsky的解决方案

+1

你能证明你打算做什么? (预计产出) – 2014-11-08 18:57:43

+0

哦对。对不起。输出结果应该是在一行上打印的一周中的某一天,并在其间留出一些空间来划分日期。基本上是一样的东西.format()不是 – pp94 2014-11-08 19:21:46

+0

请编辑你的问题,并添加一行或两行对应于你的样本数据。我不明白你想如何用空格来打印星期几,只需显示它。 – 2014-11-08 19:25:04

回答

0
# you can save your formatting as a string to stay consistent 
fmt = '{txt:>{width}}' 

# calculate how much space you need instead of guessing: 
maxwidth = max(len(day) for day in d.keys()) 

# join formatted strings with your separator and print 
separator = ' ' 
print separator.join(fmt.format(txt=day, width=maxwidth) for day in d.keys()) 

# you can use the same formatter to print other rows, just change separator 
separator = ' | ' 
for _ in range(3): 
    print ' | '.join(fmt.format(txt='', width=maxwidth) for day in d.keys())  

输出:

Monday Tuesday Wednsday Thursday  Friday Saturday  Sunday 
     |   |   |   |   |   |   
     |   |   |   |   |   |   
     |   |   |   |   |   |  
+0

好吧。它有效,但我有不同的输出。问题是关于线路。看看我的编辑 – pp94 2014-11-08 20:27:43

+0

看起来你正在使用可变宽度的字体,这就是为什么他们不对齐。切换到等宽字体系列。 – 2014-11-08 20:34:41

+0

我用你的代码代替我的循环... – pp94 2014-11-08 20:44:24