非常具体的问题(我希望):以下三个代码之间有什么区别?
(我希望它只是第一个不等待子进程完成,而第二个和第三个进行.但我需要确定这是唯一的区别......)
我也欢迎其他评论/建议(虽然我已经很清楚shell=True危险和跨平台限制)
请注意,我已经阅读过Python子进程交互,为什么我的进程可以使用Popen.communicate,但不能使用Popen.stdout.read()?并且我不希望/之后需要与程序交互.
另请注意,我已经阅读了Python Popen.communicate()内存限制的替代品?但是我没有真正得到它......
最后,请注意我知道当一个缓冲区使用一种方法填充一个输出时存在死锁的风险,但我在互联网上寻找清晰的解释时迷路了......
第一个代码:
from subprocess import Popen, PIPE
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
stdout = process.stdout.read()
stderr = process.stderr.read()
return process, stderr, stdout
Run Code Online (Sandbox Code Playgroud)
第二个代码:
from subprocess import Popen, PIPE
from subprocess import communicate
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
(stdout, stderr) …Run Code Online (Sandbox Code Playgroud) 这几乎超过了同样的问题在这里,但我问了排序结果的最有效的解决方案.
我有一个列表(大约10个整数在0到12之间随机),例如:
the_list = [5, 7, 6, 5, 5, 4, 4, 7, 5, 4]
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,该函数返回由第一个元素排序的元组(项目,计数)列表
output = [(4, 3), (5, 4), (6, 1), (7, 2)]
Run Code Online (Sandbox Code Playgroud)
到目前为止我用过:
def dupli(the_list):
return [(item, the_list.count(item)) for item in sorted(set(the_list))]
Run Code Online (Sandbox Code Playgroud)
但我把这个函数称为几乎是一个时间,我需要像我(python)一样快.因此我的问题是:如何让这个功能减少时间消耗?(内存怎么样?)
我玩了一下,但没有明显的结果:
from timeit import Timer as T
number=10000
setup = "the_list=[5, 7, 6, 5, 5, 4, 4, 7, 5, 4]"
stmt = "[(item, the_list.count(item)) for item in sorted(set(the_list))]"
T(stmt=stmt, setup=setup).timeit(number=number)
Out[230]: 0.058799982070922852
stmt = "L = []; \nfor item in sorted(set(the_list)): …Run Code Online (Sandbox Code Playgroud) 我有一个数字列表,我想创建一个文本文件(使用python),其名称按特定顺序列出(对于使用mencoder创建的电影).特别是在这里,图形名称包括月份(4月,8月......).我想要Januray的第一个,然后是2月,依此类推.
我知道我可以用一种丑陋的方式做到这一点,但我对一个既优雅(= pythonic?)又最终更通用的解决方案感兴趣.
我的文件,在自然秩序:
cld_for_April_EISopt_1000.png
cld_for_August_EISopt_1000.png
cld_for_December_EISopt_1000.png
cld_for_February_EISopt_1000.png
cld_for_January_EISopt_1000.png
cld_for_July_EISopt_1000.png
cld_for_June_EISopt_1000.png
cld_for_March_EISopt_1000.png
cld_for_May_EISopt_1000.png
cld_for_November_EISopt_1000.png
cld_for_October_EISopt_1000.png
cld_for_September_EISopt_1000.png
我想有这里面的文本文件:
cld_for_January_EISopt_1000.png
cld_for_February_EISopt_1000.png
cld_for_March_EISopt_1000.png
cld_for_April_EISopt_1000.png
cld_for_May_EISopt_1000.png
cld_for_June_EISopt_1000.png
cld_for_July_EISopt_1000.png
cld_for_August_EISopt_1000.png
cld_for_September_EISopt_1000.png
cld_for_October_EISopt_1000.png
cld_for_November_EISopt_1000.png
cld_for_December_EISopt_1000.png
或者更一般地说,如果我有一个列表或数组或字典如:
{'pattern1':rank_in_output_list_1,...,'pattern12':rank_in_output_list_12}
我该如何使用它来命令我的文件名?
到目前为止,我玩过:os.listdir,os.path.isfile,numpyp.ma.array,.compressed()或.compress(); 但我没那么成功.
非常感谢.
克里斯托夫.
python ×3
list ×2
communicate ×1
count ×1
performance ×1
popen ×1
string ×1
subprocess ×1
wait ×1