python中的双循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14379103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:14:22  来源:igfitidea点击:

double for loops in python

python

提问by Vijay

I am trying to get a list of file names in order. Something like

我正在尝试按顺序获取文件名列表。就像是

files-1-loop-21
files-1-loop-22
files-1-loop-23
files-1-loop-24
files-2-loop-21
files-2-loop-22
files-2-loop-23
.
.
.
and so on

As for testing I have written python code as below:

至于测试,我编写了如下python代码:

code sample_1:

代码示例_1:

for md in range(1,5):
   for pico in range(21,25):
      print md, pico

It gives me pair of number such as:

它给了我一对数字,例如:

  `1 21
   1 22
   1 23
   1 24
   2 21
   2 22
   2 23
   2 24
   3 21
   3 22
   3 23
   3 24
   4 21
   4 22
   4 23
   4 24

`

`

if I use:

如果我使用:

code sample_2:

代码示例_2:

 for md in range(1,5):
   for pico in range(21,25):
  print "file-md-loop-pico"

I get

我得到

  files-md-loop-pico
  files-md-loop-pico
  files-md-loop-pico
  files-md-loop-pico
  files-md-loop-pico
  files-md-loop-pico
  files-md-loop-pico

How (code sample_2) should be altered to get the file lists as I wanted (as shown in the beginning of this post) in python?

应该如何更改(代码示例_2)以在 python 中获取我想要的文件列表(如本文开头所示)?

Thanks in advance.

提前致谢。

Regards

问候

采纳答案by Artsiom Rudzenka

Try this:

尝试这个:

for md in range(1,5):
   for pico in range(21,25):
      print "file-{0}-loop-{1}".format(md, pico)

Or:

或者:

from itertools import product
for md, pico in product(range(1,5), range(21,25)):
    print "file-{0}-loop-{1}".format(md, pico)

回答by Volatility

Use string formatting.

使用字符串格式。

print 'file-{0}-loop-{1}'.format(md, pico)

回答by AlexFoxGill

1) Python scope is determined by indentation. Your inner code must be properly indented:

1) Python 范围由缩进决定。您的内部代码必须正确缩进:

for md in range(1,5):
    for pico in range(21,25):
        print "file-md-loop-pico"

2) You are using a string literal "file-md-loop-pico"rather than inserting your variables mdand pico. To format your string correctly, use:

2)您使用的是字符串文字,"file-md-loop-pico"而不是插入变量mdpico. 要正确格式化字符串,请使用:

for md in range(1,5):
    for pico in range(21,25):
        print "file-{0}-loop-{1}".format(md, pico)

回答by Peter Wooster

The solution, provided in the other answers,with arguments surrounded by {} is the correct modern way to do this

解决方案,在其他答案中提供,参数被 {} 包围是正确的现代方法

print'file-{0}-loop-{1}'.format{md,poco)

There is an older way to do this that you will probably see, and that might make more sense if you are familiar with c or c++ sprintf

您可能会看到一种较旧的方法来执行此操作,如果您熟悉 c 或 c++ sprintf,这可能更有意义

print'file-%d-loop-%d'%(md,poco)

The documentation: http://docs.python.org/2/tutorial/inputoutput.html

文档:http: //docs.python.org/2/tutorial/inputoutput.html

回答by vtlinh

one-liner:

单线:

print '\n'.join([('files-%d-loop_%d' %(i, j)) for i in xrange(1,5) for j in xrange(21,25)])

回答by DEBLEEN BISWAS

This will solve the problem easily:

这将轻松解决问题:

Code:

代码:

for md in range(1,5):
       for pico in range(21,25):

            print "file-%d-loop-%d"%(md,pico)

Output:

输出:

file-1-loop-21
file-1-loop-22
file-1-loop-23
file-1-loop-24
file-2-loop-21
file-2-loop-22
file-2-loop-23
file-2-loop-24
file-3-loop-21
file-3-loop-22
file-3-loop-23
file-3-loop-24
file-4-loop-21
file-4-loop-22
file-4-loop-23
file-4-loop-24

回答by Sandeep Gupta

for md in range(1,5,1):
  for pico in range(21,25,1):
     print ("file-" + str(md) +"-loop-" + str(pico) )