pythondict排序原理(Python标准库使用OrderedDict类的实例讲解)
类别:脚本大全 浏览量:124
时间:2022-03-28 09:16:32 pythondict排序原理
Python标准库使用OrderedDict类的实例讲解目标:创建一个字典,记录几对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 OrderedDict python_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 OrderedDict python_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. |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/zsx0728/article/details/81234532
您可能感兴趣
- pythonsocket建立多用户通讯(Python socket实现多对多全双工通信的方法)
- python获取游戏画面信息(python游戏开发之视频转彩色字符动画)
- python3和python区别(Python2与Python3的区别实例总结)
- python抽奖转盘代码(详解python--模拟轮盘抽奖游戏)
- python pdf文件操作(Python常见读写文件操作实例总结文本、json、csv、pdf等)
- python opencv图像合并(Python3+OpenCV2实现图像的几何变换平移、镜像、缩放、旋转、仿射)
- pythonrequests爬虫使用教程(Python 通过requests实现腾讯新闻抓取爬虫的方法)
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- python图书管理系统(python面向对象法实现图书管理系统)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python简单代码画图(python+pyqt5实现图片批量缩放工具)
- python的基础数据结构有哪些(详解python的四种内置数据结构)
- python函数基本操作(Python定义函数功能与用法实例详解)
- python操作redis(Python获取Redis所有Key以及内容的方法)
- python实现两个变量的交换(在Python 中同一个类两个函数间变量的调用方法)
- python 二维数组怎么取第二列(python实现二维数组的对角线遍历)
- 苹果自研芯片跑分对比 A16芯片排名靠后,M1系列霸榜(苹果自研芯片跑分对比)
- X86处理器的梦魇 苹果M1自研芯片到底有多强(苹果M1自研芯片到底有多强)
- 泰剧《爱欲之神》Boom kitkong和Great合体杂志(泰剧爱欲之神Boomkitkong和Great合体杂志)
- 素人恋爱综艺火药味十足 男生为赢得芳心集体扯头花,真是出好戏(素人恋爱综艺火药味十足)
- 《囧妈》为何受抵制 春节七部影片撤档背后的责任与博弈(囧妈为何受抵制)
- 提醒 2019年起河南驾考要开设科目五 官方回应来了(2019年起河南驾考要开设科目五)
热门推荐
- python中dict怎么创建(Python数据类型之Dict字典实例详解)
- nginx做图片服务器代理(使用nginx动态转换图片大小生成缩略图)
- thinkphp5开发教程(thinkPHP5框架接口写法简单示例)
- css控制div上下移动(CSS鼠标悬浮DIV后显示DIV外的按钮解决方法)
- pandas 导入数据库(详解pandas的外部数据导入与常用方法)
- mysql innodb存储原理(mysql innodb的重要组件汇总)
- 数据库索引如何使用
- mysql的连接池名称如何查看(如何使用mysqladmin获取一个mysql实例当前的TPS和QPS)
- 阿里云centos7java服务器搭建(阿里云 ubuntu16.04搭建IPSec服务)
- ios键盘字符预览怎么关闭(详解通过focusout事件解决IOS键盘收起时界面不归位的问题)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9