打印Python之禅
import this
输出如下。
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
在列表中插入元素
list.append()
展示如下。
>>> a = ['i', 'love']
>>> a.append('apple')
>>> print(a)
['i', 'love', 'apple']
从列表中删除元素
del a[0]
展示如下
>>> a = ['i', 'love', 'apple']
>>> del a[0]
>>> print(a)
['love', 'apple']
取出列表末尾元素并删除它
list.pop()
展示如下
>>> a = ['i', 'love', 'apple']
>>> a.pop()
'apple'
>>> print(a)
['i', 'love']
弹出列表中指定元素
list.pop(index)
>>> a = ['i', 'love', 'apple']
>>> a.pop(0)
'i'
>>> a
['love', 'apple']
根据值删除列表中的元素
list.remove(value)
>>> a = ['i', 'love', 'apple']
>>> a.remove('love')
>>> a
['i', 'apple']
>>> a = ['i', 'love', 'apple', 'apple']
>>> a.remove('apple')
>>> a
['i', 'love', 'apple']
>>> a.remove('not-existing')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
如果有多个值,只会删除第一个指定的值 如果要删除的值不存在,则会报错ValueError
原文始发于微信公众号(Know Why It):Python列表知识查漏补缺
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/276291.html