简体   繁体   English

这个用Python编写的程序的过程是怎样的?

[英]What is the process of this program written in Python?

I have to understand the process of a program which looks like measuring the performance of multiplication for several repeat sizes.我必须了解一个程序的过程,该程序看起来像是测量多个重复大小的乘法性能。

I tried to execute the program, but there was no clue to understand it from the output.我试图执行该程序,但没有从输出中理解它的线索。 I need a help how to use this program and please let me know what are input and output of it.我需要如何使用这个程序的帮助,请让我知道它的输入和输出是什么。

import sys
import numpy as np
import matplotlib.pyplot as plt

if len(sys.argv) != 2:
    print ("usage:", sys.argv[0], "<filename>")
    exit()

a = np.loadtxt(sys.argv[1])

print(a)

list1, list2 = zip(*a)
plt.plot(list1, list2)
plt.show()

When I executed the above code, its output was below.当我执行上面的代码时,它的输出如下。

$ python sample.py
usage: sample.py <filename>

trial following the answer回答后的审判

I have executed the program like below.我已经执行了如下所示的程序。 There was no change on the code of "sample.py". “sample.py”的代码没有变化。 How can I fix the error and what is the appropriate content in fileToRead.txt?如何修复错误以及 fileToRead.txt 中的适当内容是什么?

$ python sample.py fileToRead.txt
3.1415926535
Traceback (most recent call last):
  File "arrmultbysize.py", line 24, in <module>
    list1, list2 = zip(*a)
TypeError: iteration over a 0-d array

fileToRead.txt文件读取.txt

3.1415926535

It seems you are new to the world of programming.看来您是编程世界的新手。 "sys.argv" is used to take Command Line Arguments . “sys.argv”用于获取命令行参数

  • when you run as "python sample.py", the variable sys.argv will be a single element list ie ["sample.py"]当您作为“python sample.py”运行时,变量 sys.argv 将是一个单元素列表,即 ["sample.py"]
  • len(sys.argv) is 1 in this case在这种情况下 len(sys.argv) 为 1

The Expected Working of the program is:该计划的预期工作是:

  • when you run as "python sample.py fileToRead.txt", the variable sys.argv will be a two element list ie ["sample.py","fileToRead.txt"]当您作为“python sample.py fileToRead.txt”运行时,变量 sys.argv 将是一个两元素列表,即 ["sample.py","fileToRead.txt"]
  • len(sys.argv) is 2 in this case在这种情况下 len(sys.argv) 为 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM