8

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:

code sample_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:

 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?

Thanks in advance.

Regards

Vijay
  • 965
  • 5
  • 13
  • 27

7 Answers7

18

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)
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
2

Use string formatting.

print 'file-{0}-loop-{1}'.format(md, pico)
Volatility
  • 31,232
  • 10
  • 80
  • 89
  • Lets say I have three loops, can I use as ( print "files-{0}-loop-{1}-varies-{2}".format(md,pico,jay) )? Is that possible explain what the meaning {0} and {1}? – Vijay Jan 17 '13 at 12:37
  • 1
    @Vijay sure you can, the numbers are references to which paramater of `format` you want to include in the brackets. – Volatility Jan 17 '13 at 12:38
2

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

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 md and pico. To format your string correctly, use:

for md in range(1,5):
    for pico in range(21,25):
        print "file-{0}-loop-{1}".format(md, pico)
Community
  • 1
  • 1
Alex
  • 7,639
  • 3
  • 45
  • 58
  • 1
    Considering the output, it's unlikely indentation is the problem; most likely the OP just formatted the SO post incorrectly and not the actual code. – Wooble Jan 17 '13 at 13:02
2

one-liner:

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

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

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

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

Peter Wooster
  • 6,009
  • 2
  • 27
  • 39
0

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
oz123
  • 27,559
  • 27
  • 125
  • 187
0
for md in range(1,5,1):
  for pico in range(21,25,1):
     print ("file-" + str(md) +"-loop-" + str(pico) )