2014-03-25 68 views
0

我有一個應用程序,它應該知道每年的每個月會有多少雪,將它存儲在字典中,並按正確的順序打印所有數據。我還需要打印全年的積雪。如何以正確的順序打印此字典並打印總金額? (python)

這是我到目前爲止有:

def main(): 
    M=['January', 'February', 'March','April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December'] 
    dic = {} 
    for i in range(0, len(M)): 
     theKeys = list(dic.items()) 
     theKeys.sort() 
     inpt = int(input("How much did it snow in " + str(M[i]) + " ?:")) 
     dic[str(M[i])] = inpt 

    theKeys = list(dic.items()) 
    theKeys.sort() 
    print(dic) 


main() 

該程序詢問用戶以正確的順序雪。然而,當它打印的一切,這是我得到:

How much did it snow in January ?:1 
How much did it snow in February ?:2 
How much did it snow in March ?:3 
How much did it snow in April ?:4 
How much did it snow in May ?:5 
How much did it snow in June ?:6 
How much did it snow in July ?:7 
How much did it snow in August ?:8 
How much did it snow in September ?:9 
How much did it snow in October ?:10 
How much did it snow in November ?:11 
How much did it snow in December ?:12 

{'June': 6, 'July': 7, 'January': 1, 'December': 12, 'March': 3, 'October': 10, 'September': 9, 'May': 5, 'August': 8, 'February': 2, 'April': 4, 'November': 11} 

另外,我可以使用什麼功能打印的降雪總量?和()? 我的書沒有太多的字典信息。對不起,如果這是一個非常愚蠢的問題。

回答

0

你有一些多餘的線條,如果你想在字典輸出爲按照輸入方式排序,您需要使用OrderedDict

>>> from collections import OrderedDict as od 
>>> def myfunc(): 
     M=['January', 'February', 'March','April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December'] 
     weather = od() #defining an ordered dictionary 
     for i in range(0, len(M)): 
      #theKeys = list(weather.items()) # you are not using this line 
      #theKeys.sort()     # you are not using this one 
      inpt = int(input("How much did it snow in " + str(M[i]) + " ?:")) 
      weather[str(M[i])] = inpt 
     #theKeys = list(weather.items())  # this one is not used 
     print "total snow during the year was ", sum(weather.values()) 

     print(weather) 

行總和(weather.values())原因的列表與來自天氣字典的值和被構造然後值的該列表被求和

天氣= OD()初始化與名稱天氣

一個OrderedDictionary
>>> myfunc() 
How much did it snow in January ?:1 
How much did it snow in February ?:1 
How much did it snow in March ?:1 
How much did it snow in April ?:1 
How much did it snow in May ?:1 
How much did it snow in June ?:1 
How much did it snow in July ?:1 
How much did it snow in August ?:1 
How much did it snow in September ?:1 
How much did it snow in October ?:1 
How much did it snow in November ?:1 
How much did it snow in December ?:1 
total snow during the year was 12 
OrderedDict([('January', 1), ('February', 1), ('March', 1), ('April', 1), ('May', 1), ('June', 1), ('July', 1), ('August', 1), ('September', 1), ('October', 1), ('November', 1), ('December', 1)]) 
+0

「weather = od()」是什麼意思? – user3451338

+0

謝謝!它完美地工作,這正是我所期待的。 :) – user3451338

0

在做theKeys.sort()時,您只會對列表theKeys排序,而不是真正的鍵。 爲了正確打印,您可以使用模塊collections中的OrderedDict

OrderedDict是一個字典,它記住了鍵被首次插入的順序。

排序會按字母順序排序心想:

for k in theKeys: 
... print k,dic[k] 
... 
April 4 
August 8 
December 12 
February 2 
January 1 
July 7 
June 6 
March 3 
May 5 
November 11 
October 10 
September 9 

關於總和,使用dic.values()

>>> print sum(i for i in dic.values()) 
78 
+0

這也解釋了爲什麼它是按此順序打印。謝謝!! – user3451338

0

字典無序。在你的情況下,這不是問題,因爲你有正確的順序,只有一組已知和有限的鍵和值。試試這個:

>>> print(", ".join("{}: {}".format(m, dic[m]) for m in M)) 
January: 1, February: 2, March: 3, April: 4, May: 5, June: 6, July: 7, August: 8, September: 9, October: 10, November: 11, December: 12 

你也做了很多的東西,你不應該做,如排序字典的按鍵,當你有需要的非字母順序排列。您還將字符串轉換爲字符串,並通過它們的索引訪問元素,這不是必需的。下面是一個簡化版本:

>>> def snow_fall(): 
...  months = ['January', 'February', 'March','April', 'May', 'June', 'July', 'August','September', 'October', 'November', 'December'] 
...  snow_fall = {} 
...  for month in months: 
...   snow_fall[month] = input("How much did it snow in {}? ".format(month)) 
...  print(", ".join("{}: {}".format(m, snow_fall[m]) for m in months)) 
...  print("Total snowfall: {}".format(sum(snow_fall.values()))) 
... 
>>> 

一些解釋:

  1. Python的for循環在大多數語言是不喜歡。它更像是for-each-loop。您可以遍歷一系列元素,併爲您提供該元素。如果您絕對需要索引,您將使用enumerate。因此,for month in months會給你January, February ..

  2. 打印正確的順序就像我上面顯示

  3. 打印的總降雪在字典的值相加。

演示:

所有的
>>> snow_fall() 
How much did it snow in January? 10 
How much did it snow in February? 5 
How much did it snow in March? 18 
How much did it snow in April? 3 
How much did it snow in May? 0 
How much did it snow in June? 0 
How much did it snow in July? 0 
How much did it snow in August? 0 
How much did it snow in September? 20 
How much did it snow in October? 30 
How much did it snow in November? 80 
How much did it snow in December? 120 
January: 10, February: 5, March: 18, April: 3, May: 0, June: 0, July: 0, August: 0, September: 20, October: 30, November: 80, December: 120 
Total snowfall: 286 
0

首先,詞典或映射類型沒有任何爲了尊重他們的鑰匙。這是由於其底層結構中的哈希算法。

但是Python用標準庫來拯救。看看文檔:http://docs.python.org/3.3/library/collections.html#collections.OrderedDict

此外,您不需要以任何方式擺弄按鍵或月份列表,這是使用OrderedDict自動完成的。

從收藏導入OrderedDict

def main(): 
    """ 
    Query the amount of snow in each month of a specific year, store it and print 
    the amounts. Calculates the sum of all amounts and print it. 
    """ 

    m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 
     'September', 'October', 'November', 'December'] 
    dic = OrderedDict() 
    for month in m: 
     dic[month] = int(input("How much did it snow in " + month + "? :")) 

    # prints the object-representation of the OrderedDict 
    print(dic) 

    # a slightly better output 
    from pprint import pprint 
    pprint(dic) 

    # calculate the sum 
    print(sum(dic.values())) 

if __name__ == '__main__': 
    main() 

另一個小提示:一個好的演講的代碼風格是http://legacy.python.org/dev/peps/pep-0008/ 這並不意味着,上面的代碼是完全兼容的:)