473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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?

Aug 21 '06 #1
5 1223
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/
Aug 21 '06 #2

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

Aug 21 '06 #3
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

Aug 21 '06 #4

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

Aug 21 '06 #5
On 2006-08-21, Steven <sf********@gmail.comwrote:
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
Aug 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Rajarshi Guha | last post by:
Hi, is there an efficient (pythonic) way in which I could split a list into say 5 groups? By split I mean the the first x members would be one group, the next x members another group and so on 5...
13
by: TB | last post by:
Hi, Is there an elegant way to assign to a list from a list of unknown size? For example, how could you do something like: >>> a, b, c = (line.split(':')) if line could have less than three...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
8
by: Guy Hocking | last post by:
Hi there, I am having a few problems compiling a list box that is conditional on what is selected in another list box. What i need is a List box (lstArea) that displays one thing when the List...
4
by: Alex Dempsey | last post by:
Recently I tried to slice every element of a list of strings. First I tried: f = open("export.xls", "r") lines = f.readlines() for line in lines: line = line line = line.split('\"\t\"') ...
4
by: Andy M | last post by:
ALERT There is a person by the name of Mike Cox who's trying to turn this mailing list into a Big-8 newsgroup. Many of you know that this and most of the other postresql mailing lists are...
14
by: Rob Cowie | last post by:
I'm having a bit of trouble with this so any help would be gratefully recieved... After splitting up a url I have a string of the form 'tag1+tag2+tag3-tag4', or '-tag1-tag2' etc. The first tag...
6
by: rkmr.em | last post by:
Hi I need to process a really huge text file (4GB) and this is what i need to do. It takes for ever to complete this. I read some where that "list comprehension" can fast up things. Can you point...
3
by: Jordan | last post by:
I want to store small collections of objects that have a unique id string:"Name", so I opted to use a Dictionary<string,MyObject> collection. However, since my collection will rarely exceed 10 or...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.