Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10976 > unrolled thread
| Started by | smith jack <thinke365@gmail.com> |
|---|---|
| First post | 2011-08-07 01:07 +0800 |
| Last post | 2011-08-06 22:29 -0700 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.python
how to separate a list into two lists? smith jack <thinke365@gmail.com> - 2011-08-07 01:07 +0800
Re: how to separate a list into two lists? bud <only@fleshwound.org> - 2011-08-06 18:13 +0000
Re: how to separate a list into two lists? Gelonida N <gelonida@gmail.com> - 2011-08-06 22:00 +0200
Re: how to separate a list into two lists? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-07 10:07 +1000
Re: how to separate a list into two lists? Gelonida N <gelonida@gmail.com> - 2011-08-07 19:11 +0200
Re: how to separate a list into two lists? Tim Roberts <timr@probo.com> - 2011-08-06 17:58 -0700
Re: how to separate a list into two lists? Chris Angelico <rosuav@gmail.com> - 2011-08-07 02:05 +0100
Re: how to separate a list into two lists? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-07 11:35 +1000
Re: how to separate a list into two lists? Paul Rubin <no.email@nospam.invalid> - 2011-08-06 22:29 -0700
| From | smith jack <thinke365@gmail.com> |
|---|---|
| Date | 2011-08-07 01:07 +0800 |
| Subject | how to separate a list into two lists? |
| Message-ID | <mailman.1983.1312650424.1164.python-list@python.org> |
if a list L is composed with tuple consists of two elements, that is L = [(a1, b1), (a2, b2) ... (an, bn)] is there any simple way to divide this list into two separate lists , such that L1 = [a1, a2... an] L2=[b1,b2 ... bn] i do not want to use loop, any methods to make this done?
[toc] | [next] | [standalone]
| From | bud <only@fleshwound.org> |
|---|---|
| Date | 2011-08-06 18:13 +0000 |
| Message-ID | <4e3d8440$0$5872$c3e8da3$12bcf670@news.astraweb.com> |
| In reply to | #10976 |
On Sun, 07 Aug 2011 01:07:00 +0800, smith jack wrote: > if a list L is composed with tuple consists of two elements, that is L = > [(a1, b1), (a2, b2) ... (an, bn)] > > is there any simple way to divide this list into two separate lists , > such that L1 = [a1, a2... an] > L2=[b1,b2 ... bn] > > i do not want to use loop, any methods to make this done? (x,y) = [ [z[i] for z in L] for i in range(len(L[0]))] x : ['a1', 'a2', 'an'] y : ['b1', 'b2', 'bn']
[toc] | [prev] | [next] | [standalone]
| From | Gelonida N <gelonida@gmail.com> |
|---|---|
| Date | 2011-08-06 22:00 +0200 |
| Message-ID | <mailman.1994.1312661111.1164.python-list@python.org> |
| In reply to | #10980 |
On 08/06/2011 08:13 PM, bud wrote: > On Sun, 07 Aug 2011 01:07:00 +0800, smith jack wrote: > >> if a list L is composed with tuple consists of two elements, that is L = >> [(a1, b1), (a2, b2) ... (an, bn)] >> >> is there any simple way to divide this list into two separate lists , >> such that L1 = [a1, a2... an] >> L2=[b1,b2 ... bn] >> >> i do not want to use loop, any methods to make this done? > > > (x,y) = [ [z[i] for z in L] for i in range(len(L[0]))] > > x > : ['a1', 'a2', 'an'] > > y > : ['b1', 'b2', 'bn'] > > Asuming you are not an alias of Jack Smith and assuming you did not see Jack's thread asking the same question: x,y = unzip(*L)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-07 10:07 +1000 |
| Message-ID | <4e3dd73c$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #10987 |
Gelonida N wrote: > Asuming you [Bud] are not an alias of Jack Smith and assuming you did > not see Jack's thread asking the same question: That's a strange thing to say when Bud *answered* Jack's question. > x,y = unzip(*L) What's unzip? It doesn't exist in any version of Python between 1.5 and 3.3 that I have. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Gelonida N <gelonida@gmail.com> |
|---|---|
| Date | 2011-08-07 19:11 +0200 |
| Message-ID | <mailman.2014.1312737107.1164.python-list@python.org> |
| In reply to | #10991 |
On 08/07/2011 02:07 AM, Steven D'Aprano wrote: > Gelonida N wrote: > >> Asuming you [Bud] are not an alias of Jack Smith and assuming you did >> not see Jack's thread asking the same question: > > That's a strange thing to say when Bud *answered* Jack's question. > > >> x,y = unzip(*L) > > What's unzip? It doesn't exist in any version of Python between 1.5 and 3.3 > that I have. > Arg typo: I meant of course zip > >
[toc] | [prev] | [next] | [standalone]
| From | Tim Roberts <timr@probo.com> |
|---|---|
| Date | 2011-08-06 17:58 -0700 |
| Message-ID | <mfor37tgmao0e5mlvllj02kgo2esp25dcd@4ax.com> |
| In reply to | #10976 |
smith jack <thinke365@gmail.com> wrote: > >if a list L is composed with tuple consists of two elements, that is >L = [(a1, b1), (a2, b2) ... (an, bn)] > >is there any simple way to divide this list into two separate lists , such that >L1 = [a1, a2... an] >L2=[b1,b2 ... bn] > >i do not want to use loop, any methods to make this done? There will always be a loop. It might not be written with a "for" statement, but there will always be a loop. L1 = [k[0] for k in L] L2 = [k[1] for k in L] I did momentarily consider the following slimy solution: L1 = dict(L).keys() L2 = dict(L).values() but that reorders the tuples. They still correspond, but in a different order. -- Tim Roberts, timr@probo.com Providenza & Boekelheide, Inc.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-08-07 02:05 +0100 |
| Message-ID | <mailman.2002.1312679117.1164.python-list@python.org> |
| In reply to | #10995 |
On Sun, Aug 7, 2011 at 1:58 AM, Tim Roberts <timr@probo.com> wrote: > I did momentarily consider the following slimy solution: > L1 = dict(L).keys() > L2 = dict(L).values() > but that reorders the tuples. They still correspond, but in a different > order. > Which can be overcome with collections.OrderedDict. But what's dict(L) going to do? It's going to loop over L, more than once in fact. I guess the real question is: Why do you wish to avoid a loop? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-08-07 11:35 +1000 |
| Message-ID | <4e3debdb$0$29972$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #10996 |
Chris Angelico wrote: > On Sun, Aug 7, 2011 at 1:58 AM, Tim Roberts <timr@probo.com> wrote: >> I did momentarily consider the following slimy solution: >> L1 = dict(L).keys() >> L2 = dict(L).values() >> but that reorders the tuples. They still correspond, but in a different >> order. >> > > Which can be overcome with collections.OrderedDict. But what's dict(L) > going to do? It's going to loop over L, more than once in fact. > > I guess the real question is: Why do you wish to avoid a loop? I think what the Original Poster actually meant was he wanted to avoid *writing out an explicit loop*. That is, he wants a one-liner, so he doesn't have to think about the details of iterating over the list. When we write: a = sum(a_sequence) aren't we doing the same thing really? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2011-08-06 22:29 -0700 |
| Message-ID | <7xd3ghokl3.fsf@ruckus.brouhaha.com> |
| In reply to | #10976 |
smith jack <thinke365@gmail.com> writes: > if a list L is composed with tuple consists of two elements, that is > L = [(a1, b1), (a2, b2) ... (an, bn)] > > is there any simple way to divide this list into two separate lists , such that > L1 = [a1, a2... an] > L2=[b1,b2 ... bn] > > i do not want to use loop, any methods to make this done? That is called unzipping and there is a sneaky idiom for it: L1,L2 = zip(*L) Your homework assignment is figuring out how that works ;-).
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web