我正在尝试获取每列数组的最后一个负值的索引(以便在之后对其进行切片).1d向量上的一个简单的工作示例是:
import numpy as np
A = np.arange(10) - 5
A[2] = 2
print A # [-5 -4 2 -2 -1 0 1 2 3 4]
idx = np.max(np.where(A <= 0)[0])
print idx # 5
A[:idx] = 0
print A # [0 0 0 0 0 0 1 2 3 4]
Run Code Online (Sandbox Code Playgroud)
现在我想在2D数组的每一列上做同样的事情:
A = np.arange(10) - 5
A[2] = 2
A2 = np.tile(A, 3).reshape((3, 10)) - np.array([0, 2, -1]).reshape((3, 1))
print A2
# [[-5 -4 2 -2 -1 0 1 …Run Code Online (Sandbox Code Playgroud) 是否可以(?P<toto>...)使用等效的re.findall()?访问正则表达式中定义的符号组名称?
使用re.match(),重新返回一个可以使用MatchObject该功能的.group('toto')...我想做一些接近的事情.
这是一个例子:
import re
my_str = 'toto=1, bip=xyz, toto=15, bip=abu'
print re.findall('toto=(?P<toto>\d+)\,\sbip=(?P<bip>\w+)', my_str)
Run Code Online (Sandbox Code Playgroud)
它返回:
[('1', 'xyz'), ('15', 'abu')]
Run Code Online (Sandbox Code Playgroud)
我想得到类似的东西:
[{'toto':'1', 'bip':'xyz'}, {'toto':'15', 'bip':'abu'}]
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法呢?我在任何地方找不到它......
似乎KeyError消息的管理方式与其他错误不同.例如,如果我想使用颜色,它将适用于IndexError但不适用于KeyError:
err_message = '\x1b[31m ERROR \x1b[0m'
print err_message
raise IndexError(err_message)
raise KeyError(err_message)
Run Code Online (Sandbox Code Playgroud)
知道为什么吗?有没有办法绕过它?(我真的需要KeyError提出类型的例外,以便以后能够捕获它)
我正在尝试join在仅由字符串组成的numpy数组(表示二进制浮点数)上使用该numpy.fromstring函数来获取连接的字符串以便使用该函数,但该join函数似乎无法正常工作.
知道为什么吗?我可以使用哪种替代功能来做到这一点?
这是一个显示我的问题的独立示例:
import numpy as np
nb_el = 10
table = np.arange(nb_el, dtype='float64')
print table
binary = table.tostring()
binary_list = map(''.join, zip(*[iter(binary)] * table.dtype.itemsize))
print 'len binary list :', len(binary_list)
# len binary list : 10
join_binary_list = ''.join(binary_list)
print np.fromstring(join_binary_list, dtype='float64')
# [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
binary_split_array = np.array(binary_list)
print 'nb el :', binary_split_array.shape
# nb el : (10,)
print 'nb_el * size :', binary_split_array.shape[0] …Run Code Online (Sandbox Code Playgroud) 我有一组随机浮点数,我需要将它与另一个以不同顺序具有相同值的浮点数进行比较。就此而言,我使用总和、乘积(以及其他组合,具体取决于表格的维度,因此需要等式的数量)。
尽管如此,当我根据值的顺序对数组执行总和(或乘积)时遇到了精度问题。
这是一个简单的独立示例来说明这个问题:
import numpy as np
n = 10
m = 4
tag = np.random.rand(n, m)
s1 = np.sum(tag, axis=1)
s2 = np.sum(tag[:, ::-1], axis=1)
# print the number of times s1 is not equal to s2 (should be 0)
print np.nonzero(s1 != s2)[0].shape[0]
Run Code Online (Sandbox Code Playgroud)
如果您执行此代码,它有时会告诉您s1和s2不相等,并且差异是计算机精度的数量级。
问题是我需要np.in1d在我无法真正容忍的功能中使用它们......
有没有办法避免这个问题?
如果我有一个numpy数组,例如:
A = np.array([[3, 2], [2, -1], [2, 3], [5, 6], [7,-1] , [8, 9]])
Run Code Online (Sandbox Code Playgroud)
我想将数组的部分与具有-1的子数组分开.请记住,我正在处理非常大的数据集,所以每个操作都可能很长,所以我尝试以最有效的方式记忆内存和CPU时间.
我现在正在做的是:
slicing1 = np.where(A[:, 1] == -1)
with_ones = A[slicing1]
slicing2 = np.setdiff1d(np.arange(A.shape[0]), slicing1, assume_unique=True)
without_ones = A[slicing2]
Run Code Online (Sandbox Code Playgroud)
有没有办法不创建slicing2列表来减少内存消耗,因为它可能非常大?有没有更好的方法来解决这个问题?
我试图有一个语义UI下拉菜单,允许添加,但我似乎可以使它工作.实际上,Add <b>{term}</b>添加内容时应该出现的消息似乎永远不会出现.
这是一个非常简单的例子来测试它http://jsfiddle.net/pybxztu2/8/
如果johny在搜索中输入消息未显示.虽然如果我按下输入它将通过该onChange功能.
此外,甚至可以在这样的菜单中添加bob:即使默认情况下forceSelection: false也需要boby...
也许我只是在Semantic UI doc中遗漏了一些东西......