gpt4 book ai didi

python - 使用定界符 python 跟踪数组中的项目

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:55 25 4
gpt4 key购买 nike

我接受了一个用户提供的输入字符串数组,它看起来像下面这样:

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 转换为 arangelinspace 无论如何)。

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com