将代码从Perl转换为Python进行表处理

发布于 2025-02-03 06:30:26 字数 611 浏览 1 评论 0原文

我在Perl中有此代码,使我可以创建8到1的列表并创建编号表。 阵列是通过在“后缀”之前添加到列表数据(即8至1)的填充的数组,1.1] 。 1 ..1515.1 15.2 15.316..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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2025-02-10 06:30:26

由于您使用的是numpy,因此无法像常规的python列表那样直接在数组上进行附加。相反,您需要使用类函数 append()代码>。此外,也是因为代码> 要求输入您需要使用np.empty(0)来创建一个空数组。

import numpy as np

lst = list(range(1, 8+1))
lst.reverse()
print(lst)
numerotation = np.empty(0)

for i in lst :
    d = "1." + str(i)
    numerotation = np.append(numerotation, d)

print(numerotation)

在您的perl代码中,您将浮子创建为字符串,因此我在此示例中以这种方式离开了它们。为了使它们成为数字,您需要使用float(d)转换它们。

这是另一种使用常规python列表,然后从中创建一个numpy数组的方法。

import numpy as np

lst = list(range(1, 8+1))
lst.reverse()
print(lst)
numbers = ["1." + str(i) for i in lst]

numerotation = np.array(numbers)

print(numerotation)

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 since np.array() requires input you need to use np.empty(0) to create an empty array.

import numpy as np

lst = list(range(1, 8+1))
lst.reverse()
print(lst)
numerotation = np.empty(0)

for i in lst :
    d = "1." + str(i)
    numerotation = np.append(numerotation, d)

print(numerotation)

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.

import numpy as np

lst = list(range(1, 8+1))
lst.reverse()
print(lst)
numbers = ["1." + str(i) for i in lst]

numerotation = np.array(numbers)

print(numerotation)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文