[Tutor] Using 'join ' function to create a string
Hi there, I would like to know what is the best way to create a string object from two different lists using 'join' function. For example, I have X = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'. Any help would greatly be appreciated. -Lex - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using 'join ' function to create a string
On Fri, Dec 21, 2007 at 09:33:31AM -0700, Eric Brunson wrote: lechtlr wrote: Hi there, I would like to know what is the best way to create a string object from two different lists using 'join' function. For example, I have X = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'. How about something like this: , .join( '%s:%s' % ( x, y ) for x, y in zip( X, Y ) ) Slick. I believe that the argument you are passing to join() is the result of a generator expression. Am I right? And, I did not know that str.join(seq) could take an iterator as opposed to a plain sequence. Thanks for showing us that. Back to the original poster's problem, you could also try map() and a lambda: ', '.join(map(lambda x,y: '%s:%s' % (x, y, ), X, Y)) Or, maybe unrolling it makes it more readable: In [31]: fn = lambda x,y: '%s:%s' % (x, y, ) In [32]: ', '.join(map(fn, a, b)) Out[32]: 'aa:11, bb:22, cc:33' - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using 'join ' function to create a string
lechtlr wrote: Hi there, I would like to know what is the best way to create a string object from two different lists using 'join' function. For example, I have X = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'. How about something like this: , .join( '%s:%s' % ( x, y ) for x, y in zip( X, Y ) ) Yields: 'a:1, b:2, c:3, d:4, e:5' Any help would greatly be appreciated. -Lex Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://us.rd.yahoo.com/evt=51734/*http://tools.search.yahoo.com/newsearch/category.php?category=shopping ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using 'join ' function to create a string
lechtlr wrote: Hi there, I would like to know what is the best way to create a string object from two different lists using 'join' function. For example, I have X = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'. Best way? Depends on what you mean by best. My solution: Z = ', '.join(i+':'+str(j) for i, j in zip(X,Y)) It's interesting to note how close your output is to a dictionary display: dict(zip(X,Y)) - {'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4} BTW is is customary in Python to start variable names with lower case letters (x,y,z) in this case. Title case is then used for Classes CAPS is used for CONSTANTS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using 'join ' function to create a string
this works for me. Z=, .join([%s:%s %(a,Y[b]) for b,a in enumerate(X)]) lechtlr [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 12/21/2007 11:00 AM To tutor@python.org cc Subject [Tutor] Using 'join ' function to create a string Hi there, I would like to know what is the best way to create a string object from two different lists using 'join' function. For example, I have X = ['a', 'b', 'c', 'd', 'e'] and Y = [1, 2, 3, 4, 5]. From X and Y, I want to create a string Z = 'a:1, b:2, c:3, d:4, e:5'. Any help would greatly be appreciated. -Lex Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor