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 ...
随机推荐
- pthread_cleanup_push和pthread_cleanup_pop清除函数是否执行的说明
示例1: #include <stdio.h> #include <pthread.h> void* clean(void* arg) { printf("clean ...
- android学习十四(android的接收短信)
收发短信是每一个手机主要的操作,android手机当然也能够接收短信了. android系统提供了一系列的API,使得我们能够在自己的应用程序里接收和发送短信. 事实上接收短信主要是利用我们前面学过的 ...
- archive的时候报“ no identity found"错误 解决方式
第一步: 在xcode----target----general----identity----team里 增加你们公司的账号就能够了 第二步: Xcode > Preferences > ...
- C指针类型小结
要理解复杂的指针类型其实很简单,一个类型里会出现很多运算符,它们也像普通表达式一样,有优先级. 原则: 从变量名开始,根据运算符优先级结合,一步一步分析. 下面让我们先从简单的类型开始慢慢分析吧: i ...
- Spring4 MVC+ AngularJS CRUD使用$http实例
这篇文章显示了使用Spring MVC4整合AngularJS.我们将创建一个使用后端和AngularJS作为前端的纯JSP封装Spring REST API一个CRUD应用程序, 使用 $http ...
- 2015-2016ACM-ICPC NEER northern-subregional-contest C Concatenation
可以在这里提交: http://codeforces.com/gym/100801 题目大意: 给出两个由小写字母组成的字符串S,T,从S中取一个非空前缀,从T中取一个非空后缀,拼接成一个新的字符串. ...
- 阿里云Ubuntu环境搭建Docker服务
经过昨天和今天的不断奋战,在阿里云里面搭建Docker并不easy. 所以我认为有必要记录下来,以供后人学习. 以及我自己的回想. 首先,查看我们的系统版本号: cat /etc/issue 的到的输 ...
- html 模版
使用后台开发语言的都很了解语言的动态性给开发带来的好处,PHP,aspx,jsp页面都可以直接使用相应的语法和变量,输出的事就交给解释器或编译器了,用起来方便快捷,但需要额外的解释工作: 例如php模 ...
- chrome 设置代理服务器
通过设置google chrome浏览器的代理服务器可以让google chrome浏览器通过代理服务器上网,可以隐藏本机的IP地址或者访问一些不能直接访问的网站. 工具/原料 google ch ...
- Redis 安装成windows服务- 一主二从三哨兵,sentinel安装为Windows服务
这里只做记录说明 Redis的主从配置网上很多文章,百度一大堆,安装流程应该都可以配置通.我使用的这篇文章 https://blog.csdn.net/u010648555/article/detai ...