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的更多相关文章

  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. 版本控制器-VSS和SVN区别

    SVN 默认的工作方式和VSS不同, VSS是[锁定-修改-解锁],VSS是一个人在改的时候必须以独占的方式签出文件,导致其他人不能够修改.用VSS经常要问同事:"改完没,签入一下" ...

  2. 使用spring-boot-maven-plugin打包

    <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> ...

  3. abp主机用户不过滤租户id

    using (_unitOfWorkManager.Current.DisableFilter(DataFilterUtil.NowDisableFilter(AbpSession))) { var ...

  4. sed & awk & grep 专题( 鸟哥 )

    grep, sed 与 awk 相当有用 ! gerp 查找, sed 编辑, awk 根据内容分析并处理. awk(关键字:分析&处理) 一行一行的分析处理 awk '条件类型1{动作1}条 ...

  5. Package java.sql

    Provides the API for accessing and processing data stored in a data source (usually a relational dat ...

  6. response.sendRedirect 的功能是地址重定向(页面跳转)

    response.sendRedirect 的功能是地址重定向(页面跳转) 1.response.sendredirect(url); 新的页面并不能处理旧页面的pagecontext(request ...

  7. NetCore 中 EFcore的DbFirst和CodeFirst混合 使用注意

    NetCore 最近很火热.笔者想把自己以前的旧项目迁移到NetCore平台. 先用EFcore的DBFirst根据数据库创建实体类,然后加入数据库版本控制功能也就是EFcore的CodeFirst部 ...

  8. vs2010中显示行号

    vs2010中显示行号 工具-->选项-->文本编辑器-->所有语言-->行号~ok

  9. java php 3des实现

    php.java.android.ios通用的3des方法:http://blog.csdn.net/zcjwsrf/article/details/47659137 PHP使用3DES算法加密解密字 ...

  10. 将场景导出XML或JSON或二进制而且解析还原场景

    导出unity场景的全部游戏对象信息,一种是XML一种是JSON. 本篇文章我们把游戏场景中游戏对象的.旋转.缩放.平移与Prefab的名称导出在XML与JSON中.然后解析刚刚导出的XML或JSON ...