我有一个Python字符串,我想\include在一个乳胶文件中.但是,我的字符串有一个下划线字符(_),\_在包含之前需要将其转换为(这是_的乳胶表示).
如果我做:
self.name = self.laser_comboBox.currentText() #Get name from a qt widget
latex_name = self.name.replace("_", r"\_") #Replacing _ for \_
print self.name
print latex_name
Run Code Online (Sandbox Code Playgroud)
我去拿:
XXXXXX-YY\_SN2017060009
XXXXXX-YY\_SN2017060009
Run Code Online (Sandbox Code Playgroud)
可以看出,两个变量都被替换了.这意味着该replace方法就位.但是,如果我这样做:
self.name = self.laser_comboBox.currentText() #Get name from a qt widget
latex_name = str(self.name).replace("_", r"\_") #Converting to str and then replacing _ for \_
print self.name
print latex_name
Run Code Online (Sandbox Code Playgroud)
我去拿:
XXXXXX-YY_SN2017060009
XXXXXX-YY\_SN2017060009
Run Code Online (Sandbox Code Playgroud)
这让我很困惑......我想知道为什么会这样......
我正在开发一个函数来对大输入数据进行一些处理。但是,由于我无法一次将所有数据放入内存(点积为 117703x200000 矩阵),因此我将其分成块并按部分计算。
输出仅采用前 5 个元素(排序后),因此形状必须为 117703x5,这在内存中是可行的。但是,由于某种原因,随着循环的进行,我的内存消耗不断增加,直到出现内存错误。任何想法为什么?这是代码:
def process_predictions_proto(frac=50):
# Simulate some inputs
query_embeddings = np.random.random((117703, 512))
proto_feat = np.random.random((200000, 512))
gal_cls = np.arange(200000)
N_val = query_embeddings.shape[0]
pred = []
for i in tqdm(range(frac)):
start = i * int(np.ceil(N_val / frac))
stop = (i + 1) * int(np.ceil(N_val / frac))
val_i = query_embeddings[start:stop, :]
# Compute distances
dist_i = np.dot(val_i, proto_feat.transpose())
# Sort
index_i = np.argsort(dist_i, axis=1)[::-1]
dist_i = np.take_along_axis(dist_i, index_i, axis=1)
# Convert distances to class_ids
pred_i = np.take_along_axis( …Run Code Online (Sandbox Code Playgroud) 我正在使用for loopPyQt中的多个信号/插槽。代码如下:
# Connect Scan Callbacks
for button in ['phase', 'etalon', 'mirror', 'gain']:
getattr(self.ui, '{}_scan_button' .format(button)).clicked.connect(
lambda: self.scan_callback(button))
Run Code Online (Sandbox Code Playgroud)
我的期望:
phase_scan_button单击“ 连接”按钮signal到,scan_callback slot然后将字符串phase作为参数发送到slot。同样的etalon,mirror和gain。我得到的是:
gain作为所有按钮的参数传递。不知道我是不是很愚蠢(可能是)或它是一个错误。作为参考,该slot方法:
def scan_callback(self, scan):
print(scan) # Here I always get 'gain'
if self.scanner.isWorking:
self.scanner.isWorking = False
self.scan_thread.terminate()
self.scan_thread.wait()
else:
self.scanner.isWorking = True
self.scan_thread.start()
getattr(self.ui, '{}_scan_button' .format(
scan)).setText('Stop Scan')
getattr(self, '_signal{}Scan' .format(scan)).emit()
Run Code Online (Sandbox Code Playgroud) python ×3
qt ×2
memory ×1
memory-leaks ×1
numpy ×1
pyqt ×1
replace ×1
str-replace ×1
string ×1