使我的 for 循环可执行

发布于 2024-10-13 04:37:41 字数 343 浏览 7 评论 0原文

我已经在 Python 中创建了这个工作 for 循环:

a = 'word'

for i in range(len(a)):

 print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

我需要使其可以从命令行执行,并且还能够接受新参数。

我对如何做到这一点有点模糊。我知道您需要

if __name__ == '__main__':

以某种方式使用...而且我需要使用 sys.argv

如果有人可以解释如何执行此操作,以及从命令行使任何脚本可执行的格式,我将非常感激。

I've created this working for loop in Python:

a = 'word'

for i in range(len(a)):

 print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

and I need to make it executable from the command line and also have it be able to take in new arguments.

I'm a little hazy on how to do this. I know that you need to use

if __name__ == '__main__':

somehow...And that I need to use sys.argv

If anyone could explain how to do this, and the format for making any script executable from the command line, I would really appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

黑色毁心梦 2024-10-20 04:37:41

您可以简单地编写

import sys
a = sys.argv[1]
etc..

然后运行:

python yourcode.py argument

if __name__=='__main__':

不是必需的,但您也可以将整个代码放在该下面。其目的是指定仅当您运行“python yourcode.py”之类的程序时才执行的代码,并防止在另一个 .py 文件中写入“import yourcode”时执行该 if 语句下的代码。

You can just simply write

import sys
a = sys.argv[1]
etc..

And then run:

python yourcode.py argument

The

if __name__=='__main__':

is not required, but you can also put your whole code under that. It's purpose is to specify code which gets executed only if you run your program like "python yourcode.py" and to prevent the code under that if statement from getting executed if you write "import yourcode" in another .py file.

荭秂 2024-10-20 04:37:41

根据您希望 Python 脚本的灵活性,我会像这样构建脚本:

def get_args():
    # logic for parsing arguments here
    # return e.g. a dictionary

def your_method(arg1=None,arg2=...):
    # further logic

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

有各种模块可以解析命令行参数。看看 argparse (更简单)和optparse

如果您只需要一种简单的方法来访问命令行参数,您可以使用 sys.argv

通过这种分离,您还可以将函数导入到其他代码中。

示例:

import sys

def get_args():
    word = sys.argv[1] if (len(sys.argv) > 1) else ''
    return {"word": word}

def your_method(word=''):
    for i in range(len(word)):
        print word[i:] + word[:i], (word[i:] + word[:i])[::-1]

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

运行它

python yourscript.py someword

并使用另请参阅:

Depending on how flexible you want to have your Python script, I would structure the script like so:

def get_args():
    # logic for parsing arguments here
    # return e.g. a dictionary

def your_method(arg1=None,arg2=...):
    # further logic

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

There are various modules to parse command line arguments. Have a look at argparse (easier) and optparse.

If you just need a simple way to access the command line arguments, you can go with sys.argv.

With this separation you are also able to import your function into other code.

Example:

import sys

def get_args():
    word = sys.argv[1] if (len(sys.argv) > 1) else ''
    return {"word": word}

def your_method(word=''):
    for i in range(len(word)):
        print word[i:] + word[:i], (word[i:] + word[:i])[::-1]

if __name__ == "__main__":
    args = get_args()
    your_method(**args)

and run it with

python yourscript.py someword

See also:

忆离笙 2024-10-20 04:37:41
import sys
if __name__ == '__main__':
    a = sys.argv[1] #sys.argv[0] is your file name, and [1] is the next argument
    for i in range(len(a)):
        print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

然后,您只需更改到包含该文件的目录,并像这样调用它:

python file.py wordHere

在命令行上。

import sys
if __name__ == '__main__':
    a = sys.argv[1] #sys.argv[0] is your file name, and [1] is the next argument
    for i in range(len(a)):
        print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

Then you simply change to the directory with the file, and call it like:

python file.py wordHere

On the command line.

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