国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python標準庫使用OrderedDict類的實例講解

2020-02-16 01:08:52
字體:
來源:轉載
供稿:網友

目標:創建一個字典,記錄幾對python詞語,使用OrderedDict類來寫,并按順序輸出。

寫完報錯:

[root@centos7 tmp]# python python_terms.py  File "python_terms.py", line 9  from name,language in python_terms.items():       ^SyntaxError: invalid syntax

代碼如下:

from collections import OrderedDictpython_terms = OrderedDict()python_terms['key'] = 'vlaue'python_terms['if']  = 'match'python_terms['from'] = 'import'from name,language in python_terms.items():  print("python have many terms " + name.title() +    language.title() + '.')~   

結果for循環的for寫成from了……總是出現簡單的錯誤。

最終,正確代碼如下:

from collections import OrderedDictpython_terms = OrderedDict()python_terms['key'] = 'vlaue'python_terms['if']  = 'match'python_terms['from'] = 'import'for name,language in python_terms.items():  print("python have many terms " + name.title() +    " " + language.title() + '.')

第一行,從模塊collections中導入OrderedDict類;

第二行,創建了OrderedDict類的一個實例,并將其存儲到python_terms中,也就是創建了一個空字典;

第三至五行,為字典添加鍵值對;

最后,循環輸出結果。

運行結果:

[root@centos7 tmp]# python python_terms.py python have many terms Key Vlaue.python have many terms If Match.python have many terms From Import.

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林站長站的支持。如果你想了解更多相關內容請查看下面相關鏈接

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新蔡县| 航空| 中江县| 饶河县| 湘乡市| 大兴区| 织金县| 扎赉特旗| 贵南县| 青海省| 清丰县| 犍为县| 恩施市| 民和| 普宁市| 武穴市| 东山县| 荔波县| 额济纳旗| 娄底市| 水富县| 延庆县| 辽阳县| 安福县| 浦北县| 修武县| 柳河县| 永修县| 土默特左旗| 海林市| 文水县| 巍山| 象州县| 嵩明县| 铁岭市| 郓城县| 宁晋县| 高阳县| 彝良县| 独山县| 抚州市|