首页 > 代码库 > Simple Tips for Collection in Python
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: []FalseFalseFalseFalseindex: 0, value: 10index: 1, value: 12index: 2, value: 23index: 3, value: 24lxw 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。