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 ...
随机推荐
- 下载文件,ie文件名称乱码问题
设置响应编码,将文件名称用java.net.URLEncoder.encode编码,这样就不会乱码了 java.net.URLEncoder.encode response.setCharacterE ...
- java-MapDemo
Map数据结构的使用 package com.example; import java.util.HashMap; import java.util.Map; /** * MapDemo.java D ...
- centos 启动 nginx
service nginx start https://jingyan.baidu.com/article/bad08e1ec2adc709c85121aa.html
- org.tmatesoft.svn.core.SVNCancelException: svn: E200015: authentication canc
重新添加一个凭证,用新的凭证 第二总是取最新的代码,而不是用update 有问题,问哥
- 从一个实例看javascript几种常用格式的转换
要对如图一所示的左侧table的数据按照“总量”进行排序 1,在前端实现 2,数据格式为object,如图二 原创文章,转载请注明:http://www.cnblogs.com/phpgcs java ...
- ptmalloc、tcmalloc和jemalloc
内存优化总结:ptmalloc.tcmalloc和jemalloc 转载 2017年09月05日 18:57:12 3674 转载于:http://www.cnhalo.net/2016/06/13/ ...
- Git使用技巧(1)-- 配置【持续更新】
配置名字和邮箱 git config --global user.name "Your Name" git config --global user.email "ema ...
- Scrapy爬虫入门系列3 将抓取到的数据存入数据库与验证数据有效性
抓取到的item 会被发送到Item Pipeline进行处理 Item Pipeline常用于 cleansing HTML data validating scraped data (checki ...
- Spring 3 MVC and JSR303 @Valid example
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/ ———————————————————————————— ...
- tomcat 测试页面显示
首先下载匹配jdk版本的tomcat 解压即可使用 将完成的html文件直接放置到webapps目录下的子目录中是无法使用的 原因是tomcat默认加载的是jsp文件,且需要文件配置 所以,除去在we ...