Simple Tips for Collection in Python
2014-09-02 23:06
274 查看
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.
Output:
There are some other tips in this article.
Reference:
Python Collection小技巧:https://linuxtoy.org/archives/python-collection-tips.html
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
相关文章推荐
- Tips for Using Images in Blogs
- 10 tips for time management in a multitasking world
- 转载:Setup OpenCV for Python in Windows
- pyjamas build AJAX apps in Python (like Google did for Java)
- SimpleHTTPServer in Python
- Some tips for grammar 'For all Entries' in the ABAP
- Top 7 tips for succeeding in a technical interview for software engineers
- 在python中使用record, for ** in **以及定义函数
- How can I install Easy_Install for Python 2.6.4 in Mac OSX 10.4.11【转】
- SFTP in Python: Really Simple SSH
- Nine tips for a healthy "in production" ASP.NET application
- Making a simple web server in Python.
- python新手必碰到的问题---encode与decode,中文乱码,for 循环 in判断中文
- Python学习笔记十二:列表(4)--len 、in、For的使用
- Farmdev: An In-Process, Headless Web Browser for Python?
- Making a simple web server in Python
- [Tip: C# in VS]Some quick tips for working on C# in VS
- Tips for working with time zone in flex app
- SFTP in Python: Really Simple SSH
- Code a network packet sniffer in python for Linux