我发现在Python 3.4中,很少有用于多处理/线程的不同库:多处理与线程和asyncio.
但我不知道使用哪一个或是"推荐的".他们做同样的事情,还是不同?如果是这样,哪一个用于什么?我想编写一个在我的计算机中使用多核的程序.但我不知道我应该学习哪个图书馆.
python multithreading multiprocessing python-3.x python-asyncio
我有这样的剧本:
import argparse
parser = argparse.ArgumentParser(
description='Text file conversion.'
)
parser.add_argument("inputfile", help="file to process", type=str)
parser.add_argument("-o", "--out", default="output.txt",
help="output name")
parser.add_argument("-t", "--type", default="detailed",
help="Type of processing")
args = parser.parse_args()
for arg in args:
print(arg)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我收到错误:
TypeError: 'Namespace' object is not iterable
Run Code Online (Sandbox Code Playgroud)
如何迭代参数及其值?
如何使用枕头获得tiff图像DPI?不能在文档中看到.
from PIL import Image
im = Image.open('test.tif')
print("im dpi?")
Run Code Online (Sandbox Code Playgroud) 例如,我有这个数组并计算行的平均值:
a = np.array([[1,2,3],[2,np.NaN,4]])
mins = np.min(a, axis = 1)
Run Code Online (Sandbox Code Playgroud)
问题是输出是:[1. nan].如何在a中忽略nan并获得结果[1 2]?
如何使用python打印以彩色打印.例如
print('This should be red')
print('This should be green')
Run Code Online (Sandbox Code Playgroud)
现在一切都是黑色背景上的白色文字.我使用ubuntu,如果有帮助的话.
在python 3.4中的tkinter中,如何使用askdirectory对话框创建文件夹?
from tkinter import filedialog
filedialog.askdirectory(initialdir="/tmp/test")
Run Code Online (Sandbox Code Playgroud)
这显示了 choos 目录窗口,但看不到创建新文件夹的选项。例如,/tmp/test/new_folder。通常选择目录窗口有按钮来创建新文件夹,但在 tkinter 中找不到选项。

我有这样的示例列表:
example_list = [['aaa'], ['fff', 'gg'], ['ff'], ['', 'gg']]
Run Code Online (Sandbox Code Playgroud)
现在,我检查它是否有这样的空字符串:
has_empty = False;
for list1 in example_list:
for val1 in list1:
if val1 == '':
has_empty = True
print(has_empty)
Run Code Online (Sandbox Code Playgroud)
这可以正常,因为它打印True,但寻找更多的pythonik方法?
在 Python 3.4 和 Ubuntu 14.04 中使用此代码不会返回 True
import pathlib
path1 = pathlib.Path("/tmp")
path2 = pathlib.Path("/tmp/../tmp")
print(path1 == path2)
# gives False
print(path1 is path2)
# gives False
Run Code Online (Sandbox Code Playgroud)
但通常“/tmp”和“/tmp/../tmp”是同一个文件夹。那么如何确保比较返回 True 呢?
如果我有:
d = {'one':1, 'two':2, 'three':3, 'four':4}
Run Code Online (Sandbox Code Playgroud)
如何在一个命令中获得"一"和"三"的值.像这样的东西:
out = d['one', 'three'] # But it gives an error
Run Code Online (Sandbox Code Playgroud)