Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #62080 > unrolled thread
| Started by | Ravi Prabakaran <ravi.itm@gmail.com> |
|---|---|
| First post | 2013-12-16 09:16 -0800 |
| Last post | 2013-12-16 15:23 -0500 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Ravi Prabakaran <ravi.itm@gmail.com> |
|---|---|
| Date | 2013-12-16 09:16 -0800 |
| Subject | Concatenate string list to number list to form title - Logic needed. |
| Message-ID | <2333bfb4-cd72-4ed0-9b28-d8dbe26b5be2@googlegroups.com> |
Hi,
I'm completely new to python. I just need simple logic to get output without any loops.
I have list of string and list of list of 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 ?
Thanks
Ravi
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-17 04:29 +1100 |
| Message-ID | <mailman.4224.1387214986.18130.python-list@python.org> |
| In reply to | #62080 |
On Tue, Dec 17, 2013 at 4:16 AM, Ravi Prabakaran <ravi.itm@gmail.com> wrote: > Could anyone please guide me with best solution without loops ? > Why "without loops"? The best solution, in my opinion, is a loop. Is this a specific challenge (homework)? I could make you a list comprehension, but that's really just another form of loop More information on the problem parameters, please? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-17 04:51 +1100 |
| Message-ID | <mailman.4226.1387216286.18130.python-list@python.org> |
| In reply to | #62081 |
On Tue, Dec 17, 2013 at 4:41 AM, Ravi Prabakaran <ravi.itm@gmail.com> wrote:
>> Hi Chris,
>
> Thanks for reply. If you have any good idea with loop, please post. But
> i'm looking same without loop because python has slicing,concatenating and
> other straight forward feature. I guess it can be done without loop. My
> client does not prefer loops and expects simple and neat code to improve
> performance. We are dealing with billion data.
I'm going to hope that it was in error that you sent this off-list, or
at least that you won't mind my replying on-list. Here's one way to do
it:
t = ['Start','End']
a = [[1,2,3,4],
[5,6,7,8]]
result = []
for cur in a:
result.append("%s - %d"%(t[0],cur[2]))
result.append("%s - %d"%(t[1],cur[3]))
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-12-17 04:52 +1100 |
| Message-ID | <mailman.4227.1387216355.18130.python-list@python.org> |
| In reply to | #62081 |
On Tue, Dec 17, 2013 at 4:51 AM, Chris Angelico <rosuav@gmail.com> wrote:
> t = ['Start','End']
> a = [[1,2,3,4],
> [5,6,7,8]]
> result = []
> for cur in a:
> result.append("%s - %d"%(t[0],cur[2]))
> result.append("%s - %d"%(t[1],cur[3]))
Whoops, I misread the desired output, I thought you wanted a
four-string list. It's two strings. That's actually easier, and Mark's
solution is pretty much what I'd go for.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-12-16 17:43 +0000 |
| Message-ID | <mailman.4225.1387215798.18130.python-list@python.org> |
| In reply to | #62080 |
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 and list of list of 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 ?
>
> Thanks
> Ravi
>
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]
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-12-16 20:13 +0200 |
| Message-ID | <qotiouomzpd.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #62080 |
Ravi Prabakaran writes:
> I'm completely new to python. I just need simple logic to get output
> without any loops.
> I have list of string and list of list of 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 ?
That's a strange requirement - to have repetition without loops, in
Python, and still have a best solution.
I suppose it's fine to have a (built-in) function do the looping for
you so that there is no explicit loop in your own code. The .format
method of Python strings can do each individual string:
list(map('{2} - {0} , {3} - {1}'
.format('{0[2]}', '{0[3]}', *t)
.format, a))
The first (inner) call of .format builds the actual format string
whose .format method then builds each output string: {0[2]} in a
format string refers to the argument position 0 and its element
position 2; *t spreads the two elements of t as further positional
arguments.
If you have any background in functional programming with lists, map
should be fine and familiar. I would probably build and name the
format string outside the actual call as follows (untested).
start, end = t
format = ( '{2} - {0} , {3} - {1}'
.format('{0[2]}', '{0[3]}', start, end)
.format )
list(map(format, a))
All other things that come to mind would either be too much like loops
or they couldn't possibly be a best solution.
Incidentally, if you want a one-liner and tolerate long lines, the
first form I gave is perfectly good for that purpose.
I think *t and str.format require version 3 of Python.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-12-16 19:37 +0100 |
| Message-ID | <mailman.4229.1387219004.18130.python-list@python.org> |
| In reply to | #62087 |
Jussi Piitulainen wrote:
> Ravi Prabakaran writes:
>
>> I'm completely new to python. I just need simple logic to get output
>> without any loops.
>> I have list of string and list of list of 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 ?
>
> That's a strange requirement - to have repetition without loops, in
> Python, and still have a best solution.
>
> I suppose it's fine to have a (built-in) function do the looping for
> you so that there is no explicit loop in your own code. The .format
> method of Python strings can do each individual string:
>
> list(map('{2} - {0} , {3} - {1}'
> .format('{0[2]}', '{0[3]}', *t)
> .format, a))
Don't do that if t may contain user data. For the sake of the argument let's
assume that a[...][0:2] is confidential. Then
>>> t = "{0[0]}", "{0[1]}"
>>> list(map('{2} - {0} , {3} - {1}'
... .format('{0[2]}', '{0[3]}', *t)
... .format, a))
['1 - 3 , 2 - 4', '5 - 7 , 6 - 8']
(I think doubling the braces is sufficient to fix this)
> The first (inner) call of .format builds the actual format string
> whose .format method then builds each output string: {0[2]} in a
> format string refers to the argument position 0 and its element
> position 2; *t spreads the two elements of t as further positional
> arguments.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-12-16 18:50 +0000 |
| Message-ID | <l8ni1m$s1l$2@reader1.panix.com> |
| In reply to | #62080 |
In <2333bfb4-cd72-4ed0-9b28-d8dbe26b5be2@googlegroups.com> Ravi Prabakaran <ravi.itm@gmail.com> writes:
> Hi,
> I'm completely new to python. I just need simple logic to get output without any loops.
> I have list of string and list of list of 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 ?
output_list = []
t = ['Start','End']
a = [[1,2,3,4],
[5,6,7,8]]
output_list.append('%s - %s, %s - %s' % (t[0], a[0][2], t[1], a[0][3]))
output_list.append('%s - %s, %s - %s' % (t[0], a[1][2], t[1], a[1][3]))
print output_list
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-12-16 15:23 -0500 |
| Message-ID | <mailman.4236.1387225408.18130.python-list@python.org> |
| In reply to | #62080 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web