如何在另一条指令之后在python中编写内联for循环?

时间:2015-07-29 09:27:32

标签: python for-loop command-line

for作品开始

root@messagerie-secours[10.10.10.19] /home/serveur # python -c "for x in xrange(10):print x;"
0
1
2
3
4
5
6
7
8
9
root@messagerie-secours[10.10.10.19] /home/serveur # 

如果你在中间,那就是语法错误:

root@messagerie-secours[10.10.10.19] /home/serveur # python -c "a=2;for x in xrange(10):print x;"
  File "<string>", line 1
    a=2;for x in xrange(10):print x;
          ^
SyntaxError: invalid syntax
root@messagerie-secours[10.10.10.19] /home/serveur #

是否有可能摆脱语法错误?

6 个答案:

答案 0 :(得分:2)

将mutlitple语句和控制结构写在一行中并不是一个好主意,因为Python在很大程度上依赖于缩进。

您可以将代码正确缩进到loop.py文件中,您应该没问题。

#!/usr/bin/nev python

a = 2
for x in xrange(10):
    print x

使用python loop.py运行它。

如果您确实需要在命令行中编写的程序,请尝试以下方法:

root@messagerie-secours[10.10.10.19] /home/serveur # python -c "
> a = 2
> for x in range(10) :
>     print x
> "

答案 1 :(得分:2)

这取决于底层操作系统。在Unix上很容易:

0
1
2
3
4

正确给出

    WARNING: Exception thrown from LifecycleProcessor on context close
    java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Wed Jul 29 15:07:09 IST 2015]; root of context hierarchy
        at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:350)
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1033)
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)
        at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:578)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4831)
        at 
org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5478)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

在我的FreeBSD系统上,因为Unix shell允许在引号之间换行。

但是AFAIK,在Windows CMD shell中是不可能的。

答案 2 :(得分:2)

哇,有很多解决方案!这里还有其他一些:

python -c 'print "\n".join(map(str, xrange(10)))'

python <<"EOF"
for x in range(10):
  print x
EOF

echo $'a=12\nfor x in range(a): print x' | python

答案 3 :(得分:0)

您可以使用itertools.count,这将产生0,1,2 ...... ....

使用__import__,您可以在一行中执行此操作:

python -c 'for x in __import__("itertools").count(): print x'

答案 4 :(得分:0)

也许这就是你要找的东西。看看ThomasH的回答。

How to put multiple statements in one line

答案 5 :(得分:0)

以下适用于Python 2.7:

python -c "exec('\n'.join(['a=42', 'for x in xrange(10):', '  print x', 'print a']))"

给出输出:

0
1
2
3
4
5
6
7
8
9
42