Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

List Splitting

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


sfaulconer at gmail

Aug 21, 2006, 12:17 PM

Post #1 of 7 (100 views)
Permalink
List Splitting

Hello everyone,

I'm trying to work through a bit of a logic issue I'm having with a
script I'm writing. Essentially, I have a list that's returned to
me from another command that I need to regroup based on some aribitrary
length.

For the purposes of this question, the list will be:

t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]

Now, I know that every 3rd element of the list belongs together:

Group 1 = 0, 3, 6
Group 2 = 1, 4, 7
Group 3 = 2, 5, 8

I'm trying to sort this list out so that I get a list of lists that
contain the correct elements:

Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
["c", "a", "t" ] ]

The actual data isn't as simple as this, but if I can get the logic
sorted out, I can handle the other part.

Anyone have any good ideas on how to do this?

--
http://mail.python.org/mailman/listinfo/python-list


klaus at seistrup

Aug 21, 2006, 12:22 PM

Post #2 of 7 (96 views)
Permalink
Re: List splitting [In reply to]

Steven skrev:

> For the purposes of this question, the list will be:
>
> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>
> Now, I know that every 3rd element of the list belongs together:
>
> Group 1 = 0, 3, 6
> Group 2 = 1, 4, 7
> Group 3 = 2, 5, 8
>
> I'm trying to sort this list out so that I get a list of lists
> that contain the correct elements:
>
> Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
> ["c", "a", "t" ] ]

#v+

>>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>>> [t[i::3] for i in range(3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
>>>

#v-

Cheers,

--
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/
--
http://mail.python.org/mailman/listinfo/python-list


bill.pursell at gmail

Aug 21, 2006, 12:25 PM

Post #3 of 7 (93 views)
Permalink
Re: List Splitting [In reply to]

Steven wrote:
> Hello everyone,
>
> I'm trying to work through a bit of a logic issue I'm having with a
> script I'm writing. Essentially, I have a list that's returned to
> me from another command that I need to regroup based on some aribitrary
> length.
>
> For the purposes of this question, the list will be:
>
> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>
> Now, I know that every 3rd element of the list belongs together:
>
> Group 1 = 0, 3, 6
> Group 2 = 1, 4, 7
> Group 3 = 2, 5, 8
>
> I'm trying to sort this list out so that I get a list of lists that
> contain the correct elements:
>
> Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
> ["c", "a", "t" ] ]
>
> The actual data isn't as simple as this, but if I can get the logic
> sorted out, I can handle the other part.
>
> Anyone have any good ideas on how to do this?

how about:
>>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>>> [t[i::3] for i in range(0,len(t)/3)]
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]
--
Bill Pursell

--
http://mail.python.org/mailman/listinfo/python-list


python.list at tim

Aug 21, 2006, 12:28 PM

Post #4 of 7 (96 views)
Permalink
Re: List Splitting [In reply to]

> For the purposes of this question, the list will be:
>
> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>
> Now, I know that every 3rd element of the list belongs together:
>
> Group 1 = 0, 3, 6
> Group 2 = 1, 4, 7
> Group 3 = 2, 5, 8
>
> I'm trying to sort this list out so that I get a list of lists that
> contain the correct elements:
>
> Goal = [ [ "a", "n", "t"], [ "b", "a", "t"],
> ["c", "a", "t" ] ]

Well, the following worked for me:

>>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>>> stride = 3
>>> Goal = [t[i::stride] for i in range(stride)]
>>> Goal
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]


Or, if you like, in this example:

>>> [''.join(t[i::stride]) for i in range(stride)]
['ant', 'bat', 'cat']

if that's of any use.

-tkc





--
http://mail.python.org/mailman/listinfo/python-list


sfaulconer at gmail

Aug 21, 2006, 12:30 PM

Post #5 of 7 (96 views)
Permalink
Re: List splitting [In reply to]

Klaus Alexander Seistrup wrote:
> >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
> >>> [t[i::3] for i in range(3)]
> [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]

Klaus,

Thanks for the fast reply! Had I taken the time to look at the
list-type docs (which I did to understand how you were spliting the
list), I'd probably have seen the slicing with step option. Another
RTFM issue for me.

Thanks again,
Steven

--
http://mail.python.org/mailman/listinfo/python-list


btowle at carnegielearning

Aug 21, 2006, 12:38 PM

Post #6 of 7 (91 views)
Permalink
Re: List Splitting [In reply to]

Not the most elegant solution, perhaps, but:

>>>t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>>>outlist = []
>>>for x in range(3):
... temp=[]
... for y in range(len(t)/3):
... temp.append(t[3*y+x])
... outlist.append(temp)

>>>outlist
[['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']]

--
Brendon Towle, PhD
Cognitive Scientist
+1-412-690-2442x127
Carnegie Learning, Inc.
The Cognitive Tutor Company ®
Helping over 375,000 students in 1000 school districts succeed in math.


cerutti at tds

Aug 21, 2006, 12:42 PM

Post #7 of 7 (97 views)
Permalink
Re: List Splitting [In reply to]

On 2006-08-21, Steven <sfaulconer [at] gmail> wrote:
> Hello everyone,
>
> I'm trying to work through a bit of a logic issue I'm having with a
> script I'm writing. Essentially, I have a list that's returned to
> me from another command that I need to regroup based on some aribitrary
> length.
>
> For the purposes of this question, the list will be:
>
> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ]
>
> Now, I know that every 3rd element of the list belongs together:
>
> Group 1 = 0, 3, 6
> Group 2 = 1, 4, 7
> Group 3 = 2, 5, 8

from itertools import islice

grouped = []
grouped.append(list(islice(t, 0, None, 3))
grouped.append(list(islice(t, 1, None, 3))
grouped.append(list(islice(t, 2, None, 3))
grouped.sort()

This can probably be simplified and generalized, but I'm a novice, and
that's a start.

--
Neil Cerutti
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.