2

I'm looking to create the following list in python:

list = ["pix420_615", "pix421_615", ... , "pix425_620"]

N.B. Where the list is a list of strings, not values.

From the following two lists:

x= [420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425,
 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425]

y= [615, 615, 615, 615, 615, 615, 616, 616, 616, 616, 616, 616, 617, 617, 617, 617, 617, 617,
 618, 618, 618, 618, 618, 618, 619, 619, 619, 619, 619, 619, 620, 620, 620, 620, 620, 620]

I was wondering how I would go about doing this, and maintain the consistency of corresponding array values...?

  • 1
    Do you mean `list = ['pix420_615', 'pix421_615', ...]` where each element is a string? If not, what are the values `pix420_615`, etc.? – user94559 Jul 14 '16 at 16:38
  • @smarx Yeh, where each element is just a string created from adding pix to the x and y arrays in sequential order... –  Jul 14 '16 at 16:38
  • quite simply: `map(lambda t: 'pix%s_%s' % t, zip(x, y))` (or, if you don't like map: `['pix%s_%s' % t for t in zip(x, y)]`) – njzk2 Jul 14 '16 at 16:40

3 Answers3

2

This should do the trick. zip is the nicest way to do this sort of thing:

x = [420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425, 420, 421, 422, 423, 424, 425]

y = [615, 615, 615, 615, 615, 615, 616, 616, 616, 616, 616, 616, 617, 617, 617, 617, 617, 617, 618, 618, 618, 618, 618, 618, 619, 619, 619, 619, 619, 619, 620, 620, 620, 620, 620, 620]

z = ['pix{}_{}'.format(a, b) for a, b in zip(x, y)]

print(z)

# Output:
# ['pix420_615', 'pix421_615', 'pix422_615', 'pix423_615', 'pix424_615', 'pix425_615', 'pix420_616', 'pix421_616', 'pix422_616', 'pix423_616', 'pix424_616', 'pix425_616', 'pix420_617', 'pix421_617', 'pix422_617', 'pix423_617', 'pix424_617', 'pix425_617', 'pix420_618', 'pix421_618', 'pix422_618', 'pix423_618', 'pix424_618', 'pix425_618', 'pix420_619', 'pix421_619', 'pix422_619', 'pix423_619', 'pix424_619', 'pix425_619', 'pix420_620', 'pix421_620', 'pix422_620', 'pix423_620', 'pix424_620', 'pix425_620']
user94559
  • 59,196
  • 6
  • 103
  • 103
2

From your lists,

map(lambda t: 'pix%s_%s' % t, zip(x, y))

or

['pix%s_%s' % t for t in zip(x, y)]

However, I notice that the first list is basically [420, ..., 425], and the second one is [615, ...], which result in zip(x, y) representing the product of range(420, 426) and range(620, 626).

You can simplify all that by:

from itertools import product

['pix%s_%s' % (y, x) for x, y in product(range(615, 621), range(420, 426))]
njzk2
  • 38,969
  • 7
  • 69
  • 107
  • 1
    One *tiny* nit-pick. The small optimization benefit from using `map` over a list comprehension is lost [when using `lambda`](http://stackoverflow.com/a/1247490/1555990). I do like the `itertools.product` solution, though. – That1Guy Jul 14 '16 at 16:51
0

use map function

map(lambda a, b: 'pix{0}_{1}'.format(a,b), x, y)
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24