On 12/16/2013 12:43 PM, Mark Lawrence wrote:
On 16/12/2013 17:16, Ravi Prabakaran wrote:
Hi,
I'm completely new to python. I just need simple logic to get output
without any loops.
I have list of string

The remainder of your description and example imply that this must be a sequence of exactly two strings. In other words, 'list' is both too specific as to class and not specific enough as to length.

>> and list of list of numbers.

While this must be an iterable of sequences of exactly 4 numbers.

Each string should be concatenated with every third and fourth values
to generate proper titles in list of strings.

t = ['Start','End']
a = [[1,2,3,4],
      [5,6,7,8]]

Expected Result : ( list of strings )

['Start - 3 , End - 4',
  'Start - 7 , End - 8']

Note : First 2 values from each list should be ignored.

Could anyone please guide me with best solution without loops ?

If the length of the iterable of sequences is not fixed, a loop of some sort is needed.

I've no idea what your definition of "best" is but this works.

strings = ['{} - {} , {} - {}'.format(t[0], b[-2], t[1], b[-1]) for b in a]

Very nice. The following use the format mini language to do the indexing, avoiding passing the args twice each.

strings = ['{0[0]} - {1[2]} , {0[1]} - {1[3]}'.format(t, b) for b in a]

form = '{src[0]} - {val[2]} , {src[1]} - {val[3]}'
strings = [form.format(src=t, val=b) for b in a]

The first should be faster, but may be less readable. Note that '-2' and '-1' do not work as int indexes in the field names because they would be interpreted as string keys rather than integers.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to