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 ...
随机推荐
- LINQ操作数组(交集,并集,差集,最值,平均,去重复)
数组是大学里经常拿来做算法练习的对象.一些经典算法非常有价值,考试.装逼.面试都十分有用.但现在是效率时代,编程讲究生产效率,利用LINQ,可以让程序猿避免写一些基本算法,把精力花在业务处理上. 下面 ...
- Java synchronized详解(java 线程同步)
http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html
- undefined reference to `shm_unlink'
1.问题描述: 在编译一个程序的时候提示这样的错误: BLog.cpp:(.text+0x5fc): undefined reference to `shm_unlink'DBLog.cpp:(.te ...
- HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题
推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...
- zabbix 监控如下内容
我们使用zabbix做了如下的监控: 1.硬件监控. 通过SNMP来进行路由器交换机的监控(这些可以跟一些厂商沟通来了解如何做). 服务器的温度以及其他,可以通过IPMI来实现.当然如果没有硬件全都是 ...
- Snowflake Snow Snowflakes - poj 3349 (hash函数)
判断n朵雪花中,是否有完全一样的雪花.简单的hash,将雪花的六个边的权值加起来,记为sum,将sum相等的雪花归为一类,再在这里面根据题意找完全相同的,判断顺时针或者逆时针的所有角是否一模一样. # ...
- Spring帖子汇总
SpringMVC表单标签简介 http://haohaoxuexi.iteye.com/blog/1807330 Spring MVC 教程,快速入门,深入分析 http://elf8848.ite ...
- 模拟 Ajax 上传
----------------view----------------------------------- <form method="post" action=&quo ...
- 在CPU上运行Tensorflow
如果你是用的GPU版本的Tensorflow,你可以这样来使用CPU版本的Tensorlfow: config = tf.ConfigProto( device_count = {'GPU': 0} ...
- python 循环内部添加多个条件判断会出现越界
1.循环遍历数组是,想添加条件修改时,只删除第一个 # -*- coding: utf-8 -*- a=[11,22,33,44,55] for i in a: if i == 11 or i ==2 ...