-1

我在大学里有 Python 课程,但是我不喜欢编程,而且我对一项练习有疑问。我们必须创建一个如下所示的数组:

阵列的图像

同时拥有 x 列表(从 1.0 到 10.0,如数组中所示)以及 y1 和 y2 的公式:y1=sin(x) 和 y2=sin(x)*2.7^(-x)。我的代码如下所示:

x=[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0]
y1=[]
y2=[]
for i in x:
    l=math.sin(i)
    l=round(l, 5)
    y1.append(l)
for o in x:
    o=math.sin(o)*(pow(2.7, -o))
    o=round(o, 6)
    y2.append(o)

你能帮我把这三个列表合二为一吗?

4

2 回答 2

0

首先,欢迎来到 StackOverflow!

本质上,您需要做的是遍历您的x 列表(在 Python 中用方括号声明的结构是列表,而不是数组),并且对于每个元素,计算您提供的任何一个正弦公式。

获取数字

你在正确的轨道上,但你错过了几件事:

  1. 您忘记了导入math模块(或者您在代码段中省略了该行)。
  2. 要在控制台上获得输出,您需要打印列表xy1y2. 只需在脚本末尾添加三个打印件,如下所示:
# the print function will output everything you give it, separated by a space
print("x:", x)
print("y1:", y1)
print("y2:", y2)

现在,您的脚本应该输出以下内容:

x: [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0]                                                                                             
y1: [0.84147, 0.99749, 0.9093, 0.59847, 0.14112, -0.35078, -0.7568, -0.97753, -0.95892, -0.70554, -0.27942, 0.21512, 0.65699, 0.938, 0.98936, 0.79849, 0.41212, -0.07515, -0.54402]             
y2: [0.311656, 0.224836, 0.124732, 0.049961, 0.00717, -0.010846, -0.014241, -0.011194, -0.006683, -0.002992, -0.000721, 0.000338, 0.000628, 0.000546, 0.00035, 0.000172, 5.4e-05, -6e-06, -2.6e-0
5]

其他一些注意事项:

  • 您不需要为每个for循环更改迭代变量名称。
  • 您不需要使用该pow功能。Python 具有**语法,其行为与通常相同^(在该语言中已经具有另一种含义)。所以你可以重写pow(2.7, -o)2.7**(-o).
  • 如果您不限于使用for循环,您还可以使用非常方便的map功能。它在给定列表上应用一个函数,这正是您想要实现的。你可以“只是”写:
# not doing round to be shorter
y1 = list(map(lambda i: math.sin(i), x))
y2 = list(map(lambda i: math.sin(i)*2.7**(-i), x))
  • 或者,列表推导式存在并且非常简洁,非常适合这里:
y1 = [math.sin(i) for i in x]
y2 = [math.sin(i)*2.7**(-i) for i in x]

将它们打印在表格中

Python 具有相当强大的字符串格式,这使得将这些列表打印为表格非常简单:D
完整代码如下:

# Construct the header
header = f"| {'x':10} | {'y1':10} | {'y2':10} |"
# enough dashes to underline the whole header
line = "-" * len(header)
print(header)
print(line)

# Iterate through the three lists and print them with appropriate 
# spacing and decimal places
for ix, iy1, iy2 in zip(x, y1, y2):
    print(f"| {ix:10.5f} | {iy1:10.5f} | {iy2:10.5f} |")

print("-" * len(header))  # print new dash line

我正在使用f-strings,这使得格式化字符串非常容易。您只需f在开引号前加上一个,然后您就可以在大括号内插入变量。此外,您可以告诉 Python 您希望插入的字符串有多宽,即10列后面的 s 是什么。如果要打印浮点数,您还可以指定所需的小数位数,这是通过在宽度后添加那些.5f(格式为浮点数,带 5 个小数位,带分隔符)来完成的。 最后,该函数允许您同时迭代多个事物。在这种情况下,我用它同时遍历所有三个列表,以在同一行打印它们的每个值。.
zip

最终输出

| x          | y1         | y2         |                                                                                                                                                        
----------------------------------------                                                                                                                                                        
|    1.00000 |    0.84147 |    0.31166 |                                                                                                                                                        
|    1.50000 |    0.99749 |    0.22484 |                                                                                                                                                        
|    2.00000 |    0.90930 |    0.12473 |                                                                                                                                                        
|    2.50000 |    0.59847 |    0.04996 |                                                                                                                                                        
|    3.00000 |    0.14112 |    0.00717 |                                                                                                                                                        
|    3.50000 |   -0.35078 |   -0.01085 |                                                                                                                                                        
|    4.00000 |   -0.75680 |   -0.01424 |                                                                                                                                                        
|    4.50000 |   -0.97753 |   -0.01119 |                                                                                                                                                        
|    5.00000 |   -0.95892 |   -0.00668 |                                                                                                                                                        
|    5.50000 |   -0.70554 |   -0.00299 |                                                                                                                                                        
|    6.00000 |   -0.27942 |   -0.00072 |                                                                                                                                                        
|    6.50000 |    0.21512 |    0.00034 |                                                                                                                                                        
|    7.00000 |    0.65699 |    0.00063 |                                                                                                                                                        
|    7.50000 |    0.93800 |    0.00055 |                                                                                                                                                        
|    8.00000 |    0.98936 |    0.00035 |                                                                                                                                                        
|    8.50000 |    0.79849 |    0.00017 |                                                                                                                                                        
|    9.00000 |    0.41212 |    0.00005 |                                                                                                                                                        
|    9.50000 |   -0.07515 |   -0.00001 |                                                                                                                                                        
|   10.00000 |   -0.54402 |   -0.00003 |
----------------------------------------

我希望这对您有所帮助,并随时在评论中提出问题!

于 2021-11-14T12:30:32.930 回答
0
import math

# You don't have to use one-liners of course, I just wanted the code to be shorter.
x = [i*0.5 for i in range(2, 21)]
y1 = [round(math.sin(i), 5) for i in x]
y2 = [round(math.sin(i)*pow(2.7, -i), 6) for i in x]
print(f'{"x":10} | {"y1":10} | {"y2":10}')  # print with spaces
print('_'*36) # underline
for l1, l2, l3 in zip(x, y1, y2):
    print(f'{l1:10} | {l2:10} | {l3:10}')

输出:

x          | y1         | y2        
____________________________________
       1.0 |    0.84147 |   0.311656
       1.5 |    0.99749 |   0.224836
       2.0 |     0.9093 |   0.124732
       2.5 |    0.59847 |   0.049961
       3.0 |    0.14112 |    0.00717
       3.5 |   -0.35078 |  -0.010846
       4.0 |    -0.7568 |  -0.014241
       4.5 |   -0.97753 |  -0.011194
       5.0 |   -0.95892 |  -0.006683
       5.5 |   -0.70554 |  -0.002992
       6.0 |   -0.27942 |  -0.000721
       6.5 |    0.21512 |   0.000338
       7.0 |    0.65699 |   0.000628
       7.5 |      0.938 |   0.000546
       8.0 |    0.98936 |    0.00035
       8.5 |    0.79849 |   0.000172
       9.0 |    0.41212 |    5.4e-05
       9.5 |   -0.07515 |     -6e-06
      10.0 |   -0.54402 |   -2.6e-05
于 2021-11-14T12:20:45.567 回答