我想知道什么是最pythonic方式:
拥有字符串列表和子字符串列表会删除包含任何子字符串列表的字符串列表的元素.
list_dirs = ('C:\\foo\\bar\\hello.txt', 'C:\\bar\\foo\\.world.txt', 'C:\\foo\\bar\\yellow.txt')
unwanted_files = ('hello.txt', 'yellow.txt)
Run Code Online (Sandbox Code Playgroud)
期望的输出:
list_dirs = (C:\\bar\\foo\.world.txt')
Run Code Online (Sandbox Code Playgroud)
我曾试图实施类似的问题,比如这个,但我仍然在努力使去除和特定的实现扩展到一个列表.
到目前为止,我已经这样做了:
for i in arange(0, len(list_dirs)):
if 'hello.txt' in list_dirs[i]:
list_dirs.remove(list_dirs[i])
Run Code Online (Sandbox Code Playgroud)
这可行,但可能不是更清洁的方式,更重要的是它不支持列表,如果我想删除hello.txt或yellow.txt我将不得不使用或.谢谢.
我目前正在学习如何使用cProfile,我有一些疑问。
我目前正在尝试分析以下脚本:
import time
def fast():
print("Fast!")
def slow():
time.sleep(3)
print("Slow!")
def medium():
time.sleep(0.5)
print("Medium!")
fast()
slow()
medium()
Run Code Online (Sandbox Code Playgroud)
我执行命令python -m cProfile test_cprofile.py,结果如下:
import time
def fast():
print("Fast!")
def slow():
time.sleep(3)
print("Slow!")
def medium():
time.sleep(0.5)
print("Medium!")
fast()
slow()
medium()
Run Code Online (Sandbox Code Playgroud)
但是,当我编辑带有 pylab 导入的脚本时,例如 ( import pylab) 在顶部,输出cProfile非常大。我试图限制使用的行数,python -m cProfile test_cprofile.py | head -n 10但是我收到以下错误:
Fast!
Slow!
Medium!
7 function calls in 3.504 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function) …Run Code Online (Sandbox Code Playgroud)