假设我有一个生成器,它的__next__()功能有点贵,我想尝试并行化调用。我在哪里投入平行化?
更具体一点,请考虑以下示例:
# fast, splitting a file for example
raw_blocks = (b for b in block_generator(fin))
# slow, reading blocks, checking values ...
parsed_blocks = (block_parser(b) for b in raw_blocks)
# get all parsed blocks into a data structure
data = parsedBlocksToOrderedDict(parsed_blocks)
Run Code Online (Sandbox Code Playgroud)
最基本的事情是将第二行更改为进行并行化的内容。是否有一些生成器魔法可以让一个人并行解压生成器(在第三行)?__next__()并行调用?
在numpy数组中找到连续重复nan的最大数量的最佳方法是什么?
例子:
from numpy import nan
Run Code Online (Sandbox Code Playgroud)
输入1: [nan, nan, nan, 0.16, 1, 0.16, 0.9999, 0.0001, 0.16, 0.101, nan, 0.16]
输出1: 3
输入2: [nan, nan, 2, 1, 1, nan, nan, nan, nan, 0.101, nan, 0.16]
输出2: 4
我正在尝试在两个列表之间找到共享元素(以及共享的出现次数).例如,这两个列表的交集:
a = [1, 1, 2, 3, 4, 5, 6, 7, 8, 1]
b = [1, 1, 3, 5, 7, 9]
Run Code Online (Sandbox Code Playgroud)
应该返回Counter({1: 2, 3: 1, 5: 1, 7: 1})或类似的东西,例如{1: 2, 3: 1, 5: 1, 7: 1}或[1, 1, 3, 5, 7](列表的顺序无关紧要).
我已经有一个有效的方法:
cnts_a = Counter(a)
cnts_b = Counter(b)
cnts_a_b = Counter() # counter for the shared values
for key in set(cnts_a).intersection(cnts_b):
cnts_a_b[key] = min(cnts_a[key], cnts_b[key])
Run Code Online (Sandbox Code Playgroud)
但也许有一种更容易(或更快)的方式?
我正在尝试找到一种很好的方法来获取一个2d numpy数组并将列和行名称作为结构化数组附加.例如:
import numpy as np
column_names = ['a', 'b', 'c']
row_names = ['1', '2', '3']
matrix = np.reshape((1, 2, 3, 4, 5, 6, 7, 8, 9), (3, 3))
# TODO: insert magic here
matrix['3']['a'] # 7
Run Code Online (Sandbox Code Playgroud)
我已经能够使用像这样设置列:
matrix.dtype = [(n, matrix.dtype) for n in column_names]
Run Code Online (Sandbox Code Playgroud)
这让我做,matrix[2]['a']但现在我想重命名行,所以我可以做matrix['3']['a'].
我正在尝试使用该struct.pack功能
import struct
values = (0, 44)
s = struct.Struct('HI')
b = s.pack(*values)
print(b)
print(str(len(b)))
Run Code Online (Sandbox Code Playgroud)
它给了我这个输出:
b'\x00\x00\x00\x00,\x00\x00\x00'
8
Run Code Online (Sandbox Code Playgroud)
而python文档说:
Run Code Online (Sandbox Code Playgroud)Format - C Type - Python type - Standard size - Notes H - unsigned short - integer - 2 - (3) I - unsigned int - integer - 4 - (3)
所以len()应该是2 + 4 = 6,我需要大小= 6的字节
有任何想法吗?
我在Windows 10上使用Python 3.6
根据我的理解,__call__类内部的方法实现了函数调用运算符,例如:
class Foo:
def __init__(self):
print("I'm inside the __init__ method")
def __call__(self):
print("I'm inside the __call__ method")
x = Foo() #outputs "I'm inside the __init__ method"
x() #outputs "I'm inside the __call__ method"
Run Code Online (Sandbox Code Playgroud)
但是,我正在阅读Python Cookbook,作者定义了一个元类来控制实例创建,因此您无法直接实例化对象。他是这样做的:
class NoInstance(type):
def __call__(self, *args, **kwargs):
raise TypeError("Can't instantaite class directly")
class Spam(metaclass=NoInstance):
@staticmethod
def grok(x):
print("Spam.grok")
Spam.grok(42) #outputs "Spam.grok"
s = Spam() #outputs TypeError: Can't instantaite class directly
Run Code Online (Sandbox Code Playgroud)
但是,我不明白的是如何s()不被调用,但它的__call__方法被调用了。这是如何运作的?
我正在使用 numba 来制作一些包含 numpy 数组循环的函数。
一切都很好,花花公子,我可以使用jit并且我学会了如何定义签名。
现在我尝试在带有可选参数的函数上使用 jit,例如:
from numba import jit
import numpy as np
@jit(['float64(float64, float64)', 'float64(float64, optional(float))'])
def fun(a, b=3):
return a + b
Run Code Online (Sandbox Code Playgroud)
这有效,但如果不是optional(float)我使用optional(float64)它,则无效(与int或相同int64)。我花了 1 个小时试图弄清楚这个语法(实际上,我的一个朋友偶然发现了这个解决方案,因为他忘记64在浮动之后写),但是,为了我的爱,我不明白为什么会这样。我在互联网上找不到任何东西,而且 numba 关于该主题的文档充其量是稀缺的(并且他们指定optional应该采用 numba 类型)。
有谁知道这是如何工作的?我错过了什么?
通常,dtype当它等同于本机类型时,它是隐藏的:
>>> import numpy as np
>>> np.arange(5)
array([0, 1, 2, 3, 4])
>>> np.arange(5).dtype
dtype('int32')
>>> np.arange(5) + 3
array([3, 4, 5, 6, 7])
Run Code Online (Sandbox Code Playgroud)
但不知何故,这不适用于地板划分或模数:
>>> np.arange(5) // 3
array([0, 0, 0, 1, 1], dtype=int32)
>>> np.arange(5) % 3
array([0, 1, 2, 0, 1], dtype=int32)
Run Code Online (Sandbox Code Playgroud)
为什么会有区别?
Python 3.5.4,NumPy 1.13.1,Windows 64bit
如果我想取一个数字作为输入,我还需要这个.strip()方法吗?像这样:
n = int(input().strip())
Run Code Online (Sandbox Code Playgroud)
而不仅仅是编码:
n = int(input())
Run Code Online (Sandbox Code Playgroud)
我知道.strip()返回一个字符串的副本,其中所有字符都已从字符串的开头和结尾剥离.但我想知道为什么/如果有必要.
我测试了PyCharm和IDLE,它们都将第7个数字打印到第二行.
输入:
import numpy as np
a=np.array([ 1.02090721, 1.02763091, 1.03899317, 1.00630297, 1.00127454, 0.89916715, 1.04486896])
print(a)
Run Code Online (Sandbox Code Playgroud)
输出:
[ 1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715
1.04486896]
Run Code Online (Sandbox Code Playgroud)
如何将它们打印在一行中?