2014-11-08 49 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