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? 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/
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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\"')
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
|
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...
| |