Simple Tips for Collection in Python
I believe that the following Python code is really not hard to understand. But I think we should use these tips
in our own code.
- def main():
- #To judge whether a list is empty. We can use "if list0" instead of "if len(list0)". But we must be aware that [] is not None and [] is not False.
- list1 = [10, 12, 23, 24]
- list2 = []
- if list1:
- print "list1: {}".format(list1)
- else:
- print "list1 is an empty list: {}".format(list1)
- if list2:
- print "list2: {}".format(list2)
- else:
- print "list2 is an empty list: {}".format(list2)
- #[] is not None. [] is not False.
- print list2==None #False
- print list2==False #False
- print list2 is None #False
- print list2 is False #False
- #Get the index while traverse a list.
- for index, value in enumerate(list1):
- print "index: {}, value: {}".format(index, value)
- if __name__ == '__main__':
- main()
- else:
- print "Being imported as a module."
Output:
- lxw Practice$ python collectionTips.py
- list1: [10, 12, 23, 24]
- list2 is an empty list: []
- False
- False
- False
- False
- index: 0, value: 10
- index: 1, value: 12
- index: 2, value: 23
- index: 3, value: 24
- lxw Practice$
There are some other tips in this article.
Reference:
Python Collection小技巧:https://linuxtoy.org/archives/python-collection-tips.html
Simple Tips for Collection in Python的更多相关文章
- [Python] How to unpack and pack collection in Python?
It is a pity that i can not add the video here. As a result, i offer the link as below: How to unpa ...
- Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)
Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...
- Python Tips阅读摘要
发现了一本关于Python精通知识点的好书<Python Tips>,关于Python的进阶的技巧.摘录一些比较有价值的内容作为分享. *args and **kwargs 在函数定义的时 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- python命令行参数解析模块argparse和docopt
http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...
- Python开源框架、库、软件和资源大集合
A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...
- python 处理xml
XML XML指可扩展标记语言(Extensible Markup Language) XML被设计用于结构化.存储和传输数据 XML是一种标记语言,很类似于HTML XML没有像HTML那样 ...
- python小工具
http://blog.csdn.net/pipisorry/article/details/46754515 python复制.删除文件代码.python代码出错重新启动 python遍历和删除指定 ...
- Python 库汇总英文版
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
随机推荐
- Xilinx 7系列例化MIG IP core DDR3读写
昨晚找了一下,发现DDR3读写在工程上多是通过例化MIG,调用生成IPcore的HDL Functional Model.我说嘛,自己哪能写出那么繁琐的,不过DDR读写数据可以用到状态机,后期再添砖加 ...
- CONFIG_*头文件的配置
通常在kernel或uboot中, 有很多以CONFIG_*开头的宏配置选项,并且保存在相应的头文件中,那么这些CONFIG_*是怎么生成的呢? 在uboot的顶层Makefile中,有这么一项: 此 ...
- mysql之load语句
LOAD DATA LOCAL INFILE 'data.txt' INTO TABLE tbl_name
- Python内置函数之len()
len(s)用来判断对象的长度. 需要说明的是,整型,布尔等是没有长度这一说法的.字符串.字典.列表和元组都有长度. 例子: >>> len() Traceback (most re ...
- bootstrap-table接合knockout.js
function responseHandler(data) { if (data.ErrorNo > 0) { return; } var count = data.Data.TotalRow ...
- thinkphp 视频教程
http://edu.51cto.com/lesson/id-28238.html thinkphp
- Photoshop脚本之jpg转换成eps
function saveEPS( doc, saveFile ) { var saveOptions = new EPSSaveOptions( ); saveOptions.encoding = ...
- shell学习五十八天----/proc文件系统
/proc文件系统 前言:linux中的/proc文件系统,由一组文件夹和文件组成,挂载(mount)与/proc文件夹下. /proc文件系统是一种虚拟文件系统,以文件系统文件夹和文件形式,提供一个 ...
- 实战Java虚拟机之中的一个“堆溢出处理”
从今天開始.我会发5个关于java虚拟机的小系列: 实战Java虚拟机之中的一个"堆溢出处理" 实战Java虚拟机之二"虚拟机的工作模式" 实战Java虚拟机之 ...
- Vmware私有云虚拟机(CentOS 6.5 OS)之根分区扩容
注:适用于未使用lvm管理的分区,目前仅在CentOS 6.5 上操作,其他系统尚未测试,请谨慎操作 一.查看当前分区状况 [root@disk-test ~]# df -h Filesystem ...