- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我接受了一个用户提供的输入字符串数组,它看起来像下面这样:
x=[100.0,150.0,200.0:300.0:10.0,300.0,350.0:400.0:10.0,500.0,600.0:700.0:10.0,800.0,900.0]
由于这些是用户提供的列表,间隔切片的顺序 [例如 200.0:300.0:10.0] 可能会有所不同,没有切片的单个条目也会有所不同。
然后我在 ':,"分隔符上拆分,这样我就可以从 float 转换为 string 以便在 numpy.r_ 中使用。然后我得到以下列表:
x_arr=[100.0,150.0,200.0,300.0,10.0,300.0,350.0,400.0,10.0,500.0,600.0,700.0,10.0,800.0,900.0]
我想跟踪存在“:”定界符的原始索引以及不存在“:”定界符的位置,以便我可以通过以下方式将原始数组重建为一系列 float :
np.r_[100.0, 150.0, slice(200.0,300.0,10.0), 300, slice(350.0,400.0,10.0), 500.0, slice(600,700,10),800,900]
问题是如何以一致的方式跟踪从原始数组到新数组的索引变化。对于如何使用随机用户提供的输入最好地实现这一点,我将不胜感激。
这是我考虑过的一种方法:
我将原始数组拆分为“,”以查找缺少“:”分隔符的元素:
x_no_colon=re.split((','),x)
xh=[]
for ind in x_no_colon:
inds_wo_colon=re.findall(":",ind)
xh.append(inds_wo_colon)
使用上面的例子会返回以下内容:
xh=[[],[],[":",":"],[],[":",":"],[],[":",":"],[],[]]
然后我可以通过以下方式识别没有冒号的索引:
x_wo_colons = [item for item in range(len(xh)) if xh[item] == []]
会返回:
x_wo_colons=[0,1,3,6,8,9]
然后我使用在“:”上拆分的数组找到带有“:”分隔符的索引:
colon_arr=re.split('(:)',x)
prelim_x_with_colon=[item for item in range(len(colon_arr)) if colon_arr[item] == ':']
x_w_colon=[]
for i in prelim_x_with_colon:
if i == 1 and colon_arr[1] != ':':
x_w_colon.append(i)
elif i == 1 and colon_arr[1] == ':':
x_w_colon.append(i-1)
else:
x_w_colon_append(i-1)
对于存在冒号和不存在冒号的索引列表,唯一要做的就是从带冒号的列表中删除不带冒号的索引。我在这里发现的问题是,对于不同的列表,每次都很难使索引正确。这可能是因为我的方法很复杂,并且我为不同的列表使用了两个不同的数组。
问题是如何以一致的方式跟踪从原始数组到新数组的索引变化。对于如何使用随机用户提供的输入最好地实现这一点,我将不胜感激。
提前致谢!
最佳答案
考虑到某些项目看起来像切片,您是否正在尝试将此输入字符串/列表转换为数字列表/数组?
这是我对您的字符串进行的实验(减去 []
)。我会留下大量的试验和错误。这可能是有启发性的。
In [957]: txt='100.0,150.0,200.0:300.0:10.0,300.0,350.0:400.0:10.0,500.0,600.0:700.0:10.0,800.0,900.0'
我假设 ,
是主要的分隔符,:
是次要的
In [958]: txt.split(',')
Out[958]:
['100.0',
'150.0',
'200.0:300.0:10.0',
'300.0',
'350.0:400.0:10.0',
'500.0',
'600.0:700.0:10.0',
'800.0',
'900.0']
定义一个函数来处理这些项目之一:
In [960]: def foo(astr):
...: items=astr.split(':')
...: if len(items)==1:
...: return float(items[0])
...: else:
...: return slice(*[float(i) for i in items])
...:
In [961]: [foo(s) for s in txt.split(',')]
Out[961]:
[100.0,
150.0,
slice(200.0, 300.0, 10.0),
300.0,
slice(350.0, 400.0, 10.0),
500.0,
slice(600.0, 700.0, 10.0),
800.0,
900.0]
In [962]: np.r_[_]
Out[962]:
array([100.0, 150.0, slice(200.0, 300.0, 10.0), 300.0,
slice(350.0, 400.0, 10.0), 500.0, slice(600.0, 700.0, 10.0), 800.0,
900.0], dtype=object)
它像我预期的那样创建切片,但是 np.r_
不接受文字切片;它需要 :
语法。实际上,是 Python 解释器将 [a:b:c]
转换为 slice(a,b,c)
对象。似乎我们最近解决了这个问题。与其打架,不如直接跳到 arange
(因为 np.r_
将 slices
转换为 arange
或 linspace
无论如何)。
In [963]: def foo(astr):
...: items=astr.split(':')
...: if len(items)==1:
...: return float(items[0])
...: else:
...: return np.arange(*[float(i) for i in items])
In [964]: [foo(s) for s in txt.split(',')]
Out[964]:
[100.0,
150.0,
array([ 200., 210., 220., 230., 240., 250., 260., 270., 280., 290.]),
300.0,
array([ 350., 360., 370., 380., 390.]),
500.0,
array([ 600., 610., 620., 630., 640., 650., 660., 670., 680., 690.]),
800.0,
900.0]
In [965]: np.concatenate(_)
...
ValueError: zero-dimensional arrays cannot be concatenated
糟糕,concatenate
不喜欢单个数字;
In [966]: def foo(astr):
...: items=astr.split(':')
...: if len(items)==1:
...: return [float(items[0])]
...: else:
...: return np.arange(*[float(i) for i in items])
In [967]: [foo(s) for s in txt.split(',')]
Out[967]:
[[100.0],
[150.0],
array([ 200., 210., 220., 230., 240., 250., 260., 270., 280., 290.]),
[300.0],
array([ 350., 360., 370., 380., 390.]),
[500.0],
array([ 600., 610., 620., 630., 640., 650., 660., 670., 680., 690.]),
[800.0],
[900.0]]
In [968]: np.concatenate(_)
Out[968]:
array([ 100., 150., 200., 210., 220., 230., 240., 250., 260.,
270., 280., 290., 300., 350., 360., 370., 380., 390.,
500., 600., 610., 620., 630., 640., 650., 660., 670.,
680., 690., 800., 900.])
看起来不错。
=======================
在最近的回答中,我确实找到了一种将文字 slice
对象传递给元组中的 r_
的方法。
In [969]: def foo1(astr):
...: items=astr.split(':')
...: if len(items)==1:
...: return float(items[0])
...: else:
...: return slice(*[float(i) for i in items])
...
In [971]: tuple([foo1(s) for s in txt.split(',')])
Out[971]:
(100.0,
150.0,
slice(200.0, 300.0, 10.0),
300.0,
slice(350.0, 400.0, 10.0),
500.0,
slice(600.0, 700.0, 10.0),
800.0,
900.0)
In [972]: np.r_[tuple([foo1(s) for s in txt.split(',')])]
Out[972]:
array([ 100., 150., 200., 210., 220., 230., 240., 250., 260.,
270., 280., 290., 300., 350., 360., 370., 380., 390.,
500., 600., 610., 620., 630., 640., 650., 660., 670.,
680., 690., 800., 900.])
关于python - 使用定界符 python 跟踪数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689028/
我正在尝试创建一个包含 int[][] 项的数组 即 int version0Indexes[][4] = { {1,2,3,4}, {5,6,7,8} }; int version1Indexes[
我有一个整数数组: private int array[]; 如果我还有一个名为 add 的方法,那么以下有什么区别: public void add(int value) { array[va
当您尝试在 JavaScript 中将一个数组添加到另一个数组时,它会将其转换为一个字符串。通常,当以另一种语言执行此操作时,列表会合并。 JavaScript [1, 2] + [3, 4] = "
根据我正在阅读的教程,如果您想创建一个包含 5 列和 3 行的表格来表示这样的数据... 45 4 34 99 56 3 23 99 43 2 1 1 0 43 67 ...它说你可以使用下
我通常使用 python 编写脚本/程序,但最近开始使用 JavaScript 进行编程,并且在使用数组时遇到了一些问题。 在 python 中,当我创建一个数组并使用 for x in y 时,我得
我有一个这样的数组: temp = [ 'data1', ['data1_a','data1_b'], ['data2_a','data2_b','data2_c'] ]; // 我想使用 toStr
rent_property (table name) id fullName propertyName 1 A House Name1 2 B
这个问题在这里已经有了答案: 关闭13年前。 Possible Duplicate: In C arrays why is this true? a[5] == 5[a] array[index] 和
使用 Excel 2013。经过多年的寻找和适应,我的第一篇文章。 我正在尝试将当前 App 用户(即“John Smith”)与他的电子邮件地址“jsmith@work.com”进行匹配。 使用两个
当仅在一个边距上操作时,apply 似乎不会重新组装 3D 数组。考虑: arr 1),但对我来说仍然很奇怪,如果一个函数返回一个具有尺寸的对象,那么它们基本上会被忽略。 最佳答案 这是一个不太理
我有一个包含 GPS 坐标的 MySQL 数据库。这是我检索坐标的部分 PHP 代码; $sql = "SELECT lat, lon FROM gps_data"; $stmt=$db->query
我需要找到一种方法来执行这个操作,我有一个形状数组 [批量大小, 150, 1] 代表 batch_size 整数序列,每个序列有 150 个元素长,但在每个序列中都有很多添加的零,以使所有序列具有相
我必须通过 url 中的 json 获取文本。 层次结构如下: 对象>数组>对象>数组>对象。 我想用这段代码获取文本。但是我收到错误 :org.json.JSONException: No valu
enter code here- (void)viewDidLoad { NSMutableArray *imageViewArray= [[NSMutableArray alloc] init];
知道如何对二维字符串数组执行修剪操作,例如使用 Java 流 API 进行 3x3 并将其收集回相同维度的 3x3 数组? 重点是避免使用显式的 for 循环。 当前的解决方案只是简单地执行一个 fo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有来自 ASP.NET Web 服务的以下 XML 输出: 1710 1711 1712 1713
如果我有一个对象todo作为您状态的一部分,并且该对象包含数组列表,则列表内部有对象,在这些对象内部还有另一个数组listItems。如何更新数组 listItems 中 id 为“poi098”的对
我想将最大长度为 8 的 bool 数组打包成一个字节,通过网络发送它,然后将其解压回 bool 数组。已经在这里尝试了一些解决方案,但没有用。我正在使用单声道。 我制作了 BitArray,然后尝试
我们的数据库中有这个字段指示一周中的每一天的真/假标志,如下所示:'1111110' 我需要将此值转换为 boolean 数组。 为此,我编写了以下代码: char[] freqs = weekday
我是一名优秀的程序员,十分优秀!