Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'argument': 0.05; 'output': 0.05; 'sufficient': 0.05; 'element': 0.07; 'explicit': 0.07; 'subject:form': 0.07; 'suppose': 0.07; 'string': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sake': 0.09; 'strings.': 0.09; 'subject:number': 0.09; 'subject:string': 0.09; 'python': 0.11; 'assume': 0.14; 'braces': 0.16; 'concatenated': 0.16; 'looping': 0.16; 'loops': 0.16; 'positional': 0.16; 'ravi': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'repetition': 0.16; 'string:': 0.16; 'this)': 0.16; '{0}': 0.16; 'elements': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'solution.': 0.20; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'refers': 0.24; 'fine': 0.24; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'ignored.': 0.30; 'subject:list': 0.30; "i'm": 0.30; 'writes:': 0.31; 'anyone': 0.31; 'third': 0.33; 'actual': 0.34; 'could': 0.34; 'requirement': 0.35; 'there': 0.35; 'method': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'expected': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'new': 0.61; 'simple': 0.61; 'further': 0.61; 'first': 0.61; 'fourth': 0.84; 'spreads': 0.84; 'doubling': 0.91; 'subject:needed.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Concatenate string list to number list to form title - Logic needed. Date: Mon, 16 Dec 2013 19:37:08 +0100 Organization: None References: <2333bfb4-cd72-4ed0-9b28-d8dbe26b5be2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b396.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387219004 news.xs4all.nl 2972 [2001:888:2000:d::a6]:38653 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62090 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.