Re: unpacking vars from list of tuples
if you have numpy installed: ln[12]: import numpy as np In [13]: k = np.array([('a', 'bob', 'c'), ('p', 'joe', 'd'), ('x', 'mary', 'z')]) In [14]: k Out[14]: array([['a', 'bob', 'c'], ['p', 'joe', 'd'], ['x', 'mary', 'z']], dtype='|S4') In [15]: k[:,1] Out[15]: array(['bob', 'joe', 'mary'], dtype='|S4') -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
Cool - Now that would be some seriously dense, efficient code! Will have to play with numpy sometime. R. On 17-Sep-09, at 12:25 PM, Chris Colbert wrote: if you have numpy installed: ln[12]: import numpy as np In [13]: k = np.array([('a', 'bob', 'c'), ('p', 'joe', 'd'), ('x', 'mary', 'z')]) In [14]: k Out[14]: array([['a', 'bob', 'c'], ['p', 'joe', 'd'], ['x', 'mary', 'z']], dtype='|S4') In [15]: k[:,1] Out[15]: array(['bob', 'joe', 'mary'], dtype='|S4') -- http://mail.python.org/mailman/listinfo/python-list
unpacking vars from list of tuples
I'm inexperienced with some of the fancy list slicing syntaxes where python shines. If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... Any suggestion is appreciated... Ross. -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
On Tue, Sep 15, 2009 at 2:51 PM, Ross ros...@gmail.com wrote: I'm inexperienced with some of the fancy list slicing syntaxes where python shines. If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... Indeed: myList = [a[1] for a in k] Google for list comprehension python. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
On Tue, Sep 15, 2009 at 11:51 PM, Ross ros...@gmail.com wrote: I'm inexperienced with some of the fancy list slicing syntaxes where python shines. If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... Any suggestion is appreciated... You can use a list comprehension: myList = [a[1] for a in k] -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
On 15 Sep., 23:51, Ross ros...@gmail.com wrote: If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] if a tuple is OK: zip(*k)[1] -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... To add some readability to the other suggested solutions, I'd use tuple unpacking my_list = [name for status, name, code in k] Not knowing what [0] and [2] are, I randomly designated them as status and code, but you likely have your own meanings. If you don't, you can always just use the _ convention: my_list = [name for _, name, _ in k] # or my_list = [name for (_, name, _) in k] As an aside, my_list is preferred over myList in common Python practice. I don't know if there's a preferred convention for with vs without the parens in such a tuple-unpacking list comprehension. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
On Sep 15, 6:00 pm, Andre Engels andreeng...@gmail.com wrote: On Tue, Sep 15, 2009 at 11:51 PM, Ross ros...@gmail.com wrote: I'm inexperienced with some of the fancy list slicing syntaxes where python shines. If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] Thanks both Chris and André. That's quite obvious once it's pointed out for me. Thanks especially for the terminology that will make learning the related concepts a bit easier. Ross for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... Any suggestion is appreciated... You can use a list comprehension: myList = [a[1] for a in k] -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: unpacking vars from list of tuples
Thanks Tim, That's actually the stuff I was trying to remember. my_list = [name for _, name, _ in k] I recalled using some underscores for nice dense unnamed variable unpacking before, but couldn't recall the process. Thanks for that. Ross. On 15-Sep-09, at 6:33 PM, Tim Chase wrote: If I have a list of tuples: k=[(a, bob, c), (p, joe, d), (x, mary, z)] and I want to pull the middle element out of each tuple to make a new list: myList = [bob, joe, mary] is there some compact way to do that? I can imagine the obvious one of myList = [] for a in k: myList.append(a[1]) But I'm guessing Python has something that will do that in one line... To add some readability to the other suggested solutions, I'd use tuple unpacking my_list = [name for status, name, code in k] Not knowing what [0] and [2] are, I randomly designated them as status and code, but you likely have your own meanings. If you don't, you can always just use the _ convention: my_list = [name for _, name, _ in k] # or my_list = [name for (_, name, _) in k] As an aside, my_list is preferred over myList in common Python practice. I don't know if there's a preferred convention for with vs without the parens in such a tuple-unpacking list comprehension. -tkc -- http://mail.python.org/mailman/listinfo/python-list