在搜索Python文档时,我发现了Pythons内置函数的等效python实现.zip()
相反,一个醒目的StopIteration例外,这标志着存在由迭代器不产生其他项目的作者(S)使用的if语句来检查,如果返回的默认值形式next()等于object()(" sentinel")和停止发电机:
def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
sentinel = object()
iterators = [iter(it) for it in iterables]
while iterators:
result = []
for it in iterators:
elem = next(it, sentinel)
if elem is sentinel:
return
result.append(elem)
yield tuple(result)
Run Code Online (Sandbox Code Playgroud)
我现在想知道异常捕获或if Python Docs使用的语句之间是否有任何区别?
或者更好,正如@hiro主角指出的那样:在Python中
使用try …
我想知道是否有一种简单的方法可以通过值从python字典中删除一个或多个字典元素.
我们有一个字典叫myDict:
myDict = {1:"egg", "Answer":42, 8:14, "foo":42}
Run Code Online (Sandbox Code Playgroud)
并希望删除值等于的所有项目42.
实施建议:
那么,您认为现在用Python实现这个问题的最优雅,最"pythonic"的方式是什么?
在Python文档指出,
切片索引被静默截断以落在允许的范围内
因此IndexErrors,无论使用什么start或stop参数,切片列表时都没有上升:
>>> egg = [1, "foo", list()]
>>> egg[5:10]
[]
Run Code Online (Sandbox Code Playgroud)
由于列表egg中不包含任何大于的索引2,因此a egg[5]或egg[10]call会引发IndexError:
>> egg[5]
Traceback (most recent call last):
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
现在的问题是,当给定的切片指数超出范围时,我们如何提出IndexError?
在使用Python 的 Turtle模块时,我使用了一些关键事件,如官方文档所述:
turtle.onkey(fun, key)参数:
fun– 没有参数或 None 的函数key– 字符串:键(例如“a”)或键符号(例如“空格”)
现在,有趣的是,当你调用1)的onkeyrelease()方法,并传递一个未注册的字符串(如一个空的(""),或"+"等)key的参数:
turtle.onkeyrelease(lambda: print("Got key event while listening to none."), "")
Run Code Online (Sandbox Code Playgroud)
不管用户按什么键,程序都会输出“ Got key event ...”,顺便说一下这个问题的问题。
不幸的是,我无法在互联网上其他地方的文档中找到有关此行为的更多信息。所以我想知道是否有用于编程关键事件的所有受支持的键名字符串的完整列表?
1)问题中使用的基本设置:
import turtle
turtle.setup(700,500)
turtleWindow = turtle.Screen()
turtleWindow.onkey(lambda: print("You pressed 'a'"), "a")
turtleWindow.listen()
Run Code Online (Sandbox Code Playgroud) 我遇到了在选定元素上使用枚举器形成可迭代(即序列或迭代器或类似物)的情况,并希望返回原始索引而不是默认索引count,从 开始0一直到len(iterable) - 1.
一种非常幼稚的方法是声明一个名为的新生成器对象_enumerate()
>>> def _enumerate(iterable, offset = 0, step = 1):
index = offset
for element in iterable:
yield index, element
index += step
Run Code Online (Sandbox Code Playgroud)
...一个新的列表对象months。
>>> months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
Run Code Online (Sandbox Code Playgroud)
使用蟒蛇在积聚enumerate功能会产生这种输出的[5::2]切片:
>>> …Run Code Online (Sandbox Code Playgroud) 我有一些字典:
d =[{'a': 4}, {'b': 20}, {'c': 5}, {'d': 3}]
Run Code Online (Sandbox Code Playgroud)
我想删除花括号并转换d为如下所示的单个字典:
d ={'a': 4, 'b': 20, 'c': 5, 'd': 3}
Run Code Online (Sandbox Code Playgroud) 在官方Python文档" hashable "部分的词汇表页面下,访问者可以阅读
如果对象具有在其生命周期内永远不会更改的哈希值,则该对象是可清除的...
所有 Python的不可变内置对象都是可清除的,而没有可变容器(如列表或字典)不是......
这意味着传递一个对象
int,float,long,complex,str,bytes,tuple 要么frozenset内置hash()方法的类必须返回假定的哈希值.
问题是元组可以包含不可用的对象(例如lists),因此一些元组不可清除:
创建(有效)由hashable(ints和string)和unhashable(list)数据类型组成的元组.
>>> tuple([1, 2, [3, "4"]])
(1, 2, [3, '4'])
Run Code Online (Sandbox Code Playgroud)哈希这个元组失败了......
>>> hash((1, 2, [3, '4']))
Traceback (most recent call last):
hash((1, 2, [3, '4']))
TypeError: unhashable type: 'list'
Run Code Online (Sandbox Code Playgroud)...尽管hash的对象是一个不可变的内置类型
>>> type((1, 2, …Run Code Online (Sandbox Code Playgroud)我正在搜索文本文件中的单词.它找到单词并返回包含单词的行.这很好,但我想在该行中突出显示或使单词变为粗体.
我可以用Python做到这一点吗?此外,我可以使用更好的方法来获取用户txt文件路径.
def read_file(file):
#reads in the file and print the lines that have searched word.
file_name = raw_input("Enter the .txt file path: ")
with open(file_name) as f:
the_word = raw_input("Enter the word you are searching for: ")
print ""
for line in f:
if the_word in line:
print line
Run Code Online (Sandbox Code Playgroud) python ×8
python-3.x ×3
dictionary ×2
generator ×2
enumerate ×1
exception ×1
hash ×1
indexing ×1
list ×1
python-2.7 ×1
slice ×1
tuples ×1