Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62099
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Concatenate string list to number list to form title - Logic needed. |
| Date | 2013-12-16 15:23 -0500 |
| References | <2333bfb4-cd72-4ed0-9b28-d8dbe26b5be2@googlegroups.com> <l8ne2u$jhb$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4236.1387225408.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Concatenate string list to number list to form title - Logic needed. Ravi Prabakaran <ravi.itm@gmail.com> - 2013-12-16 09:16 -0800
Re: Concatenate string list to number list to form title - Logic needed. Chris Angelico <rosuav@gmail.com> - 2013-12-17 04:29 +1100
Re: Concatenate string list to number list to form title - Logic needed. Chris Angelico <rosuav@gmail.com> - 2013-12-17 04:51 +1100
Re: Concatenate string list to number list to form title - Logic needed. Chris Angelico <rosuav@gmail.com> - 2013-12-17 04:52 +1100
Re: Concatenate string list to number list to form title - Logic needed. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-16 17:43 +0000
Re: Concatenate string list to number list to form title - Logic needed. Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-12-16 20:13 +0200
Re: Concatenate string list to number list to form title - Logic needed. Peter Otten <__peter__@web.de> - 2013-12-16 19:37 +0100
Re: Concatenate string list to number list to form title - Logic needed. John Gordon <gordon@panix.com> - 2013-12-16 18:50 +0000
Re: Concatenate string list to number list to form title - Logic needed. Terry Reedy <tjreedy@udel.edu> - 2013-12-16 15:23 -0500
csiph-web