将代码从Perl转换为Python进行表处理
我在Perl中有此代码,使我可以创建8到1的列表并创建编号表。 阵列是通过在“后缀”之前添加到列表数据(即8至1)的填充的数组,1.1] 。 1 ..15
和15.1 15.2 15.3
和16..32
的值填充数组的值
然后用 替换推送功能。我将您的代码和我在Python中的测试中放置为:没有: perl中的代码:
for ( reverse( 1 .. 8 ) ) { push @Numerotation, '1.' . $_ } push
@Numerotation, ( 1 .. 15, 15.1, 15.2, 15.3, 16 .. 31, 34 .. 45 );
python中的代码(不起作用):
import numpy as np
lst = list(range(1,8+1))
lst.reverse()
print(lst)
numerotation= np.array()
for i in lst :
d=1.+i
numerotation.append(d)
print(numerotation)
I have this code in perl that allows me to create a list of 8 to 1 and to create a numbering table.
the array is filled by adding to the list data (i.e. 8 to 1) before a "suffix" which is 1. i.e. inside the array we will have numrotation [1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1]
. then fill the array with the values of 1 ..15
and 15.1 15.2 15.3
and 16..32
I would like to know which function can replace the push functions. I put you the code in perl and my test in python which did not :
code in perl :
for ( reverse( 1 .. 8 ) ) { push @Numerotation, '1.' . $_ } push
@Numerotation, ( 1 .. 15, 15.1, 15.2, 15.3, 16 .. 31, 34 .. 45 );
code in python (does not work) :
import numpy as np
lst = list(range(1,8+1))
lst.reverse()
print(lst)
numerotation= np.array()
for i in lst :
d=1.+i
numerotation.append(d)
print(numerotation)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的是numpy,因此无法像常规的python列表那样直接在数组上进行附加。相反,您需要使用类函数
append()代码>
。此外,也是因为代码> 要求输入您需要使用
np.empty(0)
来创建一个空数组。在您的perl代码中,您将浮子创建为字符串,因此我在此示例中以这种方式离开了它们。为了使它们成为数字,您需要使用
float(d)
转换它们。这是另一种使用常规python列表,然后从中创建一个numpy数组的方法。
Since you are using numpy you can't do an append directly on the array like you can with regular Python lists. Instead you need to use the class function
append()
. Also sincenp.array()
requires input you need to usenp.empty(0)
to create an empty array.In your Perl code you create the floats as strings so I left them that way in this example. To make them numbers you need to convert them using
float(d)
.Here is another approach that uses a regular Python list and then creates a numpy array from it.