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.

  1. def main():
  2. #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.
  3. list1 = [10, 12, 23, 24]
  4. list2 = []
  5.  
  6. if list1:
  7. print "list1: {}".format(list1)
  8. else:
  9. print "list1 is an empty list: {}".format(list1)
  10.  
  11. if list2:
  12. print "list2: {}".format(list2)
  13. else:
  14. print "list2 is an empty list: {}".format(list2)
  15.  
  16. #[] is not None. [] is not False.
  17. print list2==None #False
  18. print list2==False #False
  19. print list2 is None #False
  20. print list2 is False #False
  21.  
  22. #Get the index while traverse a list.
  23. for index, value in enumerate(list1):
  24. print "index: {}, value: {}".format(index, value)
  25.  
  26. if __name__ == '__main__':
  27. main()
  28. else:
  29. print "Being imported as a module."

Output:

  1. lxw Practice$ python collectionTips.py 
  2. list1: [10, 12, 23, 24]
  3. list2 is an empty list: []
  4. False
  5. False
  6. False
  7. False
  8. index: 0, value: 10
  9. index: 1, value: 12
  10. index: 2, value: 23
  11. index: 3, value: 24
  12. 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的更多相关文章

  1. [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 ...

  2. Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)

    Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...

  3. Python Tips阅读摘要

    发现了一本关于Python精通知识点的好书<Python Tips>,关于Python的进阶的技巧.摘录一些比较有价值的内容作为分享. *args and **kwargs 在函数定义的时 ...

  4. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  5. python命令行参数解析模块argparse和docopt

    http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...

  6. Python开源框架、库、软件和资源大集合

    A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome- ...

  7. python 处理xml

    XML XML指可扩展标记语言(Extensible Markup Language) XML被设计用于结构化.存储和传输数据 XML是一种标记语言,很类似于HTML XML没有像HTML那样 ...

  8. python小工具

    http://blog.csdn.net/pipisorry/article/details/46754515 python复制.删除文件代码.python代码出错重新启动 python遍历和删除指定 ...

  9. Python 库汇总英文版

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

随机推荐

  1. Xilinx 7系列例化MIG IP core DDR3读写

    昨晚找了一下,发现DDR3读写在工程上多是通过例化MIG,调用生成IPcore的HDL Functional Model.我说嘛,自己哪能写出那么繁琐的,不过DDR读写数据可以用到状态机,后期再添砖加 ...

  2. CONFIG_*头文件的配置

    通常在kernel或uboot中, 有很多以CONFIG_*开头的宏配置选项,并且保存在相应的头文件中,那么这些CONFIG_*是怎么生成的呢? 在uboot的顶层Makefile中,有这么一项: 此 ...

  3. mysql之load语句

    LOAD DATA LOCAL INFILE 'data.txt' INTO TABLE tbl_name

  4. Python内置函数之len()

    len(s)用来判断对象的长度. 需要说明的是,整型,布尔等是没有长度这一说法的.字符串.字典.列表和元组都有长度. 例子: >>> len() Traceback (most re ...

  5. bootstrap-table接合knockout.js

    function responseHandler(data) { if (data.ErrorNo > 0) { return; } var count = data.Data.TotalRow ...

  6. thinkphp 视频教程

    http://edu.51cto.com/lesson/id-28238.html thinkphp

  7. Photoshop脚本之jpg转换成eps

    function saveEPS( doc, saveFile ) { var saveOptions = new EPSSaveOptions( ); saveOptions.encoding = ...

  8. shell学习五十八天----/proc文件系统

    /proc文件系统 前言:linux中的/proc文件系统,由一组文件夹和文件组成,挂载(mount)与/proc文件夹下. /proc文件系统是一种虚拟文件系统,以文件系统文件夹和文件形式,提供一个 ...

  9. 实战Java虚拟机之中的一个“堆溢出处理”

    从今天開始.我会发5个关于java虚拟机的小系列: 实战Java虚拟机之中的一个"堆溢出处理" 实战Java虚拟机之二"虚拟机的工作模式" 实战Java虚拟机之 ...

  10. Vmware私有云虚拟机(CentOS 6.5 OS)之根分区扩容

    注:适用于未使用lvm管理的分区,目前仅在CentOS 6.5 上操作,其他系统尚未测试,请谨慎操作 一.查看当前分区状况 [root@disk-test ~]# df -h Filesystem   ...